使用poi导出excel表基础。
本人使用的是ssm的框架。使用了mybatis的逆向工程。数据的获取方法很多,如果用servlet大同小异。
在页面中设置一个按钮
为按钮添加点击事件。
<button type="button" class="btn btn-export" onclick = doExportExcel()>导出Excel</button>
/*这个方法用来导出excel信息*/
function doExportExcel() {
var url = "sys/exportExcelEdu.do";
//post提交请求url返回数据库数据
$.post(url, function (result) {
//对回调函数进行判定,返回jsonresult对象的状态如果==1,收到数据
if (result.state == 1) {
alert(result.message);
} else {
//如果jsonresult对象状态错误,弹出提示信息
alert(result.message);
}
},"json");
}
在controller层编写实现代码。
//导出Excel表教育局信息
@RequestMapping("exportExcelEdu")
@ResponseBody
public JsonResult exportExcelEdu(){
String message = educationService.exportExcelEdu();
JsonResult r = new JsonResult();
r.setMessage(message);
return r;
}
调用的servcie实现类。
@Override
public String exportExcelEdu() {
//1、创建工作簿
Workbook wb = new XSSFWorkbook();
//1.1、设置表格的格式----居中
CellStyle cs = wb.createCellStyle();
cs.setAlignment(VERTICAL_CENTER);
//2.1、创建工作表
Sheet sheet = wb.createSheet("教育局信息表格");
//2.2、合并单元格
sheet.setDefaultColumnWidth((int)((20 + 0.72) * 256));
sheet.setDefaultRowHeightInPoints(20);
sheet.setColumnWidth(0, (int)((20 + 0.72) * 256));
sheet.setColumnWidth(1, (int)((20 + 0.72) * 256));
sheet.setColumnWidth(2, (int)((20 + 0.72) * 256));
sheet.setColumnWidth(3, (int)((20 + 0.72) * 256));
sheet.setColumnWidth(4, (int)((20 + 0.72) * 256));
sheet.setColumnWidth(5, (int)((20 + 0.72) * 256));
sheet.setColumnWidth(6, (int)((20 + 0.72) * 256));
//3.1、创建行----表头行
Row row = sheet.createRow(0);
//4、创建格
row.setHeightInPoints(20);
Cell cell = row.createCell(0);
cell.setCellValue("教育局编号");
cell.setCellStyle(cs);
cell = row.createCell(1);
cell.setCellValue("教育局名称");
cell.setCellStyle(cs);
cell = row.createCell(2);
cell.setCellValue("教育局管理员");
cell.setCellStyle(cs);
cell = row.createCell(3);
cell.setCellValue("所在区县");
cell.setCellStyle(cs);
cell = row.createCell(4);
cell.setCellValue("所在地市");
cell.setCellStyle(cs);
cell = row.createCell(5);
cell.setCellValue("所在省份");
cell.setCellStyle(cs);
cell = row.createCell(6);
cell.setCellValue("备注");
cell.setCellStyle(cs);
//5、写入实体数据
TbEducationExample example = new TbEducationExample();
List<TbEducation> list = tbEducationMapper.selectByExample(example);
for (int i = 0; i < list.size(); i++) {
//3.2、创建行----内容行
row = sheet.createRow(i+1);
row.setHeightInPoints(20);
TbEducation us = list.get(i);
//第几行第几格 第一行第一格为“code”
row.createCell(0).setCellValue(us.getEducationId());
row.createCell(1).setCellValue(us.getEducationName());
row.createCell(2).setCellValue(us.getEducationAdmin());
row.createCell(3).setCellValue(us.getEducationArea());
row.createCell(4).setCellValue(us.getEducationCity());
row.createCell(5).setCellValue(us.getEducationProvince());
row.createCell(6).setCellValue((us.getEducationRemarks()==null) ? "":us.getEducationRemarks());
//row.createCell(2).setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(us.get()));
}
//6、将文件储存到指定位置
try {
File file=new File("d:\\河北省师资管理培训系统");
if(!file.exists()){//如果文件夹不存在
file.mkdir();//创建文件夹
}
FileOutputStream fout = new FileOutputStream("d:\\河北省师资管理培训系统\\教育局信息表.xls");
wb.write(fout);
fout.close();
} catch (IOException e) {
e.printStackTrace();
}
return "Excel成功导出到D盘";
}
具体查询数据的dao层代码因人而异这里就不粘贴了。
这样导出成功后会在D盘河北省师资管理培训系统下生成一个图表。
样式如下。