国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

POI實(shí)現(xiàn)導(dǎo)出復(fù)雜Excel(動(dòng)態(tài)行,復(fù)雜單元格,水印,Excel轉(zhuǎn)換為PDF)。

這篇具有很好參考價(jià)值的文章主要介紹了POI實(shí)現(xiàn)導(dǎo)出復(fù)雜Excel(動(dòng)態(tài)行,復(fù)雜單元格,水印,Excel轉(zhuǎn)換為PDF)。。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-457651.html

一、POI 表格框架

1.POI :?POI提供API給Java程序?qū)icrosoft Office格式檔案讀和寫(xiě)的功能

2.HSSF:Horrible SpreadSheet Format,為讀取操作提供了兩類API:usermodel和eventusermodel,即“用戶模型”和“事件-用戶模型”

3.POI 文檔結(jié)構(gòu)類

?HSSFWorkbook 文檔對(duì)象,HSSFSheet? 頁(yè),HSSFRow 行,HSSFCell 單元格,HSSFFont 字體,?HSSFName 名稱,HSSFDataFormat 日期格式

?HSSFHeader 表頭,HSSFFooter 表尾,HSSFCellStyle 單元格樣式,HSSFDateUtil 日期,HSSFPrintSetup 打印,?HSSFErrorConstants 錯(cuò)誤信息表

二、POI? 文件類型

? ? ? ???類??? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??文件? ??? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ???jar

? ? ?HSSF -- 提供讀寫(xiě)Microsoft? Excel? XLS格式檔案的功能? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?poi
  XSSF -- 提供讀寫(xiě)Microsoft? Excel? OOXML? XLSX格式檔案的功能? ? ? ? ? ? ? poi--ooxml
  HWPF -- 提供讀寫(xiě)Microsoft? Word? DOC格式檔案的功能        poi-scratchpad
  HSLF -- 提供讀寫(xiě)Microsoft? PowerPoint格式檔案的功能        ?poi-scratchpad
  HDGF -- 提供讀Microsoft? Visio格式檔案的功能            poi-scratchpad
  HPBF -- 提供讀Microsoft? Publisher格式檔案的功能          poi-scratchpad
  HSMF -- 提供讀Microsoft? Outlook格式檔案的功能          ??poi-scratchpad

?原Excel模板? ?脫產(chǎn)培訓(xùn)與網(wǎng)絡(luò)培訓(xùn)兩個(gè)列表不相同,并且為動(dòng)態(tài),數(shù)據(jù)條數(shù)未知,所以要?jiǎng)討B(tài)添加數(shù)據(jù)與動(dòng)態(tài)合并單元格。

?POI實(shí)現(xiàn)導(dǎo)出復(fù)雜Excel(動(dòng)態(tài)行,復(fù)雜單元格,水印,Excel轉(zhuǎn)換為PDF)。

需求如此,多以拋棄使用模板填充方式,改為從第一行構(gòu)建到最后一行,并且將excel添加水印轉(zhuǎn)換成pdf。

POI? Jar包? 版本之間差異看官網(wǎng),盡量用新的

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.15</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.15</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>3.15</version>
        </dependency>

PDF轉(zhuǎn)換Jar包 了解工具直接搜索jar包名稱就行

        <dependency>
            <groupId>com.bc.ext</groupId>
            <artifactId>spire.xls.free</artifactId>
            <version>5.1.0</version>
        </dependency>

業(yè)務(wù)代碼,具體行都有注釋。

public File createExcel(HashMap<String, Object> resultMap, List<Map<String, Object>> actualList, List<Map<String, Object>> netWorkList, HttpServletResponse response){
        /** 第一步,創(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("sheet1");

        //設(shè)置每個(gè)列有多寬  100為單位好計(jì)算
        sheet.setColumnWidth(0,100 * 12);
        sheet.setColumnWidth(1,100 * 19);
        sheet.setColumnWidth(2,100 * 38);
        sheet.setColumnWidth(3,100 * 38);
        sheet.setColumnWidth(4,100 * 38);
        sheet.setColumnWidth(5,100 * 38);
        sheet.setColumnWidth(6,100 * 38);

        /** 第三步,設(shè)置樣式以及字體樣式*/
        XSSFCellStyle titleStyle = createTitleCellStyle(wb);
        XSSFCellStyle headerStyle = createHeadCellStyle(wb);
        XSSFCellStyle contentStyle = createContentCellStyle(wb);

        /** 第四步,創(chuàng)建標(biāo)題 ,合并標(biāo)題單元格 */

        // 行號(hào)
        int rowNum = 0;

        // 創(chuàng)建第一頁(yè)的第一行,索引從0開(kāi)始
        XSSFRow row0 = sheet.createRow(rowNum++);
        row0.setHeight((short) 800);// 設(shè)置行高
        String title = "公務(wù)員培訓(xùn)情況備案表";
        XSSFCell c00 = row0.createCell(0);
        c00.setCellValue(title);
        c00.setCellStyle(titleStyle);
        // 合并單元格,參數(shù)依次為起始行,結(jié)束行,起始列,結(jié)束列 (索引0開(kāi)始)
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));//標(biāo)題合并單元格操作,6為總列數(shù)


        // 第二行
        XSSFRow row1 = sheet.createRow(rowNum++);
        row1.setHeight((short) 700);
        String[] row_first = {"年度","", "", "", "", "", ""};

        for (int i = 0; i < row_first.length; i++) {
            XSSFCell tempCell = row1.createCell(i);
            tempCell.setCellStyle(headerStyle);
            if (i == 0) {
                tempCell.setCellValue(Convert.toStr(resultMap.get("year")) + row_first[i]);
            }else {
                tempCell.setCellValue(row_first[i]);
            }
        }

        // 合并單元格,參數(shù)依次為起始行,結(jié)束行,起始列,結(jié)束列 (索引0開(kāi)始)
        sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 6));//標(biāo)題合并單元格操作,7為總列數(shù)

        //第三行
        XSSFRow row2 = sheet.createRow(rowNum++);
        row2.setHeight((short) 700);
        String[] row_2_str = {"姓名","", Convert.toStr(resultMap.get("name")), "性別", Convert.toStr(resultMap.get("sex")), "政治面貌", Convert.toStr(resultMap.get("politicVal"))};
        for (int i = 0; i < row_2_str.length; i++) {
            XSSFCell tempCell = row2.createCell(i);
            tempCell.setCellStyle(headerStyle);
            tempCell.setCellValue(row_2_str[i]);
        }

        // 合并單元格,參數(shù)依次為起始行,結(jié)束行,起始列,結(jié)束列 (索引0開(kāi)始)
        sheet.addMergedRegion(new CellRangeAddress(2, 2, 0, 1));//標(biāo)題合并單元格操作,7為總列數(shù)


        //第四行
        XSSFRow row3 = sheet.createRow(rowNum++);
        row3.setHeight((short) 700);
        String[] row_3_str = {"學(xué)歷","",Convert.toStr(resultMap.get("educationVal")), "行政級(jí)別",Convert.toStr(resultMap.get("administrationVal")), "",""};
        for (int i = 0; i < row_3_str.length; i++) {
            XSSFCell tempCell = row3.createCell(i);
            tempCell.setCellStyle(headerStyle);
            tempCell.setCellValue(row_3_str[i]);
        }

        // 合并單元格,參數(shù)依次為起始行,結(jié)束行,起始列,結(jié)束列 (索引0開(kāi)始)
        sheet.addMergedRegion(new CellRangeAddress(3, 3, 0, 1));//標(biāo)題合并單元格操作,7為總列數(shù)
        sheet.addMergedRegion(new CellRangeAddress(3, 3, 4, 6));//標(biāo)題合并單元格操作,7為總列數(shù)

        //第五行
        XSSFRow row4 = sheet.createRow(rowNum++);
        row4.setHeight((short) 700);
        String[] row_4_str = {"單位名稱","",Convert.toStr(resultMap.get("companyName")), "內(nèi)設(shè)機(jī)構(gòu)",Convert.toStr(resultMap.get("mechanism")), "職務(wù)",Convert.toStr(resultMap.get("studentDuties"))};
        for (int i = 0; i < row_4_str.length; i++) {
            XSSFCell tempCell = row4.createCell(i);
            tempCell.setCellStyle(headerStyle);
            tempCell.setCellValue(row_4_str[i]);
        }

        // 合并單元格,參數(shù)依次為起始行,結(jié)束行,起始列,結(jié)束列 (索引0開(kāi)始)
        sheet.addMergedRegion(new CellRangeAddress(4, 4, 0, 1));//標(biāo)題合并單元格操作,7為總列數(shù)

        //第六行
        XSSFRow row5 = sheet.createRow(rowNum++);
        row5.setHeight((short) 700);
        String[] row_5_str = {"脫產(chǎn)培訓(xùn)","培訓(xùn)班名稱","", "培訓(xùn)時(shí)間","", "主辦單位","學(xué)分"};
        for (int i = 0; i < row_5_str.length; i++) {
            XSSFCell tempCell = row5.createCell(i);
            tempCell.setCellStyle(headerStyle);
            tempCell.setCellValue(row_5_str[i]);
        }

        // 合并單元格,參數(shù)依次為起始行,結(jié)束行,起始列,結(jié)束列 (索引0開(kāi)始)
        sheet.addMergedRegion(new CellRangeAddress(5, 5, 1, 2));//標(biāo)題合并單元格操作,7為總列數(shù)
        sheet.addMergedRegion(new CellRangeAddress(5, 5, 3, 4));//標(biāo)題合并單元格操作,7為總列數(shù)


        //第六行帶的數(shù)據(jù)  循環(huán)插入

        for (Map<String, Object> actuaLData : actualList) {
            XSSFRow tempRow = sheet.createRow(rowNum++);
            tempRow.setHeight((short) 700);
            // 循環(huán)單元格填入數(shù)據(jù)
            for (int j = 0; j < 7; j++) {
                XSSFCell tempCell = tempRow.createCell(j);
                tempCell.setCellStyle(contentStyle);
                String tempValue;
                if (j == 0) {
                    tempValue = "";
                } else if (j == 1) {
                    tempValue = Convert.toStr(actuaLData.get("actualName"));
                } else if (j == 2) {
                    tempValue = "";
                } else if (j == 3) {
                    tempValue = Convert.toStr(actuaLData.get("actualTime"));
                } else if (j == 4) {
                    tempValue = "";
                } else if (j == 5) {
                    tempValue = Convert.toStr(actuaLData.get("actualOrgan"));
                } else {
                    tempValue = Convert.toStr(actuaLData.get("actualPoint"));
                }
                tempCell.setCellValue(tempValue);
            }
            sheet.addMergedRegion(new CellRangeAddress(rowNum - 1, rowNum - 1, 1, 2));//標(biāo)題合并單元格操作,7為總列數(shù)
            sheet.addMergedRegion(new CellRangeAddress(rowNum - 1, rowNum - 1, 3, 4));//標(biāo)題合并單元格操作,7為總列數(shù)
        }



        //培訓(xùn)班小計(jì)行
        XSSFRow rowxj = sheet.createRow(rowNum++);
        rowxj.setHeight((short) 700);
        String[] row_xj_str = {"","小計(jì)","", "","", "",Convert.toStr(resultMap.get("totalActualPoint"))};
        for (int i = 0; i < row_5_str.length; i++) {
            XSSFCell tempCell = rowxj.createCell(i);
            tempCell.setCellStyle(headerStyle);
            tempCell.setCellValue(row_xj_str[i]);
        }

        // 合并單元格,參數(shù)依次為起始行,結(jié)束行,起始列,結(jié)束列 (索引0開(kāi)始)
        //此處沒(méi)有任何問(wèn)題,程序不讓運(yùn)行,就給捕獲了,不用操作別的,之后的代碼也是一樣
        try {
            sheet.addMergedRegion(new CellRangeAddress(rowNum-1, rowNum - 1, 1, 5));//標(biāo)題合并單元格操作,7為總列數(shù)
            sheet.addMergedRegion(new CellRangeAddress(5, rowNum - 1, 0, 0));//標(biāo)題合并單元格操作,7為總列數(shù)
        } catch (Exception e){

        }

        //網(wǎng)絡(luò)培訓(xùn)標(biāo)題行
        XSSFRow rowNetworkTitle = sheet.createRow(rowNum++);
        rowNetworkTitle.setHeight((short) 700);
        String[] row_networkTitle_str = {"網(wǎng)絡(luò)培訓(xùn)","所學(xué)課程類別","", "","", "課程數(shù)","學(xué)分"};
        for (int i = 0; i < row_networkTitle_str.length; i++) {
            XSSFCell tempCell = rowNetworkTitle.createCell(i);
            tempCell.setCellStyle(headerStyle);
            tempCell.setCellValue(row_networkTitle_str[i]);
        }

        try {
            // 合并單元格,參數(shù)依次為起始行,結(jié)束行,起始列,結(jié)束列 (索引0開(kāi)始)
            sheet.addMergedRegion(new CellRangeAddress(rowNum - 1, rowNum - 1, 1, 4));//標(biāo)題合并單元格操作,7為總列數(shù)
        } catch (Exception e){

        }

        //做標(biāo)記,動(dòng)態(tài)計(jì)算坐標(biāo)合并
        int startIndex = rowNum - 1;
        //網(wǎng)絡(luò)培訓(xùn)list數(shù)據(jù)填充
        for (Map<String, Object> networkData : netWorkList) {
            XSSFRow tempRow = sheet.createRow(rowNum++);
            tempRow.setHeight((short) 700);
            // 循環(huán)單元格填入數(shù)據(jù)
            for (int j = 0; j < 7; j++) {
                XSSFCell tempCell = tempRow.createCell(j);
                tempCell.setCellStyle(contentStyle);
                String tempValue;
                if (j == 0) {
                    tempValue = "";
                } else if (j == 1) {
                    tempValue = Convert.toStr(networkData.get("name"));
                } else if (j == 2) {
                    tempValue = "";
                } else if (j == 3) {
                    tempValue = "";
                } else if (j == 4) {
                    tempValue = "";
                } else if (j == 5) {
                    tempValue = Convert.toStr(networkData.get("networkCount"));
                } else {
                    tempValue = Convert.toStr(networkData.get("point"));
                }
                tempCell.setCellValue(tempValue);
            }
            sheet.addMergedRegion(new CellRangeAddress(rowNum - 1, rowNum - 1, 1, 4));//標(biāo)題合并單元格操作,7為總列數(shù)
        }

        //網(wǎng)絡(luò)培訓(xùn)專題班行
        XSSFRow rowNetworkZtb = sheet.createRow(rowNum++);
        rowNetworkZtb.setHeight((short) 700);
        String[] row_NetworkZtb_str = {"","專題班","", "","", Convert.toStr(resultMap.get("lessonCount")),Convert.toStr(resultMap.get("pointSum"))};
        for (int i = 0; i < row_NetworkZtb_str.length; i++) {
            XSSFCell tempCell = rowNetworkZtb.createCell(i);
            tempCell.setCellStyle(headerStyle);
            tempCell.setCellValue(row_NetworkZtb_str[i]);
        }
        //合并
        try {
            sheet.addMergedRegion(new CellRangeAddress(rowNum-1, rowNum - 1, 1, 4));//標(biāo)題合并單元格操作,7為總列數(shù)
        } catch (Exception e){

        }

        // 合并單元格,參數(shù)依次為起始行,結(jié)束行,起始列,結(jié)束列 (索引0開(kāi)始)
        //此處沒(méi)有任何問(wèn)題,程序不讓運(yùn)行,就給捕獲了,不用操作別的,之后的代碼也是一樣

        //網(wǎng)絡(luò)培訓(xùn)小計(jì)行
        XSSFRow rowNetworkXj = sheet.createRow(rowNum++);
        rowNetworkXj.setHeight((short) 700);
        String[] row_NetworkXj_str = {"","小計(jì)","", "","", Convert.toStr(resultMap.get("networkCount")),Convert.toStr(resultMap.get("networkPoint"))};
        for (int i = 0; i < row_NetworkXj_str.length; i++) {
            XSSFCell tempCell = rowNetworkXj.createCell(i);
            tempCell.setCellStyle(headerStyle);
            tempCell.setCellValue(row_NetworkXj_str[i]);
        }

        // 合并單元格,參數(shù)依次為起始行,結(jié)束行,起始列,結(jié)束列 (索引0開(kāi)始)
        //此處沒(méi)有任何問(wèn)題,程序不讓運(yùn)行,就給捕獲了,不用操作別的,之后的代碼也是一樣
        try {
            sheet.addMergedRegion(new CellRangeAddress(rowNum-1, rowNum - 1, 1, 4));//標(biāo)題合并單元格操作,7為總列數(shù)
            sheet.addMergedRegion(new CellRangeAddress(startIndex, rowNum - 1, 0, 0));//標(biāo)題合并單元格操作,7為總列數(shù)
        } catch (Exception e){

        }

        //年度完成專題班行
        XSSFRow rowYearNetwork = sheet.createRow(rowNum++);
        rowYearNetwork.setHeight((short) 700);
        String yearNetwork =  Convert.toStr(resultMap.get("yearNetwork"));
        String[] row_YearNetwork_str;
        if (yearNetwork == null){
            row_YearNetwork_str = new String[]{"年度完成專題班", "", "", "", "", "個(gè)", ""};
        }else {
           row_YearNetwork_str = new String[]{"年度完成專題班", "", "", "", "", Convert.toStr(resultMap.get("yearNetworkCount")) + "個(gè)", ""};
        }
        for (int i = 0; i < row_YearNetwork_str.length; i++) {
            XSSFCell tempCell = rowYearNetwork.createCell(i);
            tempCell.setCellStyle(headerStyle);
            tempCell.setCellValue(row_YearNetwork_str[i]);
        }

        // 合并單元格,參數(shù)依次為起始行,結(jié)束行,起始列,結(jié)束列 (索引0開(kāi)始)
        //此處沒(méi)有任何問(wèn)題,程序不讓運(yùn)行,就給捕獲了,不用操作別的,之后的代碼也是一樣
        try {
            sheet.addMergedRegion(new CellRangeAddress(rowNum-1, rowNum - 1, 0, 4));//標(biāo)題合并單元格操作,7為總列數(shù)
            sheet.addMergedRegion(new CellRangeAddress(rowNum-1, rowNum - 1, 5, 6));//標(biāo)題合并單元格操作,7為總列數(shù)
        } catch (Exception e){

        }

        //學(xué)分合計(jì)行
        XSSFRow rowPointCount = sheet.createRow(rowNum++);
        rowPointCount.setHeight((short) 700);
        String[] row_PointCount_str = new String[]{"學(xué)分合計(jì)", "", "", "", "", Convert.toStr(resultMap.get("yearPoint")), ""};
        for (int i = 0; i < row_PointCount_str.length; i++) {
            XSSFCell tempCell = rowPointCount.createCell(i);
            tempCell.setCellStyle(headerStyle);
            tempCell.setCellValue(row_PointCount_str[i]);
        }

        // 合并單元格,參數(shù)依次為起始行,結(jié)束行,起始列,結(jié)束列 (索引0開(kāi)始)
        //此處沒(méi)有任何問(wèn)題,程序不讓運(yùn)行,就給捕獲了,不用操作別的,之后的代碼也是一樣
        try {
            sheet.addMergedRegion(new CellRangeAddress(rowNum-1, rowNum - 1, 0, 4));//標(biāo)題合并單元格操作,7為總列數(shù)
            sheet.addMergedRegion(new CellRangeAddress(rowNum-1, rowNum - 1, 5, 6));//標(biāo)題合并單元格操作,7為總列數(shù)
        } catch (Exception e){

        }

        //excel底部?jī)尚?        XSSFRow rowUnder2 = sheet.createRow(rowNum++);
        rowUnder2.setHeight((short) 900);
        String[] row_Under2_str = new String[]{"主管部門意見(jiàn)", "", "", Convert.toStr(resultMap.get("is")), "", "", ""};
        for (int i = 0; i < row_Under2_str.length; i++) {
            XSSFCell tempCell = rowUnder2.createCell(i);
            tempCell.setCellStyle(headerStyle);
            tempCell.setCellValue(row_Under2_str[i]);
        }

        try {
            sheet.addMergedRegion(new CellRangeAddress(rowNum-1, rowNum - 1, 3, 6));//標(biāo)題合并單元格操作,7為總列數(shù)
        } catch (Exception e){

        }

        XSSFRow rowUnder1 = sheet.createRow(rowNum++);
        rowUnder1.setHeight((short) 800);
        String[] row_Under1_str = new String[]{"", "", "", "", "", Convert.toStr(resultMap.get("pdfTime")), ""};
        for (int i = 0; i < row_Under1_str.length; i++) {
            XSSFCell tempCell = rowUnder1.createCell(i);
            tempCell.setCellStyle(headerStyle);
            tempCell.setCellValue(row_Under1_str[i]);
        }

        // 合并單元格,參數(shù)依次為起始行,結(jié)束行,起始列,結(jié)束列 (索引0開(kāi)始)
        //此處沒(méi)有任何問(wèn)題,程序不讓運(yùn)行,就給捕獲了,不用操作別的,之后的代碼也是一樣
        try {
            sheet.addMergedRegion(new CellRangeAddress(rowNum-1, rowNum - 1, 3, 4));//標(biāo)題合并單元格操作,7為總列數(shù)
            sheet.addMergedRegion(new CellRangeAddress(rowNum-1, rowNum - 1, 5, 6));//標(biāo)題合并單元格操作,7為總列數(shù)
            sheet.addMergedRegion(new CellRangeAddress(rowNum-2, rowNum - 1, 0, 2));//標(biāo)題合并單元格操作,7為總列數(shù)
        } catch (Exception e){

        }

        //備注行
        XSSFRow rowremarks = sheet.createRow(rowNum++);
        rowremarks.setHeight((short) 1300);
        String[] row_remarks_str = new String[]{"注:根據(jù)《干部教育培訓(xùn)工作條例》,干部參加教育培訓(xùn),每年累計(jì)不少于12天或者90學(xué)時(shí),因故未按規(guī)定參加教育培訓(xùn)或者未達(dá)到教育培訓(xùn)要求的,應(yīng)當(dāng)及時(shí)補(bǔ)訓(xùn)。干部教育培訓(xùn)考核不合格的,年度考核不得確定為優(yōu)秀等次。", "", "", "", "","", ""};
        for (int i = 0; i < row_remarks_str.length; i++) {
            XSSFCell tempCell = rowremarks.createCell(i);
            tempCell.setCellStyle(headerStyle);
            tempCell.setCellValue(row_remarks_str[i]);
        }

        // 合并單元格,參數(shù)依次為起始行,結(jié)束行,起始列,結(jié)束列 (索引0開(kāi)始)
        //此處沒(méi)有任何問(wèn)題,程序不讓運(yùn)行,就給捕獲了,不用操作別的,之后的代碼也是一樣
        try {
            sheet.addMergedRegion(new CellRangeAddress(rowNum-1, rowNum - 1, 0, 6));//標(biāo)題合并單元格操作,7為總列數(shù)
        } catch (Exception e){

        }

        try {
            //添加圖片水印
            FileOutputStream fileOut = null;
            BufferedImage bufferImg = null;

            ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
            //加載圖片
            bufferImg = ImageIO.read(new File("src/main/resources/static/template/peqk.png"));


            ImageIO.write(bufferImg, "png", byteArrayOut);

            XSSFDrawing patriarch = sheet.createDrawingPatriarch();
            XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0,5, rowNum-3, 7,rowNum-1 );
            //插入圖片 1
            patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), XSSFWorkbook.PICTURE_TYPE_PNG));

//            // 輸出文件
//            wb.write(fileOut);

        } catch (Exception e){
            e.printStackTrace();
        }

        File fileExcel = xssfWorkbookToFile(wb, "src/main/resources/static/template/peqk.xlsx");

        ExcelToPdf excelToPdf = new ExcelToPdf();
        excelToPdf.sheetToPdf("src/main/resources/static/template/peqk.xlsx", "src/main/resources/static/template/peqk.pdf");
        File filePdf = new File("src/main/resources/static/template/peqk.pdf");



        ServletOutputStream out = null;
        try{

            out = response.getOutputStream();

            /** 導(dǎo)出pdf文件流 */
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/pdf");
            response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "inline; filename="+ URLEncoder.encode("公務(wù)員培訓(xùn)情況備案表.pdf","UTF-8"));


            FileInputStream inputStream = new FileInputStream(filePdf);
            // 讀取文件流
            int len = 0;
            byte[] buffer = new byte[1024 * 10];
            while ((len = inputStream.read(buffer)) != -1) {
                out.write(buffer, 0, len);
            }
            out.close();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            fileExcel.delete();

            filePdf.delete();
        }



        return null;
    }

?下面是工具方法,樣式為自定義,如果想添加表格背景顏色,字體,都是可以定義的,具體查詢poi的api


    /**
     * 講ex對(duì)象轉(zhuǎn)換成文件
     * @param wb
     * @param name
     * @return
     */
    public static File xssfWorkbookToFile(XSSFWorkbook wb, String name) {
        File toFile = new File(name);
        try {
            OutputStream os = new FileOutputStream(toFile);
            wb.write(os);
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return toFile;
    }


    /**
     * 創(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.setBorderBottom(BorderStyle.THIN); //下邊框
//        cellStyle.setBorderLeft(BorderStyle.THIN); //左邊框
//        cellStyle.setBorderRight(BorderStyle.THIN); //右邊框
//        cellStyle.setBorderTop(BorderStyle.THIN); //上邊框
//        cellStyle.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex());//背景顏色

        XSSFFont headerFont1 = (XSSFFont) wb.createFont(); // 創(chuàng)建字體樣式
//        headerFont1.setBold(true); //字體加粗
        headerFont1.setFontName("宋體"); // 設(shè)置字體類型
        headerFont1.setFontHeightInPoints((short) 17); // 設(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) 16); // 設(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é)果為 pdf 向?qū)С鰁xcel把轉(zhuǎn)換pdf的語(yǔ)句刪掉就可以了

POI實(shí)現(xiàn)導(dǎo)出復(fù)雜Excel(動(dòng)態(tài)行,復(fù)雜單元格,水印,Excel轉(zhuǎn)換為PDF)。

?

到了這里,關(guān)于POI實(shí)現(xiàn)導(dǎo)出復(fù)雜Excel(動(dòng)態(tài)行,復(fù)雜單元格,水印,Excel轉(zhuǎn)換為PDF)。的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • Java使用poi導(dǎo)出excel針對(duì)不同數(shù)據(jù)列配置設(shè)置不同單元格格式(適用于通用導(dǎo)出excel數(shù)據(jù))

    Java使用poi導(dǎo)出excel針對(duì)不同數(shù)據(jù)列配置設(shè)置不同單元格格式(適用于通用導(dǎo)出excel數(shù)據(jù))

    公司大部分業(yè)務(wù)都是查詢相關(guān)的業(yè)務(wù), 所以建了一個(gè)項(xiàng)目專門做數(shù)據(jù)查詢, 數(shù)據(jù)中轉(zhuǎn)等抽象通用的業(yè)務(wù), 有一天給我安排了一個(gè)功能, 做excel導(dǎo)出, 配置好查詢sql和表頭字段映射后即可導(dǎo)出excel, 無(wú)需修改代碼 后來(lái)因?yàn)閷?dǎo)出數(shù)據(jù)要求保留幾位小數(shù)或者轉(zhuǎn)換成百分比等設(shè)置單元格格

    2024年02月07日
    瀏覽(26)
  • java poi導(dǎo)出excel單元格設(shè)置自定義背景顏色(任意顏色)

    java poi導(dǎo)出excel單元格設(shè)置自定義背景顏色(任意顏色)

    一、思考過(guò)程(看代碼的移步第二點(diǎn)) 現(xiàn)有方法 現(xiàn)有資料多為使用 IndexedColors 設(shè)置顏色, 但是IndexedColors能設(shè)置的顏色有限 ,而需求中所要顏色都是花里胡哨的,需要真正的自定義; 而顏色的本質(zhì)是rgb ,所以只要我們能自己設(shè)置rgb的值就能獲取任意想要的顏色了; 源碼分

    2023年04月10日
    瀏覽(14)
  • 若依框架自定義導(dǎo)出Excel多sheet頁(yè)+合并單元格(Poi)

    若依框架自定義導(dǎo)出Excel多sheet頁(yè)+合并單元格(Poi)

    先看效果: ? ? 在用若依框架是發(fā)現(xiàn)自帶的導(dǎo)出功能中并不能導(dǎo)出多個(gè)sheet和合并單元格,所以我在這里做了修改希望可以幫到你,用到的點(diǎn)個(gè)贊唄! 我們先一步步來(lái) 整個(gè)程序的思路為先返回下載地址,然后根據(jù)下載地址去下載excel 首先是我們需要excel的下載地址,這里我們

    2024年02月03日
    瀏覽(31)
  • 前端vue+elementui導(dǎo)出復(fù)雜(單元格合并,多級(jí)表頭)表格el-table轉(zhuǎn)為excel導(dǎo)出

    前端vue+elementui導(dǎo)出復(fù)雜(單元格合并,多級(jí)表頭)表格el-table轉(zhuǎn)為excel導(dǎo)出

    需求 :前端對(duì)el-table表格導(dǎo)出 插件 : npm install xlsx -S npm install file-saver --save 原理 :直接導(dǎo)出el-table的表格里面的數(shù)據(jù),這樣就會(huì)存在缺點(diǎn),只會(huì)導(dǎo)出當(dāng)前頁(yè)面的數(shù)據(jù),如果需要導(dǎo)出全部數(shù)據(jù),可以自己重新渲染一個(gè)全部數(shù)據(jù)不可見(jiàn)的el-table表格,來(lái)導(dǎo)出就可以了 擴(kuò)展 :經(jīng)過(guò)

    2024年02月04日
    瀏覽(31)
  • POI 實(shí)現(xiàn)Excel導(dǎo)入導(dǎo)出

    什么是POI Apache POI 是用Java編寫(xiě)的免費(fèi)開(kāi)源的跨平臺(tái)的 Java API,Apache POI提供API給Java程序?qū)icrosoft Office格式檔案讀和寫(xiě)的功能。POI為“Poor Obfuscation Implementation”的首字母縮寫(xiě),意為“簡(jiǎn)潔版的模糊實(shí)現(xiàn)”。 生成xls和xlsx有什么區(qū)別呢? XLS XLSX 只能打開(kāi)xls格式,無(wú)法直接打開(kāi)x

    2024年02月03日
    瀏覽(27)
  • poi實(shí)現(xiàn)Excel文件的導(dǎo)入導(dǎo)出

    poi結(jié)構(gòu)說(shuō)明 引入poi依賴包 第一步、獲取表內(nèi)容數(shù)據(jù): 根據(jù)表頭內(nèi)容與實(shí)體類屬性對(duì)應(yīng)的map,利用反射機(jī)制獲取get方法來(lái)取出該實(shí)體數(shù)據(jù) 第二步、開(kāi)始導(dǎo)出 編輯表格內(nèi)樣式 第三步、設(shè)定響應(yīng)請(qǐng)求頭格式,發(fā)送文件到客戶端 判斷表格行數(shù)據(jù)是否為空 判斷表格列值是否為空 設(shè)

    2024年02月12日
    瀏覽(26)
  • hutool poi、apache poi實(shí)現(xiàn)導(dǎo)入導(dǎo)出以及解析excel

    hutool poi、apache poi實(shí)現(xiàn)導(dǎo)入導(dǎo)出以及解析excel

    一、前言 看了例子之后后續(xù)需要更加深入學(xué)習(xí)或者更多理解其他API的話,建議看官方文檔。hutool項(xiàng)目是中國(guó)人維護(hù)的,有中文文檔,閱讀起來(lái)很方便。apache poi比較底層一點(diǎn),可以更加自由去二次開(kāi)發(fā)自己所需的功能。 hutool官方文檔 hutool官方gitee apache poi官方文檔 二、基于

    2024年02月09日
    瀏覽(24)
  • poi實(shí)現(xiàn)Excel文件導(dǎo)出【SpringBoot篇】

    在系統(tǒng)中,數(shù)據(jù)庫(kù)的excel文件導(dǎo)出是一項(xiàng)及為基礎(chǔ)的功能。此篇文章將通過(guò)實(shí)例利用poi實(shí)現(xiàn)excel文件導(dǎo)出。 Jakarta POI 是apache的子項(xiàng)目,目標(biāo)是處理ole2對(duì)象。它提供了一組操縱Windows文檔的Java API 。目前比較成熟的是HSSF接口,處理MS Excel(97-2002)對(duì)象。它不象我們僅僅是用csv生

    2024年02月05日
    瀏覽(27)
  • java poi實(shí)現(xiàn)Excel多級(jí)表頭導(dǎo)出

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

    最近碰到一個(gè)導(dǎo)出,比較繁瑣,也查詢了許多博客,在其中一篇博客的基礎(chǔ)上修改,實(shí)現(xiàn)了最終想要的效果。話不多說(shuō),直接上效果圖 1.主代碼: 2.合并單元格 3.設(shè)置表頭單元格的寬度 4.填充數(shù)據(jù)(注:我這里的數(shù)據(jù)格式是ListMapString, Object類型,可以根據(jù)自己的實(shí)際情況來(lái)封

    2024年02月03日
    瀏覽(47)
  • Java 使用 poi 和 aspose 實(shí)現(xiàn) word 模板數(shù)據(jù)寫(xiě)入并轉(zhuǎn)換 pdf 增加水印

    Java 使用 poi 和 aspose 實(shí)現(xiàn) word 模板數(shù)據(jù)寫(xiě)入并轉(zhuǎn)換 pdf 增加水印

    本項(xiàng)目所有源碼和依賴資源都在文章頂部鏈接,有需要可以下載使用 1. 需求描述 從指定位置讀取一個(gè) word 模板 獲取業(yè)務(wù)數(shù)據(jù)并寫(xiě)入該 word 模板,生成新的 word 文檔 將新生成的 word 文檔轉(zhuǎn)換為 pdf 格式 對(duì) pdf 文檔添加水印 2. 效果預(yù)覽 word 模板 帶水印的 pdf 文檔 3. 實(shí)現(xiàn)思路

    2024年02月08日
    瀏覽(29)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包