亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

java poi實(shí)現(xiàn)Excel多級(jí)表頭導(dǎo)出方式(多級(jí)表頭,復(fù)雜表頭)

 更新時(shí)間:2025年01月02日 11:07:02   作者:桃花面包  
文章介紹了使用javapoi庫(kù)實(shí)現(xiàn)Excel多級(jí)表頭導(dǎo)出的方法,通過主代碼、合并單元格、設(shè)置表頭單元格寬度、填充數(shù)據(jù)、web下載和提供表頭樣式、內(nèi)容樣式以及標(biāo)題樣式等步驟,作者實(shí)現(xiàn)了最終的效果

java poi實(shí)現(xiàn)Excel多級(jí)表頭導(dǎo)出(多級(jí)表頭,復(fù)雜表頭)

最近碰到一個(gè)導(dǎo)出,比較繁瑣,也查詢了許多博客,在其中一篇博客的基礎(chǔ)上修改,實(shí)現(xiàn)了最終想要的效果。

話不多說,直接上效果圖:

上代碼

1.主代碼

        
        List<Map<String, Object>> convertListMap = toListConvertListMap(tableTitleList, list);

        // 第一步,創(chuàng)建一個(gè)Workbook,對(duì)應(yīng)一個(gè)Excel文件
        XSSFWorkbook wb = new XSSFWorkbook();
        // 第二步,在Workbook中添加一個(gè)sheet,對(duì)應(yīng)Excel文件中的sheet
        XSSFSheet sheet = wb.createSheet("sheet");
        // 第三步,設(shè)置樣式以及字體樣式
        XSSFCellStyle titleStyle = createTitleCellStyle(wb);
        XSSFCellStyle headerStyle = createHeadCellStyle(wb);
        XSSFCellStyle contentStyle = createContentCellStyle(wb);
        // 行號(hào)
        int rowNum = 0;
       
        // 創(chuàng)建第一頁(yè)的第一行,索引從0開始
        XSSFRow row0 = sheet.createRow(rowNum++);
        row0.setHeight((short) 600);// 設(shè)置行高
        sheet.setColumnWidth(2, 8000);
        sheet.setColumnWidth(3, 8000);
        //第二行
        XSSFRow row2 = sheet.createRow(rowNum++);
        row2.setHeight((short) 700);
        // todo: 第三行
        XSSFRow row3 = sheet.createRow(rowNum++);
        row3.setHeight((short) 700);

        String[] row_one = null;
        String[] row_two = null;
        String[] row_three = null;
        int cellSize = 0;

        if (tableTitleList.size() == 2) {  // 全部
            row_one = new String[]{"序號(hào)", "所屬模塊", "單位名稱", "情況統(tǒng)計(jì)", "合計(jì)", "", "",
                    "測(cè)試類型A", "", "", "", "", "", "", "", "", "", "", "",
                    "測(cè)試類型B", "", "", "", "", "", "", "", "", "", "", "",};
            row_two = new String[]{"", "", "", "", "合計(jì)A", "合計(jì)B", "合計(jì)C",
                    "測(cè)試類型A1", "", "", "",
                    "測(cè)試類型A2", "", "", "",
                    "測(cè)試類型A3", "", "", "",
                    "測(cè)試類型B1", "", "", "",
                    "測(cè)試類型B2", "", "", "",
                    "測(cè)試類型B3", "", "", ""};

            row_three = new String[]{"", "", "", "", "", "", "",
                    "起床", "洗漱", "吃飯", "上班",
                    "起床", "洗漱", "吃飯", "上班",
                    "起床", "洗漱", "吃飯", "上班",

                    "起床", "洗漱", "吃飯", "上班",
                    "起床", "洗漱", "吃飯", "上班",
                    "起床", "洗漱", "吃飯", "上班",};
            // 合并單元格
            extracted(sheet);
        }

        // 創(chuàng)建第一行單元格
        for (int i = 0; i < row_one.length; i++) {
            XSSFCell c00 = row0.createCell(i);
            c00.setCellValue(row_one[i]);
            c00.setCellStyle(headerStyle);
        }
        // 創(chuàng)建第二行單元格
        for (int i = 0; i < row_two.length; i++) {
            XSSFCell tempCell = row2.createCell(i);
            tempCell.setCellValue(row_two[i]);
            tempCell.setCellStyle(headerStyle);
        }
        // 創(chuàng)建第三行單元格
        for (int i = 0; i < row_three.length; i++) {
            XSSFCell tempCell = row3.createCell(i);
            tempCell.setCellValue(row_three[i]);
            tempCell.setCellStyle(headerStyle);
        }


        // 設(shè)置表頭單元格的寬度
        tableTitleStyleColumnwidth(sheet);

        // todo: 填充數(shù)據(jù)
        approvalMethodfillInData(convertListMap, sheet, contentStyle, rowNum, row_three, tableTitleList);
//        approvalMethodfillInData(convertListMap, sheet, contentStyle, rowNum, cellSize, tableTitleList);
        //導(dǎo)出到瀏覽器下載
        buildExcelDocument("監(jiān)測(cè)數(shù)據(jù).xlsx", wb, response);

2.合并單元格

private static void extracted(XSSFSheet sheet) {
        // todo: 合并參數(shù)分別為: 起始行,結(jié)束行,起始列,結(jié)束列
        // 將第一列 的 第一行到第三行合并
        sheet.addMergedRegion(new CellRangeAddress(0, 2, 0, 0));
        // 將第二列的 第一行到第三行合并
        sheet.addMergedRegion(new CellRangeAddress(0, 2, 1, 1));
        //  將第三列的 第一行到第三行合并
        sheet.addMergedRegion(new CellRangeAddress(0, 2, 2, 2));
        sheet.addMergedRegion(new CellRangeAddress(0, 2, 3, 3));

        // 合計(jì)
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 4, 6));
        // 合計(jì)下的單元格
        sheet.addMergedRegion(new CellRangeAddress(1, 2, 4, 4));
        sheet.addMergedRegion(new CellRangeAddress(1, 2, 5, 5));
        sheet.addMergedRegion(new CellRangeAddress(1, 2, 6, 6));

       
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 7, 18));
     
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 19, 30));

        
        sheet.addMergedRegion(new CellRangeAddress(1, 1, 7, 10));
        sheet.addMergedRegion(new CellRangeAddress(1, 1, 11, 14));
        sheet.addMergedRegion(new CellRangeAddress(1, 1, 15, 18));

       
        sheet.addMergedRegion(new CellRangeAddress(1, 1, 19, 22));
        sheet.addMergedRegion(new CellRangeAddress(1, 1, 23, 26));
        sheet.addMergedRegion(new CellRangeAddress(1, 1, 27, 30));
    }

3.設(shè)置表頭單元格的寬度

private static void tableTitleStyleColumnwidth(XSSFSheet sheet) {

        sheet.setColumnWidth(2, 8000);
        sheet.setColumnWidth(3, 8000);

        sheet.setColumnWidth(7, 3000);
        sheet.setColumnWidth(8, 3000);
        sheet.setColumnWidth(9, 3000);
        sheet.setColumnWidth(10, 3000);

        sheet.setColumnWidth(11, 3000);
        sheet.setColumnWidth(12, 3000);
        sheet.setColumnWidth(13, 3000);
        sheet.setColumnWidth(14, 3000);

        sheet.setColumnWidth(15, 3000);
        sheet.setColumnWidth(16, 3000);
        sheet.setColumnWidth(17, 3000);
        sheet.setColumnWidth(18, 3000);

        sheet.setColumnWidth(19, 3000);
        sheet.setColumnWidth(20, 3000);
        sheet.setColumnWidth(21, 3000);
        sheet.setColumnWidth(22, 3000);

        sheet.setColumnWidth(23, 3000);
        sheet.setColumnWidth(24, 3000);
        sheet.setColumnWidth(25, 3000);
        sheet.setColumnWidth(26, 3000);

        sheet.setColumnWidth(27, 3000);
        sheet.setColumnWidth(28, 3000);
        sheet.setColumnWidth(29, 3000);
        sheet.setColumnWidth(30, 3000);
    }

4.填充數(shù)據(jù)

(注:我這里的數(shù)據(jù)格式是List<Map<String, Object>>類型,可以根據(jù)自己的實(shí)際情況來(lái)封裝數(shù)據(jù))

private static void approvalMethodfillInData(List<Map<String, Object>> convertListMap, XSSFSheet sheet, XSSFCellStyle contentStyle, int rowNum, String[] cellSize, List<Object> tableTitleList) {

        for (Map<String, Object> map : convertListMap) {
            XSSFRow tempRow = sheet.createRow(rowNum++);
            tempRow.setHeight((short) 500);

            // 判斷選擇類型
            if (tableTitleList.size() == 2 || "enterprise".equals(tableTitleList.get(0).toString())) {
                // 循環(huán)單元格填入數(shù)據(jù)
                for (int i = 0; i < cellSize.length; i++) {
                    //列寬自適應(yīng),j為自適應(yīng)的列,true就是自適應(yīng),false就是不自適應(yīng),默認(rèn)不自適應(yīng)
                    sheet.autoSizeColumn(i, true);
                    XSSFCell tempCell = tempRow.createCell(i);
                    tempCell.setCellStyle(contentStyle);
                    String tempValue = "";
                    switch (i) {
                        case 0:
                            tempValue = map.get("indexNum").toString();
                            break;
                        case 1:
                            tempValue = map.get("platName").toString();
                            break;
                        case 2:
                            tempValue = map.get("deptName").toString();
                            break;
                        case 3:
                            tempValue = map.get("type").toString();
                            break;
                        case 4:
                            tempValue = map.get("totalProNum").toString();
                            break;
                        case 5:
                            tempValue = map.get("totalBidNum").toString();
                            break;
                        case 6:
                            tempValue = map.get("totalAmount").toString();
                            break;
                        case 7:   
                            tempValue = map.get("enterpriseEngineeringProNum").toString();
                            break;
                        case 8:
                            tempValue = map.get("enterpriseEngineeringBidNum").toString();
                            break;
                        case 9:
                            tempValue = map.get("enterprisePurchaseAmount").toString();
                            break;
                        case 10:
                            tempValue = map.get("planEnterprisePurchaseAmount").toString();
                            break;
                        case 11:   
                            tempValue = map.get("enterprisePurchaseProNum").toString();
                            break;
                        case 12:
                            tempValue = map.get("enterprisePurchaseBidNum").toString();
                            break;
                        case 13:
                            tempValue = map.get("enterprisePurchaseAmount").toString();
                            break;
                        case 14:
                            tempValue = map.get("planEnterprisePurchaseAmount").toString();
                            break;
                        case 15:  
                            tempValue = map.get("enterpriseServiceProNum").toString();
                            break;
                        case 16:
                            tempValue = map.get("enterpriseServiceBidNum").toString();
                            break;
                        case 17:
                            tempValue = map.get("enterpriseServiceAmount").toString();
                            break;
                        case 18:
                            tempValue = map.get("planEnterpriseServiceAmount").toString();
                            break;

                        case 19:  
                            tempValue = map.get("groupEngineeringProNum").toString();
                            break;
                        case 20:
                            tempValue = map.get("grouppurchaseBidNum").toString();
                            break;
                        case 21:
                            tempValue = map.get("groupPurchaseAmount").toString();
                            break;
                        case 22:
                            tempValue = map.get("palnGroupPurchaseAmount").toString();
                            break;
                        case 23: 
                            tempValue = map.get("groupPurchaseProNum").toString();
                            break;
                        case 24:
                            tempValue = map.get("groupEngineeringBidNum").toString();
                            break;
                        case 25:
                            tempValue = map.get("groupEngineeringAmount").toString();
                            break;
                        case 26:
                            tempValue = map.get("planGroupEngineeringAmount").toString();
                            break;
                        case 27: 
                            tempValue = map.get("groupServiceProNum").toString();
                            break;
                        case 28:
                            tempValue = map.get("groupServiceBidNum").toString();
                            break;
                        case 29:
                            tempValue = map.get("groupServiceAmount").toString();
                            break;
                        case 30:
                            tempValue = map.get("planGroupServiceAmount").toString();
                            break;
                    }
                    tempCell.setCellValue(tempValue);
                }
            }
}

5.web下載,導(dǎo)出到瀏覽器

private static void buildExcelDocument(String fileName, Workbook wb, HttpServletResponse response) {
        try {
            response.setContentType("application/octet-stream");
            // 可自行定義編碼格式
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8"));
            //清除jsp編譯html文件的空白,防止excel出現(xiàn)空行
            response.flushBuffer();
            //寫出
            wb.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(wb);
        }
    }

6.提供表頭樣式,內(nèi)容樣式以及標(biāo)題樣式

 /**
     * 創(chuàng)建標(biāo)題樣式
     *
     * @param wb
     * @return
     */
    private static XSSFCellStyle createTitleCellStyle(XSSFWorkbook wb) {
        XSSFCellStyle cellStyle = wb.createCellStyle();
        cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直對(duì)齊
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
//        cellStyle.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex());//背景顏色

        XSSFFont headerFont1 = (XSSFFont) wb.createFont(); // 創(chuàng)建字體樣式
        headerFont1.setBold(true); //字體加粗
        headerFont1.setFontName("黑體"); // 設(shè)置字體類型
        headerFont1.setFontHeightInPoints((short) 15); // 設(shè)置字體大小
        cellStyle.setFont(headerFont1); // 為標(biāo)題樣式設(shè)置字體樣式
        return cellStyle;
    }

    /**
     * 創(chuàng)建表頭樣式
     *
     * @param wb
     * @return
     */
    private static XSSFCellStyle createHeadCellStyle(XSSFWorkbook wb) {
        XSSFCellStyle cellStyle = wb.createCellStyle();
        cellStyle.setWrapText(true);// 設(shè)置自動(dòng)換行
        cellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());//背景顏色
        cellStyle.setAlignment(HorizontalAlignment.CENTER); //水平居中
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); //垂直對(duì)齊
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
//        cellStyle.setBottomBorderColor(IndexedColors.BLACK.index);
        cellStyle.setBorderBottom(BorderStyle.THIN); //下邊框
        cellStyle.setBorderLeft(BorderStyle.THIN); //左邊框
        cellStyle.setBorderRight(BorderStyle.THIN); //右邊框
        cellStyle.setBorderTop(BorderStyle.THIN); //上邊框

        XSSFFont headerFont = (XSSFFont) wb.createFont(); // 創(chuàng)建字體樣式
        headerFont.setBold(true); //字體加粗
        headerFont.setFontName("黑體"); // 設(shè)置字體類型
        headerFont.setFontHeightInPoints((short) 12); // 設(shè)置字體大小
        cellStyle.setFont(headerFont); // 為標(biāo)題樣式設(shè)置字體樣式

        return cellStyle;
    }

    /**
     * 創(chuàng)建內(nèi)容樣式
     *
     * @param wb
     * @return
     */
    private static XSSFCellStyle createContentCellStyle(XSSFWorkbook wb) {
        XSSFCellStyle cellStyle = wb.createCellStyle();
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直居中
        cellStyle.setAlignment(HorizontalAlignment.CENTER);// 水平居中
        cellStyle.setWrapText(true);// 設(shè)置自動(dòng)換行
        cellStyle.setBorderBottom(BorderStyle.THIN); //下邊框
        cellStyle.setBorderLeft(BorderStyle.THIN); //左邊框
        cellStyle.setBorderRight(BorderStyle.THIN); //右邊框
        cellStyle.setBorderTop(BorderStyle.THIN); //上邊框

        // 生成12號(hào)字體
        XSSFFont font = wb.createFont();
        font.setColor((short) 8);
        font.setFontHeightInPoints((short) 12);
        cellStyle.setFont(font);

        return cellStyle;
    }

到此,大功告成!

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Spring Boot啟動(dòng)過程(四)之Spring Boot內(nèi)嵌Tomcat啟動(dòng)

    Spring Boot啟動(dòng)過程(四)之Spring Boot內(nèi)嵌Tomcat啟動(dòng)

    這篇文章主要介紹了Spring Boot啟動(dòng)過程(四)之Spring Boot內(nèi)嵌Tomcat啟動(dòng)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2017-04-04
  • 使用jd-gui反編譯修改jar包里的.class并重新生成新jar問題

    使用jd-gui反編譯修改jar包里的.class并重新生成新jar問題

    這篇文章主要介紹了使用jd-gui反編譯修改jar包里的.class并重新生成新jar問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • Spring AI內(nèi)置DeepSeek的詳細(xì)步驟

    Spring AI內(nèi)置DeepSeek的詳細(xì)步驟

    Spring AI 最新快照版已經(jīng)內(nèi)置 DeepSeek 了,所以以后項(xiàng)目中對(duì)接 DeepSeek 就方便多了,但因?yàn)榭煺瞻鏁?huì)有很多 Bug,所以今天咱們就來(lái)看穩(wěn)定版的 Spring AI 如何對(duì)接 DeepSeek 滿血版,感興趣的小伙伴跟著小編一起來(lái)看看吧
    2025-02-02
  • java實(shí)現(xiàn)圖片角度旋轉(zhuǎn)并獲得圖片信息

    java實(shí)現(xiàn)圖片角度旋轉(zhuǎn)并獲得圖片信息

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)圖片角度旋轉(zhuǎn)并獲得圖片信息,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-02-02
  • IDEA2020.1構(gòu)建Spring5.2.x源碼的方法

    IDEA2020.1構(gòu)建Spring5.2.x源碼的方法

    這篇文章主要介紹了IDEA2020.1構(gòu)建Spring5.2.x源碼的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • SpringBoot實(shí)現(xiàn)返回值數(shù)據(jù)脫敏的步驟詳解

    SpringBoot實(shí)現(xiàn)返回值數(shù)據(jù)脫敏的步驟詳解

    這篇文章主要給大家介紹一下SpringBoot實(shí)現(xiàn)返回值數(shù)據(jù)脫敏的步驟,文章通過代碼示例介紹的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下
    2023-07-07
  • Java文件操作之IO流 File類的使用詳解

    Java文件操作之IO流 File類的使用詳解

    在java中提供有對(duì)于文件操作系統(tǒng)的支持,這個(gè)支持在java.io.File類中進(jìn)行了定義,也就是說在整個(gè)java.io包中File類是唯一一個(gè)與文件本身操作有關(guān)的類(創(chuàng)建,刪除,重命名)有關(guān)的類,而如果想要進(jìn)行File類的操作,我們需要提供有完整的路徑支持,而后可以調(diào)用相應(yīng)的方法進(jìn)行處理
    2021-09-09
  • Java開發(fā)者必備10大數(shù)據(jù)工具和框架

    Java開發(fā)者必備10大數(shù)據(jù)工具和框架

    這篇文章主要為大家詳細(xì)介紹了Java開發(fā)者必備10大數(shù)據(jù)工具和框架,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • java 多線程死鎖詳解及簡(jiǎn)單實(shí)例

    java 多線程死鎖詳解及簡(jiǎn)單實(shí)例

    這篇文章主要介紹了java 多線程死鎖詳解及簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2017-01-01
  • 詳解springboot中的jar包部署步驟

    詳解springboot中的jar包部署步驟

    這篇文章主要介紹了springboot中的jar包部署步驟及l(fā)inux中部署項(xiàng)目常用指令,需要的朋友可以參考下
    2018-07-07

最新評(píng)論