Java使用POI導(dǎo)出Excel(二):多個(gè)sheet
相關(guān)文章:
Java使用POI導(dǎo)出Excel(一):?jiǎn)蝧heet
Java使用POI導(dǎo)出Excel(二):多個(gè)sheet
相信在大部分的web項(xiàng)目中都會(huì)有導(dǎo)出導(dǎo)入Excel的需求,但是在我們?nèi)粘5墓ぷ髦?,需求往往沒(méi)這么簡(jiǎn)單,可能需要將數(shù)據(jù)按類(lèi)型分類(lèi)導(dǎo)出或者數(shù)據(jù)量過(guò)大,需要分多張表導(dǎo)出等等。遇到類(lèi)似的需求該怎么辦呢,別慌,往下看。
一、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>
二、工具類(lèi)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)出多個(gè)sheet工具類(lèi) * @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) { //新建一個(gè)sheet //獲取該sheet名稱 HSSFSheet sheet = wb.createSheet(excel.getFileName()); //獲取sheet的標(biāo)題名 String[] handers = excel.getHanders(); //第一個(gè)sheet的第一行為標(biāo)題 HSSFRow rowFirst = sheet.createRow(0); //寫(xiě)標(biāo)題 for (int i = 0; i < handers.length; i++) { //獲取第一行的每個(gè)單元格 HSSFCell cell = rowFirst.createCell(i); //往單元格里寫(xiě)數(shù)據(jù) cell.setCellValue(handers[i]); //加樣式 cell.setCellStyle(style); //設(shè)置每列的列寬 sheet.setColumnWidth(i, 4000); } //寫(xiě)數(shù)據(jù)集 List<String[]> dataset = excel.getDataset(); for (int i = 0; i < dataset.size(); i++) { //獲取該對(duì)象 String[] data = dataset.get(i); //創(chuàng)建數(shù)據(jù)行 HSSFRow row = sheet.createRow(i + 1); for (int j = 0; j < data.length; j++) { //設(shè)置對(duì)應(yīng)單元格的值 row.createCell(j).setCellValue(data[j]); } } } // 下載文件谷歌文件名會(huì)亂碼,用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)出多個(gè)sheet表 * @author: *** * @date: 2022/9/15 */ @Data public class ExcelSheet { /*** sheet的名稱*/ private String fileName; /*** sheet里的標(biāo)題*/ 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)出開(kāi)票及運(yùn)單信息*/ ExportInvoiceAndBillVo exportInvoiceAndBillInfo(InvoiceReviewListDto dto);
2.impl實(shí)現(xiàn)類(lèi)
實(shí)現(xiàn)類(lèi)里的代碼,需要各位根據(jù)自己的業(yè)務(wù)場(chǎng)景進(jìn)行改動(dòng),無(wú)非就是將需要導(dǎo)出的數(shù)據(jù)先查出來(lái),帶入模板中,調(diào)用工具類(lèi)的方法導(dǎo)出。
?package com.***.vo.invoicereview; import lombok.Data; import java.io.Serializable; import java.util.List; /** * @description: 導(dǎo)出開(kāi)票和運(yùn)單信息Vo * @author: *** * @date: 2022/9/19 */ @Data public class ExportInvoiceAndBillVo implements Serializable { /*** 開(kāi)票信息*/ private List<String[]> invoiceList; /*** 運(yùn)單信息*/ private List<String[]> billList; } ? @Override public ExportInvoiceAndBillVo exportInvoiceAndBillInfo(InvoiceReviewListDto dto) { ExportInvoiceAndBillVo invoiceAndBill = new ExportInvoiceAndBillVo(); // 查詢需要導(dǎo)出的開(kāi)票信息 PageInfo<InvoiceReviewListVo> pageInfo = queryInvoiceReviewList(dto); List<InvoiceReviewListVo> invoiceList = pageInfo.getList(); if (invoiceList.size() > 10000) { throw new ServiceException("開(kāi)票數(shù)據(jù)過(guò)多,請(qǐng)分批次導(dǎo)出"); } // 查詢需要導(dǎo)出的車(chē)運(yùn)運(yùn)單信息 List<Long> invoiceIdList = invoiceList.stream().map(InvoiceReviewListVo::getInvoiceId).collect(Collectors.toList()); List<ExportBillVo> billList = getBillInfo(invoiceIdList); if (billList.size() > 10000) { throw new ServiceException("運(yùn)單數(shù)據(jù)過(guò)多,請(qǐng)分批次導(dǎo)出"); } // 將表1 表2的數(shù)據(jù) 放入定義的對(duì)象Vo中 invoiceAndBill.setInvoiceList(getInvoiceDataList(invoiceList)); invoiceAndBill.setBillList(getBillDataList(billList)); return invoiceAndBill; }
3.controller層
controller層的代碼需要注意的是:
1.因?yàn)閷?dǎo)出Excel一般都是通過(guò)瀏覽器進(jìn)行下載的,所以入?yún)⒅行枰尤?strong>HttpServletResponse
2.調(diào)用封裝的工具類(lèi)ExportSheetUtil中的exportManySheetExcel方法就可以了
3.表頭和表名需要各位根據(jù)自身的業(yè)務(wù)場(chǎng)景修改哈
? /** * 導(dǎo)出開(kāi)票和運(yùn)單信息 */ @Log @PostMapping("/exportInvoiceAndBillInfo") public void exportInvoiceAndBillInfo(@RequestBody InvoiceReviewListDto dto, HttpServletResponse response) { ExportInvoiceAndBillVo invoiceAndBillVo = invoiceFacadeService.exportInvoiceAndBillInfo(dto); //設(shè)置sheet的表頭與表名 String[] invoiceSheetHead = {"開(kāi)票編號(hào)", "票號(hào)", "公司名稱", "收票方名稱", "結(jié)算類(lèi)型", "納稅識(shí)別碼", "收票聯(lián)系人", "聯(lián)系人電話", "運(yùn)單總金額(元)", "含稅總金額(元)", "開(kāi)票狀態(tài)", "提交開(kāi)票時(shí)間", "運(yùn)營(yíng)審核時(shí)間", "運(yùn)營(yíng)審核人", "財(cái)務(wù)審核時(shí)間", "財(cái)務(wù)審核人", "開(kāi)票完成時(shí)間", "沖銷(xiāo)操作人", "沖銷(xiāo)時(shí)間"}; String[] billSheetHead = {"開(kāi)票編號(hào)", "運(yùn)單號(hào)", "發(fā)貨地", "收貨地", "司機(jī)", "司機(jī)電話", "貨物名稱", "貨物數(shù)量", "單位", "貨物重量(噸)", "運(yùn)單狀態(tài)", "運(yùn)單金額(元)", "含稅金額(元)"}; ExcelSheet invoiceExcel = new ExcelSheet("開(kāi)票信息", invoiceSheetHead, invoiceAndBillVo.getInvoiceList()); ExcelSheet billExcel = new ExcelSheet("運(yùn)單信息", billSheetHead, invoiceAndBillVo.getBillList()); List<ExcelSheet> mysheet = new ArrayList<>(); mysheet.add(invoiceExcel); mysheet.add(billExcel); ExportSheetUtil.exportManySheetExcel("開(kāi)票及運(yùn)單信息", mysheet, response); }
最終導(dǎo)出的Excel文件:
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(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)代理、動(dòng)態(tài)代理以及CGLIB動(dòng)態(tài)代理總結(jié)
本篇文章主要介紹了JAVA中的靜態(tài)代理、動(dòng)態(tài)代理以及CGLIB動(dòng)態(tài)代理總結(jié),具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08Spring Boot自定義配置屬性源(PropertySource)
這篇文章主要介紹了Spring Boot自定義配置屬性源(PropertySource),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-06-06Spring?MVC?請(qǐng)求映射路徑的配置實(shí)現(xiàn)前后端交互
在Spring?MVC中,請(qǐng)求映射路徑是指與特定的請(qǐng)求處理方法關(guān)聯(lián)的URL路徑,這篇文章主要介紹了Spring?MVC?請(qǐng)求映射路徑的配置,實(shí)現(xiàn)前后端交互,需要的朋友可以參考下2023-09-09Java guava monitor監(jiān)視器線程的使用詳解
工作中的場(chǎng)景中是否存在類(lèi)似這樣的場(chǎng)景,需要提交的線程在某個(gè)觸發(fā)條件下執(zhí)行。本文主要就是使用guava中的monitor來(lái)優(yōu)雅的實(shí)現(xiàn)帶監(jiān)視器的線程2021-11-11Mybatis中如何設(shè)置sqlSession自動(dòng)提交
在MyBatis中,默認(rèn)情況下,獲取的SqlSession對(duì)象不會(huì)自動(dòng)提交事務(wù),這意味著在進(jìn)行更新、刪除或插入等操作后,需要顯式調(diào)用commit方法來(lái)提交事務(wù),但是,可以在獲取SqlSession時(shí)通過(guò)將openSession方法的參數(shù)設(shè)置為true2024-09-09Sentinel熱門(mén)詞匯限流的實(shí)現(xiàn)詳解
這篇文章主要介紹了使用Sentinel對(duì)熱門(mén)詞匯進(jìn)行限流的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07