Java實現(xiàn)導(dǎo)出word表格的示例詳解
目標
多級表頭、分頁、動態(tài)數(shù)據(jù)
實現(xiàn)
依賴
<!-- poi工具類--> <dependency> <groupId>com.deepoove</groupId> <artifactId>poi-tl</artifactId> <version>1.12.0</version> </dependency>
模版
代碼
TableData數(shù)據(jù)(模版對應(yīng)的數(shù)據(jù)對象)
package org.example.bean; import com.deepoove.poi.data.TableRenderData; import lombok.Data; @Data public class TableData { /** * 標題 */ private String title; /** * 表格 */ private TableRenderData table; private String[][] tableList; /** * 總價 */ private String totalPrice; }
核心代碼
package org.example.controller; import lombok.SneakyThrows; import org.example.bean.TableData; import org.springframework.web.bind.annotation.*; import com.deepoove.poi.XWPFTemplate; import com.deepoove.poi.data.*; import java.io.*; import java.math.BigDecimal; import java.net.URLEncoder; import javax.servlet.http.HttpServletResponse; /** * Java導(dǎo)出word表格 * 根據(jù)word模版,手繪表格 */ @RestController @RequestMapping(value = "/word") public class WordController { @GetMapping(value = "/table") @SneakyThrows public void table(TableData tableData, HttpServletResponse response) { /* 假數(shù)據(jù) */ tableData.setTitle("附件1-報價明細表"); String[][] strings = new String[100][5]; for (int i = 0; i < 100; i++) { strings[i] = new String[]{"1", "EREWHON", "生豬", "酒鬼酒", "125"}; } tableData.setTableList(strings); // 模版路徑 String wordPath = "/Users/issavior/java/java/seckill-redis/test/src/main/resources/"; String modelName = "表格.docx"; // 手繪表格 // 表頭 RowRenderData row0 = Rows.of("項號", "編號", "種類", "", "價格").center().create(); RowRenderData row1 = Rows.of("項號", "編號", "期貨", "股票", "價格").center().create(); int length = 0; if (tableData.getTableList() != null) { length = tableData.getTableList().length; } // 表格數(shù)據(jù) 加上2行表頭 再加上最后一行總價 RowRenderData[] rowRenderData = new RowRenderData[length + 3]; rowRenderData[0] = row0; rowRenderData[1] = row1; // 計算價錢 BigDecimal totalPrice = new BigDecimal("0"); for (int i = 0; i < length; i++) { rowRenderData[i + 2] = Rows.of(tableData.getTableList()[i]).center().create(); String s = tableData.getTableList()[i][4]; BigDecimal bigDecimal = new BigDecimal(s); totalPrice = totalPrice.add(bigDecimal); } RowRenderData row4 = Rows.of("總價", "", "", "", totalPrice.toString()).center().create(); rowRenderData[rowRenderData.length - 1] = row4; // 表格合并,根據(jù)坐標 MergeCellRule rule = MergeCellRule.builder().map(MergeCellRule.Grid.of(0, 0), MergeCellRule.Grid.of(1, 0)). map(MergeCellRule.Grid.of(0, 1), MergeCellRule.Grid.of(1, 1)). map(MergeCellRule.Grid.of(0, 2), MergeCellRule.Grid.of(0, 3)). map(MergeCellRule.Grid.of(0, 4), MergeCellRule.Grid.of(1, 4)). map(MergeCellRule.Grid.of(rowRenderData.length - 1, 0), MergeCellRule.Grid.of(rowRenderData.length - 1, 3)). build(); TableRenderData table = Tables.of(rowRenderData).mergeRule(rule).create(); // 數(shù)據(jù)封裝 tableData.setTable(table); // 傳入模板模板地址+信息數(shù)據(jù) XWPFTemplate template = XWPFTemplate.compile(wordPath + modelName).render(tableData); // 指定下載的文件名--設(shè)置響應(yīng)頭 response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("附件1-報價明細表.docx", "UTF-8")); response.setContentType("application/vnd.ms-excel;charset=UTF-8"); response.setHeader("Pragma", "no-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); try { OutputStream out = response.getOutputStream(); BufferedOutputStream bos = new BufferedOutputStream(out); template.write(out); bos.flush(); out.flush(); template.close(); } catch (IOException e) { e.printStackTrace(); } } }
到此這篇關(guān)于Java實現(xiàn)導(dǎo)出word表格的示例詳解的文章就介紹到這了,更多相關(guān)Java導(dǎo)出word表格內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java中的異常處理(try,catch,finally,throw,throws)
本文主要介紹了Java中的異常處理,文章主要介紹的異常處理包括5個關(guān)鍵字try,catch,finally,throw,throws,更多詳細內(nèi)容需要的朋友可以參考一下2022-06-06springboot配置文件如何實現(xiàn)多個yml相互讀取問題
在SpringBoot應(yīng)用中,可以通過多種方式實現(xiàn)多個YAML配置文件的相互讀取和組合,SpringBoot2.4及以上版本可以使用spring.config.import屬性導(dǎo)入其他配置文件,@PropertySource注解雖不支持YAML2024-11-11Java 實戰(zhàn)項目基于遺傳算法學(xué)校排課系統(tǒng)的實現(xiàn)流程
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠遠不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+Springboot+Maven+mybatis+Vue+Mysql實現(xiàn)一個基于遺傳算法的學(xué)校排課系統(tǒng),大家可以在過程中查缺補漏,提升水平2021-11-11Java?Spring?AOP源碼解析之事務(wù)實現(xiàn)原理
這篇文章主要為大家介紹了Java?Spring?AOP事務(wù)實現(xiàn)原理,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-01-01