1. 程式人生 > 其它 >記錄使用Java Poi匯出Excel表格,合併單元格,單元格背景顏色,單元格邊框

記錄使用Java Poi匯出Excel表格,合併單元格,單元格背景顏色,單元格邊框

預覽的匯出效果

實現程式碼

        Workbook wb = new XSSFWorkbook();
        String sheetName = "全過程諮詢";
        List<ExcelTitle> excelTitles = new ArrayList<>();
        excelTitles.add(new ExcelTitle("序號" , null));
        excelTitles.add(new ExcelTitle("業務部/分公司" , null));
        excelTitles.add(new ExcelTitle("工程實施專案部" , null));
        List<String> columns = new ArrayList<String>(){{
            add("公司整體形象");
            add("合同履行情況及信譽度");
        }};
        excelTitles.add(new ExcelTitle("工程監理過程控制能力/招標專案服務水平能力/專案諮詢造價控制能力" , columns));
        excelTitles.add(new ExcelTitle("阿達瓦達瓦薩達" , null));
        columns = new ArrayList<String>(){{
            add("專案服務質量、進度控制/諮詢服務質量、進度控制");
            add("專案服務的造價控制/諮詢業務能力");
            add("專案服務的安全控制/諮詢服務的時間安排");
        }};
        excelTitles.add(new ExcelTitle("公司整體實力" , columns));
        excelTitles.add(new ExcelTitle("發乾哈阿斯達斯的" , null));
        columns = new ArrayList<String>(){{
            add("專案服務質量、進度控制/諮詢服務質量、進度控制");
            add("專案服務的造價控制/諮詢業務能力");
            add("專案服務的安全控制/諮詢服務的時間安排");
            add("專案服務的造價控制/諮詢業務能力");
            add("專案服務的安全控制/諮詢服務的時間安排");
        }};
        excelTitles.add(new ExcelTitle("4566666666546456564564" , columns));

        // 計算實際列數
        int countColNums = 0;
        for (ExcelTitle excelTitle : excelTitles) {
            if(excelTitle.getColumns() != null){
                // 如果有子項,那麼不統計父項的數量 不然會多出列數
                countColNums += excelTitle.getColumns().size();
            }else{
                countColNums++;
            }
        }

        Sheet sheet = wb.createSheet(sheetName);

        int rowSize = 1;
        Row row = sheet.createRow(rowSize);
        row.setHeight((short) (50*20));

        // 第四步,建立單元格,並設定值表頭 設定表頭居中
        CellStyle style = wb.createCellStyle();
        //設定樣式對齊方式:水平\垂直居中
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        style.setFillForegroundColor(IndexedColors.GOLD.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        // 單元格邊框
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        style.setBorderTop(BorderStyle.THIN);
        // 文字自動換行
        style.setWrapText(true);

        int columnWidth = 150*20;

        // 記錄父項開始的列數
        Map<String, Integer> titleStartColNumMap = new HashMap<>();

        // 建立標題
        int n = 0;
        for (int i = 0; i < countColNums; i++) {
            ExcelTitle excelTitle = excelTitles.get(n);
            // 合併開始列
            int startRangeSize = i;
            // 合併結束列
            int endRangeColSize = i;

            if(excelTitle.getColumns() != null){
                titleStartColNumMap.put(excelTitle.getName() , i);
                for (int k = 0; k < excelTitle.getColumns().size(); k++) {
                    Cell cell = row.createCell(i + k);
                    cell.setCellStyle(style);
                    cell.setCellValue(excelTitle.getName());
                    sheet.setColumnWidth((short) i + k , columnWidth);
                }

                i += excelTitle.getColumns().size() - 1;
                endRangeColSize = i;
            }else{
                Cell cell = row.createCell(i);
                cell.setCellStyle(style);
                cell.setCellValue(excelTitle.getName());
                sheet.setColumnWidth((short) i , columnWidth);
            }

            int rangeRowSize = excelTitle.getColumns() == null ? row.getRowNum() + 1 : row.getRowNum();
            // 合併單元格
            CellRangeAddress rangeAddress = new CellRangeAddress(row.getRowNum(), rangeRowSize, startRangeSize, endRangeColSize);
            sheet.addMergedRegion(rangeAddress);

            // 增加合併後的單元格邊框
            RegionUtil.setBorderBottom(BorderStyle.THIN, rangeAddress, sheet); // 下邊框
            RegionUtil.setBorderLeft(BorderStyle.THIN, rangeAddress, sheet); // 左邊框
            RegionUtil.setBorderRight(BorderStyle.THIN, rangeAddress, sheet); // 有邊框
            RegionUtil.setBorderTop(BorderStyle.THIN, rangeAddress, sheet); // 上邊框

            n++;
        }

        row = sheet.getRow(++rowSize);
        row.setHeight((short) (50*20));
         // 將子項內容新增至父項下面的單元格
        for (int i = 0; i < excelTitles.size(); i++) {
            ExcelTitle excelTitle = excelTitles.get(i);
            if(excelTitle.getColumns() != null){
                for (int j = 0; j < excelTitle.getColumns().size(); j++) {
                    Cell cell = row.createCell(titleStartColNumMap.get(excelTitle.getName()) + j);
                    cell.setCellStyle(style);
                    cell.setCellValue(excelTitle.getColumns().get(j));
                    sheet.setColumnWidth((short) i , columnWidth);
                }
            }
        }

        row = sheet.createRow(0);
        row.setHeight((short) (30*20));

        style = wb.createCellStyle();
        //設定樣式對齊方式:水平\垂直居中
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        // 標題
        Cell cell = row.createCell(0);
        cell.setCellValue("2020年上半年(   )分公司(部門)監理、諮詢客戶滿意度調查情況明細");
        cell.setCellStyle(style);
        CellRangeAddress rangeAddress = new CellRangeAddress(0, 0, 0, countColNums - 1);
        sheet.addMergedRegion(rangeAddress);
        file.getParentFile().mkdirs();
        FileOutputStream os = new FileOutputStream(file);
        wb.write(os);
        os.flush();
        os.close();
        public static class ExcelTitle {
            private String name;

            private List<String> columns;

            public ExcelTitle() {
            }

            public ExcelTitle(String name, List<String> columns) {
                this.name = name;
                this.columns = columns;
            }

            public String getName() {
                return name;
            }

            public void setName(String name) {
                this.name = name;
            }

            public List<String> getColumns() {
                return columns;
            }

            public void setColumns(List<String> columns) {
                this.columns = columns;
            }
    }
      pom.xml 版本
      <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.0</version>
        </dependency>