Java使用POI導(dǎo)出Excel(二):多個sheet
相關(guān)文章:
Java使用POI導(dǎo)出Excel(一):單sheet
Java使用POI導(dǎo)出Excel(二):多個sheet
相信在大部分的web項目中都會有導(dǎo)出導(dǎo)入Excel的需求,但是在我們?nèi)粘5墓ぷ髦?,需求往往沒這么簡單,可能需要將數(shù)據(jù)按類型分類導(dǎo)出或者數(shù)據(jù)量過大,需要分多張表導(dǎo)出等等。遇到類似的需求該怎么辦呢,別慌,往下看。
一、pom引用
pom文件中,添加以下依賴
<!--Excel工具--> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>5.2.2</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>5.2.2</version> <scope>compile</scope> </dependency>
二、工具類util
1.ExportSheetUtil
?package com.***.excel; import org.apache.poi.hssf.usermodel.*; import org.apache.poi.ss.usermodel.HorizontalAlignment; import org.apache.poi.ss.usermodel.VerticalAlignment; import org.springframework.http.MediaType; import javax.servlet.http.HttpServletResponse; import java.net.URLEncoder; import java.util.List; /** * @description: excel導(dǎo)出多個sheet工具類 * @author: *** * @date: 2022/9/15 */ public class ExportSheetUtil { /** * 拆解并導(dǎo)出多重Excel */ public static void exportManySheetExcel(String fileName, List<ExcelSheet> mysheets, HttpServletResponse response) { //創(chuàng)建工作薄 HSSFWorkbook wb = new HSSFWorkbook(); //表頭樣式 HSSFCellStyle style = wb.createCellStyle(); // 垂直 style.setVerticalAlignment(VerticalAlignment.CENTER); // 水平 style.setAlignment(HorizontalAlignment.CENTER); //字體樣式 HSSFFont fontStyle = wb.createFont(); fontStyle.setFontName("微軟雅黑"); fontStyle.setFontHeightInPoints((short) 12); style.setFont(fontStyle); for (ExcelSheet excel : mysheets) { //新建一個sheet //獲取該sheet名稱 HSSFSheet sheet = wb.createSheet(excel.getFileName()); //獲取sheet的標題名 String[] handers = excel.getHanders(); //第一個sheet的第一行為標題 HSSFRow rowFirst = sheet.createRow(0); //寫標題 for (int i = 0; i < handers.length; i++) { //獲取第一行的每個單元格 HSSFCell cell = rowFirst.createCell(i); //往單元格里寫數(shù)據(jù) cell.setCellValue(handers[i]); //加樣式 cell.setCellStyle(style); //設(shè)置每列的列寬 sheet.setColumnWidth(i, 4000); } //寫數(shù)據(jù)集 List<String[]> dataset = excel.getDataset(); for (int i = 0; i < dataset.size(); i++) { //獲取該對象 String[] data = dataset.get(i); //創(chuàng)建數(shù)據(jù)行 HSSFRow row = sheet.createRow(i + 1); for (int j = 0; j < data.length; j++) { //設(shè)置對應(yīng)單元格的值 row.createCell(j).setCellValue(data[j]); } } } // 下載文件谷歌文件名會亂碼,用IE try { response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE); response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName + ".xlsx", "utf-8")); response.setHeader("Cache-Control", "No-cache"); response.flushBuffer(); wb.write(response.getOutputStream()); wb.close(); } catch (Exception e) { e.printStackTrace(); } } }
2.ExcelSheet
?package com.***.excel; import lombok.Data; import java.util.List; /** * @description: 導(dǎo)出多個sheet表 * @author: *** * @date: 2022/9/15 */ @Data public class ExcelSheet { /*** sheet的名稱*/ private String fileName; /*** sheet里的標題*/ private String[] handers; /*** sheet里的數(shù)據(jù)集*/ private List<String[]> dataset; public ExcelSheet(String fileName, String[] handers, List<String[]> dataset) { this.fileName = fileName; this.handers = handers; this.dataset = dataset; } }
三、相關(guān)業(yè)務(wù)代碼
1.service層
/*** 導(dǎo)出開票及運單信息*/ ExportInvoiceAndBillVo exportInvoiceAndBillInfo(InvoiceReviewListDto dto);
2.impl實現(xiàn)類
實現(xiàn)類里的代碼,需要各位根據(jù)自己的業(yè)務(wù)場景進行改動,無非就是將需要導(dǎo)出的數(shù)據(jù)先查出來,帶入模板中,調(diào)用工具類的方法導(dǎo)出。
?package com.***.vo.invoicereview; import lombok.Data; import java.io.Serializable; import java.util.List; /** * @description: 導(dǎo)出開票和運單信息Vo * @author: *** * @date: 2022/9/19 */ @Data public class ExportInvoiceAndBillVo implements Serializable { /*** 開票信息*/ private List<String[]> invoiceList; /*** 運單信息*/ private List<String[]> billList; } ? @Override public ExportInvoiceAndBillVo exportInvoiceAndBillInfo(InvoiceReviewListDto dto) { ExportInvoiceAndBillVo invoiceAndBill = new ExportInvoiceAndBillVo(); // 查詢需要導(dǎo)出的開票信息 PageInfo<InvoiceReviewListVo> pageInfo = queryInvoiceReviewList(dto); List<InvoiceReviewListVo> invoiceList = pageInfo.getList(); if (invoiceList.size() > 10000) { throw new ServiceException("開票數(shù)據(jù)過多,請分批次導(dǎo)出"); } // 查詢需要導(dǎo)出的車運運單信息 List<Long> invoiceIdList = invoiceList.stream().map(InvoiceReviewListVo::getInvoiceId).collect(Collectors.toList()); List<ExportBillVo> billList = getBillInfo(invoiceIdList); if (billList.size() > 10000) { throw new ServiceException("運單數(shù)據(jù)過多,請分批次導(dǎo)出"); } // 將表1 表2的數(shù)據(jù) 放入定義的對象Vo中 invoiceAndBill.setInvoiceList(getInvoiceDataList(invoiceList)); invoiceAndBill.setBillList(getBillDataList(billList)); return invoiceAndBill; }
3.controller層
controller層的代碼需要注意的是:
1.因為導(dǎo)出Excel一般都是通過瀏覽器進行下載的,所以入?yún)⒅行枰尤?strong>HttpServletResponse
2.調(diào)用封裝的工具類ExportSheetUtil中的exportManySheetExcel方法就可以了
3.表頭和表名需要各位根據(jù)自身的業(yè)務(wù)場景修改哈
? /** * 導(dǎo)出開票和運單信息 */ @Log @PostMapping("/exportInvoiceAndBillInfo") public void exportInvoiceAndBillInfo(@RequestBody InvoiceReviewListDto dto, HttpServletResponse response) { ExportInvoiceAndBillVo invoiceAndBillVo = invoiceFacadeService.exportInvoiceAndBillInfo(dto); //設(shè)置sheet的表頭與表名 String[] invoiceSheetHead = {"開票編號", "票號", "公司名稱", "收票方名稱", "結(jié)算類型", "納稅識別碼", "收票聯(lián)系人", "聯(lián)系人電話", "運單總金額(元)", "含稅總金額(元)", "開票狀態(tài)", "提交開票時間", "運營審核時間", "運營審核人", "財務(wù)審核時間", "財務(wù)審核人", "開票完成時間", "沖銷操作人", "沖銷時間"}; String[] billSheetHead = {"開票編號", "運單號", "發(fā)貨地", "收貨地", "司機", "司機電話", "貨物名稱", "貨物數(shù)量", "單位", "貨物重量(噸)", "運單狀態(tài)", "運單金額(元)", "含稅金額(元)"}; ExcelSheet invoiceExcel = new ExcelSheet("開票信息", invoiceSheetHead, invoiceAndBillVo.getInvoiceList()); ExcelSheet billExcel = new ExcelSheet("運單信息", billSheetHead, invoiceAndBillVo.getBillList()); List<ExcelSheet> mysheet = new ArrayList<>(); mysheet.add(invoiceExcel); mysheet.add(billExcel); ExportSheetUtil.exportManySheetExcel("開票及運單信息", mysheet, response); }
最終導(dǎo)出的Excel文件:
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
相關(guān)文章
淺談Spring Cloud Netflix-Ribbon灰度方案之Zuul網(wǎng)關(guān)灰度
這篇文章主要介紹了淺談Spring Cloud Netflix-Ribbon灰度方案之Zuul網(wǎng)關(guān)灰度,想了解Ribbon灰度的同學(xué)可以參考下2021-04-04JAVA中的靜態(tài)代理、動態(tài)代理以及CGLIB動態(tài)代理總結(jié)
本篇文章主要介紹了JAVA中的靜態(tài)代理、動態(tài)代理以及CGLIB動態(tài)代理總結(jié),具有一定的參考價值,有興趣的可以了解一下2017-08-08Spring Boot自定義配置屬性源(PropertySource)
這篇文章主要介紹了Spring Boot自定義配置屬性源(PropertySource),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06Spring?MVC?請求映射路徑的配置實現(xiàn)前后端交互
在Spring?MVC中,請求映射路徑是指與特定的請求處理方法關(guān)聯(lián)的URL路徑,這篇文章主要介紹了Spring?MVC?請求映射路徑的配置,實現(xiàn)前后端交互,需要的朋友可以參考下2023-09-09Java guava monitor監(jiān)視器線程的使用詳解
工作中的場景中是否存在類似這樣的場景,需要提交的線程在某個觸發(fā)條件下執(zhí)行。本文主要就是使用guava中的monitor來優(yōu)雅的實現(xiàn)帶監(jiān)視器的線程2021-11-11Mybatis中如何設(shè)置sqlSession自動提交
在MyBatis中,默認情況下,獲取的SqlSession對象不會自動提交事務(wù),這意味著在進行更新、刪除或插入等操作后,需要顯式調(diào)用commit方法來提交事務(wù),但是,可以在獲取SqlSession時通過將openSession方法的參數(shù)設(shè)置為true2024-09-09