1.jar包选择
2.数据库表
3.JDBC连接数据库工具类
package Test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBhepler {
String url ="jdbc:mysql://192.168.0.109:3306/javenforexcel?useUnicode=true&characterEncoding=utf-8";
String user="root";
String pwd="123456";
Connection conn=null;
ResultSet rs=null;
public void DataBase() {
try {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection(url, user, pwd);
} catch (ClassNotFoundException e) {
System.out.println("装载JDBC 驱动程序失败");
e.printStackTrace();
} catch (SQLException e) {
System.out.println("无法连接数据库");
e.printStackTrace();
}
}
//查询
public ResultSet Search(String sql,String str[]) {
DataBase();
try {
PreparedStatement pst=conn.prepareStatement(sql);
if (str!=null) {
for (int i = 0; i < str.length; i++) {
pst.setString(i+1, str[i]);
}
}
rs=pst.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
//增删修改
public int AddU(String sql,String str[]) {
int a=0;
DataBase();
try {
PreparedStatement pst=conn.prepareStatement(sql);
if (str!=null) {
for (int i = 0; i < str.length; i++) {
pst.setString(i+1, str[i]);
}
}
a=pst.executeUpdate();
} catch (Exception e) {
// TODO: handle exception
}
return a;
}
}
4.查询数据库数据表的工具类
package service;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import Test.DBhepler;
import entity.StuEntity;
public class StuService {
//查询表中所有的数据
public static List<StuEntity> getAllByDb(){
List<StuEntity>list=new ArrayList<StuEntity>();
try {
DBhepler db=new DBhepler();
String sql="select * from stu";
ResultSet rs=db.Search(sql, null);
while (rs.next()) {
int id=rs.getInt("id");
String name=rs.getString("name");
String sex=rs.getString("sex");
int num=rs.getInt("num");
StuEntity entity=new StuEntity();
entity.setId(id);
entity.setName(name);
entity.setSex(sex);
entity.setNum(num);
list.add(entity);
}
} catch (Exception e) {
// TODO: handle exception
}
return list;
}
}
5.建立Excel表,并将读取到的数据库表的数据写入到表格中
package excel;
import java.io.File;
import java.util.List;
import entity.StuEntity;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import service.StuService;
public class TestDbToExcel {
public static void main(String[] args) {
try {
WritableWorkbook wwb=null;
//创建可写入的Excel工作薄
String fileName="D://book.xls";
File file=new File(fileName);
if(!file.exists()) {
file.createNewFile();
}
//以fileName 为文件名来创建一个Workbook
wwb=Workbook.createWorkbook(file);
//创建工作表
WritableSheet ws=wwb.createSheet("Test Shee 1", 0);
//查询数据库中所有的数据
List<StuEntity> list=StuService.getAllByDb();
//要插入到的Excel表格的行号,默认从0开始
Label labelId=new Label(0, 0, "编号(id)");
Label labelName=new Label(1, 0, "姓名(name)");
Label labelSex=new Label(2, 0, "性别(sex)");
Label labelNum=new Label(3, 0, "薪水(num)");
ws.addCell(labelId);
ws.addCell(labelName);
ws.addCell(labelSex);
ws.addCell(labelNum);
for (int i = 0; i < list.size(); i++) {
Label labelId_i=new Label(0, i+1, list.get(i).getId()+"");
Label labelName_i=new Label(1, i+1, list.get(i).getName());
Label labelSex_i=new Label(2, i+1, list.get(i).getSex());
Label labelNum_i=new Label(3, i+1, list.get(i).getNum()+"");
ws.addCell(labelId_i);
ws.addCell(labelName_i);
ws.addCell(labelSex_i);
ws.addCell(labelNum_i);
}
//写进文档
wwb.write();
System.out.println("数据写入成功");
//关闭Excel工作簿对象
wwb.close();
} catch (Exception e) {
System.out.println("数据写入失败");
e.printStackTrace();
}
}
}