\n * 提供列表和函數作爲數據源\n */\npublic static void writeAutoWriter() {\n final String fileName = defaultFileName(\"writeAutoWriter\");\n EasyExcelFactory.write(fileName)\n .head(Item.class)\n .sheet(\"模板\")\n .doWrite(WriteSample::sampleItems);\n}\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"EasyExcel提供了","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"EasyExcelFactory","attrs":{}}],"attrs":{}},{"type":"text","text":"類,API方法也是fluent方式,可以如絲般順滑的實現生成Excel文件。如果感覺","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"EasyExcelFactory","attrs":{}}],"attrs":{}},{"type":"text","text":"太長,還可以直接寫作","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"EasyExcel","attrs":{}}],"attrs":{}},{"type":"text","text":",這是","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"EasyExcelFactory","attrs":{}}],"attrs":{}},{"type":"text","text":"的子類,類似於別名。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"不過,這種別名定義方式,在有些規範中屬於","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"smell code","attrs":{}}],"attrs":{}},{"type":"text","text":",所以,根據自己或者公司規範選擇吧。","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"com.alibaba.excel.EasyExcelFactory#write(java.lang.String)","attrs":{}}],"attrs":{}},{"type":"text","text":"方法的參數傳的是導出文件的文件名,如果並不需要生成文件,只需要創建文件流,也可以傳入一個輸出流","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"OutputStream","attrs":{}}],"attrs":{}},{"type":"text","text":",這樣就可以更加靈活的實現生成邏輯了。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"com.alibaba.excel.metadata.AbstractParameterBuilder#head()","attrs":{}}],"attrs":{}},{"type":"text","text":"方法是定義表頭,只要傳入一個類,就會讀取這個類的所有字段作爲表頭。如果字段上","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"com.alibaba.excel.annotation.ExcelProperty","attrs":{}}],"attrs":{}},{"type":"text","text":"註解,定義了","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"value","attrs":{}}],"attrs":{}},{"type":"text","text":",就會取","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"value","attrs":{}}],"attrs":{}},{"type":"text","text":"的值作爲表頭。此處還有很多操作,比如,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"value","attrs":{}}],"attrs":{}},{"type":"text","text":"是數組,可以定義多個,如果是相鄰字段定義了相同的表頭,會合並單元格,表體內容會選擇第一個單元格的內容。這個註解還可以定義","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"index","attrs":{}}],"attrs":{}},{"type":"text","text":"、","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"order","attrs":{}}],"attrs":{}},{"type":"text","text":"、","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"converter","attrs":{}}],"attrs":{}},{"type":"text","text":"等,後面會一一給出例子。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"com.alibaba.excel.write.builder.ExcelWriterBuilder#sheet()","attrs":{}}],"attrs":{}},{"type":"text","text":"方法定義工作表,有多個重載方法,可以定義","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"sheetNo","attrs":{}}],"attrs":{}},{"type":"text","text":"指明是第幾個工作表,可以傳入","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"sheetName","attrs":{}}],"attrs":{}},{"type":"text","text":"指明工作表名稱。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"com.alibaba.excel.write.builder.ExcelWriterSheetBuilder#doWrite()","attrs":{}}],"attrs":{}},{"type":"text","text":"方法就是寫Excel文件了,傳入全部的列表數據,或者使用Java8+的","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Supplier","attrs":{}}],"attrs":{}},{"type":"text","text":"函數。還可以實現分頁寫入,後面會給出例子。這個方法會自動關閉文件流,真是很貼心。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"結果爲:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/a0/a0de5cb490bf6616084bed5a19ec208c.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"根據頭對象和列表向多個工作表中寫數據","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上面是向一個工作表寫數據,接下來我們向多個工作表寫數據。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這個例子會涉及更多的內部對象,比如:ExcelWriter、WriteSheet。","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"/**\n * 手動創建{@link com.alibaba.excel.ExcelWriter},指定sheet寫入數據。\n *
\n * 提供列表和函數作爲數據源\n */\npublic static void writeManualWither() {\n String fileName = defaultFileName(\"writeManualWriter\");\n ExcelWriter excelWriter = null;\n try {\n excelWriter = EasyExcelFactory.write(fileName)\n .head(Item.class)\n .build();\n final WriteSheet writeSheet1 = EasyExcelFactory.writerSheet(\"模板1\").build();\n excelWriter.write(WriteSample::sampleItems, writeSheet1);\n\n final WriteSheet writeSheet2 = EasyExcelFactory.writerSheet(\"模板2\").build();\n excelWriter.write(sampleItems(), writeSheet2);\n } finally {\n // 千萬別忘記finish 會幫忙關閉流\n if (excelWriter != null) {\n excelWriter.finish();\n }\n }\n}\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"因爲是想多個工作表中寫數據,我們就不能直接使用","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"doWrite","attrs":{}}],"attrs":{}},{"type":"text","text":"方法了。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"com.alibaba.excel.ExcelWriter","attrs":{}}],"attrs":{}},{"type":"text","text":"類是Excel寫對象,用來創建Excel工作簿的。","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"com.alibaba.excel.write.metadata.WriteSheet","attrs":{}}],"attrs":{}},{"type":"text","text":"類是Sheet寫對象,用來創建Sheet工作表的。通過","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"com.alibaba.excel.ExcelWriter#write()","attrs":{}}],"attrs":{}},{"type":"text","text":"方法,指定寫入數據和寫入的目標工作表,就可以實現向多個工作表中寫數據的功能。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"此處需要注意,我們在創建","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"ExcelWriter","attrs":{}}],"attrs":{}},{"type":"text","text":"對象時,調用了","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"head()","attrs":{}}],"attrs":{}},{"type":"text","text":"方法定義了表頭,這是整個Excel的定義,sheet會繼承這個定義。這樣,整個Excel文件中的所有工作表,表頭都是相同的。不要停,後面會給出不同工作表定義不同表頭的示例。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"結果爲:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/8f/8f2c86fea7f678a4456af7727f7feeb6.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"按照定義指定導出列","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在後臺系統中,會有行列權限的控制。行權限,通過數據行實現,只導出有權限的行數據即可。列權限,可以通過只導出有權限的列,排除沒有權限的列(通常是分等級的敏感數據)。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"有時候需要定製化導出,導出所有列表格比較大,用戶根據需要指定需要導出的列。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"排除指定列","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"private static void writeExcludeColumn() {\n String fileName = defaultFileName(\"writeExcludeColumn\");\n Set \n * 可以實現多層表頭。\n */\nprivate static void writeDynamicHead() {\n String fileName = defaultFileName(\"writeDynamicHead\");\n EasyExcelFactory.write(fileName)\n .head(dynamicHead())\n .sheet()\n .doWrite(sampleItems());\n}\n\nprivate static List>}格式數據。\n *
> dynamicHead() {\n List
> heads = new ArrayList<>();\n final List
>","attrs":{}}],"attrs":{}},{"type":"text","text":"類型的數據即可。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"結果爲:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/5e/5ed1e13df09a2fc9feac3f6ec17c0197.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當然,這樣做還不是徹底的動態。我們可以使用","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"com.alibaba.excel.write.builder.ExcelWriterSheetBuilder#doWrite(java.util.Collection>)","attrs":{}}],"attrs":{}},{"type":"text","text":"實現動態表體。代碼如下:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"private static void writeDynamicData() {\n String fileName = defaultFileName(\"writeDynamicData\");\n EasyExcelFactory.write(fileName)\n .head(dynamicHead())\n .sheet()\n .doWrite(dynamicData());\n}\n\nprivate static List
> dynamicData() {\n List
> list = new ArrayList<>();\n for (int i = 0; i < 10; i++) {\n List