可以直接拿走用的 excel 导出工具类
public void outPutExcel(String year, int id,String name) {
//创建表头,这里是excel的第一行数据
//第一处改点
String[] title = {"姓名","年龄","性别"};
//创建Excel工作部
HSSFWorkbook workbook = new HSSFWorkbook();
//创建一个工作表sheet1
HSSFSheet sheet = workbook.createSheet();
//创建第一行
HSSFRow row = sheet.createRow(0);
HSSFCell cell = null;
//把第一行的excel数据插入,坐表头
for (int i = 0;i<title.length;i++){
cell = row.createCell(i);
cell.setCellValue(title[i]);
}
//这里是数据库里查出来的数据
//第二处改点
List<people> peoples =
//把数据一个一个添加到sheet1中
//第三处改点 改掉list中的元素属性就可以了
for (int i = 1; i<=peoples.size();i++){
HSSFRow nexTrow = sheet.createRow(i);
HSSFCell cell1 = nexTrow.createCell(0);
cell1.setCellValue(peoples.get(i-1).getName()); //list对象中第一个属性对应 名字
cell1 = nexTrow.createCell(1);
cell1.setCellValue(peoples.get(i-1).getAGE()); //list对象中第二个属性对应 年龄
cell1 = nexTrow.createCell(2);
cell1.setCellValue(peoples.get(i-1).getGender()); //list对象中第三个属性对应 性别
cell1 = nexTrow.createCell(3);
}
//真正的导出就这一句话
//这里写上路径和你想要导出的表明,就欧克了
//最后一处改点
BufferedOutputStream out = FileUtil.getOutputStream("路径"+"表明"+".xls");
try {
workbook.write(out);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}