工作上客户经常需要统计一些数据导出成excel,那么这个时候就可以用一下easypoi来解决一下他们的需求。代码如下:
我们项目用的是pom.xml,就是你在里面写点东西,人家自动帮你把包下载下来的那玩意。
为了使用easypoi,我们需要在里面写以下配置:
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
</dependency>
接下来是java代码:
import ...//一堆包
public String outputChatNum(){//导出的逻辑
List<ChatNumExcel> chatNumExcelList1 = customerService.chatNumExcel(startTime, endTime);//这里就是把要导出的数据安排了一下
List<ChatNumExcel> chatNumExcelList2 = customerService.chatNumExcel(startTime, endTime);//这里就是把要导出的数据再安排一下
ExportParams ep1 = new ExportParams();
ExportParams ep2 = new ExportParams();
ep1.setSheetName("第一个sheet的名字");
ep1.setStyle(ExcelExportStylerDefaultImpl.class);//设置excel页面样式,这里用的是eastpoi提供的默认样式
ep2.setSheetName("第二个sheet的名字");
ep2.setStyle(ExcelExportStylerDefaultImpl.class);
System.out.println(i++);
Map<String, Object> map1 = new HashMap<String, Object>();
map1.put("title", ep1);
map1.put("entity", ChatNumExcel.class);//这里是导出的数据的对象
map1.put("data", chatNumExcelList1);
Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("title", ep2);
map2.put("entity", ChatNumExcel.class);
map2.put("data", chatNumExcelList2);
List<Map<String, Object>> mapList = new ArrayList<>();//这里这样搞一下就可以导出多个sheet
mapList.add(map1);
mapList.add(map2);
Workbook workbook = ExcelExportUtil.exportExcel(mapList, ExcelType.HSSF);
try {
ServletOutputStream outStream = null;
String fileName = "文件的名字.xls";
setFileDownloadHeader(request, response, fileName);//这个方法在下面
outStream = response.getOutputStream();
workbook.write(outStream);//作为流搞出来
outStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void setFileDownloadHeader(HttpServletRequest request, HttpServletResponse response,
String fileName) {//网上抄的设置文件头的代码
try {
// 中文文件名支持
String encodedfileName = null;
String agent = request.getHeader("USER-AGENT");
if (null != agent && -1 != agent.indexOf("MSIE")) {// IE
encodedfileName = java.net.URLEncoder.encode(fileName, "UTF-8");
} else if (null != agent && -1 != agent.indexOf("Mozilla")) {
encodedfileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
} else {
encodedfileName = java.net.URLEncoder.encode(fileName, "UTF-8");
}
response.setHeader("Content-Disposition", "attachment; filename=\"" + encodedfileName + "\"");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
下面是上面数据的类的定义:
import cn.afterturn.easypoi.excel.annotation.Excel;
public class ChatNumExcel{//数据对象对应的类的结构
private String loginId;
@Excel(name = "姓名", orderNum = "0", width = 20)
private String name;
@Excel(name = "工号", orderNum = "1", width = 20)
private String loginName;
public String getLoginId() {
return loginId;
}
public void setLoginId(String loginId) {
this.loginId = loginId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
}
完事辣,这样要是还是看不懂,那你可真是个臭弟弟。