Java實(shí)現(xiàn)導(dǎo)出word表格的示例詳解
目標(biāo)
多級(jí)表頭、分頁(yè)、動(dòng)態(tài)數(shù)據(jù)
實(shí)現(xiàn)
依賴(lài)
<!-- poi工具類(lèi)--> <dependency> <groupId>com.deepoove</groupId> <artifactId>poi-tl</artifactId> <version>1.12.0</version> </dependency>
模版
代碼
TableData數(shù)據(jù)(模版對(duì)應(yīng)的數(shù)據(jù)對(duì)象)
package org.example.bean; import com.deepoove.poi.data.TableRenderData; import lombok.Data; @Data public class TableData { /** * 標(biāo)題 */ private String title; /** * 表格 */ private TableRenderData table; private String[][] tableList; /** * 總價(jià) */ 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-報(bào)價(jià)明細(xì)表"); 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("項(xiàng)號(hào)", "編號(hào)", "種類(lèi)", "", "價(jià)格").center().create(); RowRenderData row1 = Rows.of("項(xiàng)號(hào)", "編號(hào)", "期貨", "股票", "價(jià)格").center().create(); int length = 0; if (tableData.getTableList() != null) { length = tableData.getTableList().length; } // 表格數(shù)據(jù) 加上2行表頭 再加上最后一行總價(jià) RowRenderData[] rowRenderData = new RowRenderData[length + 3]; rowRenderData[0] = row0; rowRenderData[1] = row1; // 計(jì)算價(jià)錢(qián) 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("總價(jià)", "", "", "", totalPrice.toString()).center().create(); rowRenderData[rowRenderData.length - 1] = row4; // 表格合并,根據(jù)坐標(biāo) 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-報(bào)價(jià)明細(xì)表.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實(shí)現(xiàn)導(dǎo)出word表格的示例詳解的文章就介紹到這了,更多相關(guān)Java導(dǎo)出word表格內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java中的異常處理(try,catch,finally,throw,throws)
本文主要介紹了Java中的異常處理,文章主要介紹的異常處理包括5個(gè)關(guān)鍵字try,catch,finally,throw,throws,更多詳細(xì)內(nèi)容需要的朋友可以參考一下2022-06-06springboot配置文件如何實(shí)現(xiàn)多個(gè)yml相互讀取問(wèn)題
在SpringBoot應(yīng)用中,可以通過(guò)多種方式實(shí)現(xiàn)多個(gè)YAML配置文件的相互讀取和組合,SpringBoot2.4及以上版本可以使用spring.config.import屬性導(dǎo)入其他配置文件,@PropertySource注解雖不支持YAML2024-11-11Java 實(shí)戰(zhàn)項(xiàng)目基于遺傳算法學(xué)校排課系統(tǒng)的實(shí)現(xiàn)流程
讀萬(wàn)卷書(shū)不如行萬(wàn)里路,只學(xué)書(shū)上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+Springboot+Maven+mybatis+Vue+Mysql實(shí)現(xiàn)一個(gè)基于遺傳算法的學(xué)校排課系統(tǒng),大家可以在過(guò)程中查缺補(bǔ)漏,提升水平2021-11-11Java?Spring?AOP源碼解析之事務(wù)實(shí)現(xiàn)原理
這篇文章主要為大家介紹了Java?Spring?AOP事務(wù)實(shí)現(xiàn)原理,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-01-01