發個博客推廣一下alibaba的easyExcel,以一些我使用的體會技巧。
我理解easyexcel就是在poi上進行了優化和封裝,性能的道路極高的改善。極高,官網測試1:100的速度。
在使用上也極度好理解,就是把excel抽象成二維數組,我們只需要注意數據類型及表格樣式,在二維數組中設置。就可以實現自己想看的功能。比以前poi或其他的使用,簡單超多倍。
easyexcel github連接:https://github.com/alibaba/easyexcel
官網有很多案例,我寫我用怎麼用的,上代碼。
@Test
public void test01() throws Exception {
OutputStream out = new FileOutputStream(
"C:/Users/Administrator/Desktop/test.xls");
ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLS, false);
// 寫第一個sheet, sheet1 數據全是List<String> 無模型映射關係
Sheet sheet1 = new Sheet(1,0);
sheet1.setSheetName("玫瑰圖");
// 去掉表頭 如果不設置,在不設置表頭,總是從第二行開始有數據
sheet1.setStartRow(-1);
int colWidth = 2400;
// 設置列寬 設置每列的寬度
Map columnWidth = new HashMap();
for (int i = 0; i < 17; i++) {
columnWidth.put(i,colWidth);
}
sheet1.setColumnWidthMap(columnWidth);
writer.write0(createTestListString(), sheet1);
writer.finish();
out.close();
}
private static List<List<String>> createTestListString() {
List<List<String>> object = new ArrayList<List<String>>();
for (int i = 0; i < 10; i++) {
List<String> da = new ArrayList<String>();
da.add("");
da.add("字符串"+i);
da.add("字符串"+i);
da.add("字符串"+i);
da.add("字符串"+i);
da.add("字符串"+i);
da.add("字符串"+i);
da.add("字符串"+i);
object.add(da);
}
return object;
}
然後就可以看到生成的excel了。
在無恥的沾一下人家web下載案例:
public class Down {
@GetMapping("/a.htm")
public void cooperation(HttpServletRequest request, HttpServletResponse response) {
ServletOutputStream out = response.getOutputStream();
response.setContentType("multipart/form-data");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-disposition", "attachment;filename="+fileName+".xlsx");
ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX, true);
String fileName = new String(("UserInfo " + new SimpleDateFormat("yyyy-MM-dd").format(new Date()))
.getBytes(), "UTF-8");
Sheet sheet1 = new Sheet(1, 0);
sheet1.setSheetName("第一個sheet");
writer.write0(getListString(), sheet1);
writer.finish();
out.flush();
}
}
}
主要還推廣和我的理解。
java操作easyexcel環境cglib-3.1.jar commons-collections4-4.1.jar easyexcel-1.1.2-beta5.jar junit-4.10.jar poi-3.17.jar poi-ooxml-3.17.jar poi-ooxml-schemas
環境jar包下載鏈接:https://download.csdn.net/download/zl15145428/11317546