业务场景:用户勾选想要导出的字段
本来想着百度搜下大神封装好的util,直接拿过来用,然而发现网上大都是写死的(网上好多都是全表字段导出)
于是就自己写了个通用的动态导出封装类,前台选中什么字段就导出什么字段
先看效果:
这是我数据库表的字段
假如用户现在只想导出name和phone这两个字段:
直接上导出成功效果图:
第一步:导入POM
<!--excel导入导出 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.11-beta2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.11-beta2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.11-beta2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-excelant</artifactId>
<version>3.11-beta2</version>
</dependency>
第二部:导入我的util类(注释写的很明白啦)
/**
* @param title 表格的名字
* @param heards 前台选中要导出的字段
* @param list 查询数据库返回所有字段的结果
* @param out 输出流
* @throws Exception
*/
public static void exportUtil(String title, String[] heards, List<?> list, OutputStream out) throws Exception {
// 创建excel工作薄
HSSFWorkbook workbook = new HSSFWorkbook();
// 创建excel表
HSSFSheet sheet = workbook.createSheet(title);
// 创建标题行
HSSFRow titleRow = sheet.createRow(0);
// 标题行写内容
for (int i = 0; i < heards.length; i++) {
String s = heards[i];
HSSFCell cell = titleRow.createCell(i);
cell.setCellValue(s);
}
// 数据行,从1行开始,共list.size行
for (int i = 1; i <= list.size(); i++) {
// 创建行
HSSFRow listRow = sheet.createRow(i);
// 数据行单元格,从0开始,共heards.length列
for (int j = 0; j < heards.length; j++) {
// 创建数据单元格
HSSFCell listCell = listRow.createCell(j);
// 通过反射的方式,将heards元素通过字符串拼接的方式,拼接出实体类相对应的get方法;
String methodName = "get" + heards[j].substring(0, 1).toUpperCase() + heards[j].substring(1);
try {
// 通过反射拿到类对象,再获取类对象的额methodName这个方法
Method declaredMethod = Student.class.getDeclaredMethod(methodName, null);
// 通过invoke提交对象,执行declaredMethod这个方法
// 从List<Student>集合中取出list.get(i - 1)的methodName属性的值;
Object result = declaredMethod.invoke(list.get(i - 1));
//判断返回值的类型
if (result instanceof Date) {
//填写日期格式内容
HSSFDataFormat dataFormat = workbook.createDataFormat();
short format = dataFormat.getFormat("yyyy年MM月dd日");
HSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cellStyle.setDataFormat(format);
listCell.setCellStyle(cellStyle);
} else {
//设置内容
listCell.setCellValue(result.toString());
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
}
/*// 设置响应头
response.setContentType("application/vnd.ms-excel");
String fileName = "用户自定义信息表.xls";
response.setCharacterEncoding("utf-8");
response.setContentType("multipart/form-data");
response.addHeader("Content-Disposition", "attachment;" + fileName);
workbook.write(response.getOutputStream());*/
try {
workbook.write(out);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
第三部:直接调用测试
@RequestMapping("/exports")
public Map exports(HttpServletResponse response) throws Exception {
Map map = new HashMap();
String[] heards = {"id", "name"};
List<Student> list = exportService.export();
exportUtil("zsp", heards, list, new FileOutputStream("E:/test.xls"));
return map;
}