二 Java之POI導出Excel:多個sheet

相信在大部分的web項目中都會有導出導入Excel的需求,之前我也寫過一篇導出單個sheet工作表的文章,沒看過的小伙伴可以去看哈,鏈接也給大家放出來了:導出單個sheet
但是在我們日常的工作中,需求往往沒這么簡單,可能需要將數據按類型分類導出或者數據量過大,需要分多張表導出等等 。遇到類似的需求該怎么辦呢,別慌,往下看 。


一、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>
二、工具類util1.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導出多個sheet工具類 * @author: *** * @date: 2022/9/15 */public class ExportSheetUtil {    /**     * 拆解并導出多重Excel     */    public static void exportManySheetExcel(String fileName, List<ExcelSheet> mysheets, HttpServletResponse response) {        //創建工作薄        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);                //往單元格里寫數據                cell.setCellValue(handers[i]);                //加樣式                cell.setCellStyle(style);                //設置每列的列寬                sheet.setColumnWidth(i, 4000);            }            //寫數據集            List<String[]> dataset = excel.getDataset();            for (int i = 0; i < dataset.size(); i++) {                //獲取該對象                String[] data = dataset.get(i);                //創建數據行                HSSFRow row = sheet.createRow(i + 1);                for (int j = 0; j < data.length; j++) {                    //設置對應單元格的值                    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();        }    }}

推薦閱讀