如何通过java spring格式化excel?

我导出了一个excel文件,如图:

http://img3.mukewang.com/628df21900012af410860433.jpg

我想为工作表中的文本编辑“align_center”。当我合并两行时,我想为它格式化“中心”。这是它的后端代码:


CellStyle headerCellStyle = workbook.createCellStyle();

        headerCellStyle.setFont(headerFont);

        headerCellStyle.setAlignment(HorizontalAlignment.CENTER);


        // Row for Header

        Row headerRow = sheet.createRow(0);


        // Header

        for (int col = 0; col < COLUMNTASK.length; col++) {

            Cell cell = headerRow.createCell(col);

            cell.setCellValue(COLUMNTASK[col]);

            cell.setCellStyle(headerCellStyle);

        }

for (int col = 0; col < COLUMNTASK.length; col++) {

            Cell cell = headerRow.createCell(col);

            cell.setCellValue(COLUMNTASK[col]);

            cell.setCellStyle(headerCellStyle);

        }

        int count;

        for (int i = 0; i < allTasks.size()-1; i++) {

            count = i;

            for (int j = count+1; j < allTasks.size(); j++) {

                if (allTasks.get(i).getUserName().equals(allTasks.get(j).getUserName())) {

                    count++;

                } else if (count != i) {

                    sheet.addMergedRegion(new CellRangeAddress(i+1, count+1, 0, 0));

                    i = count;

                } else if (count == i) {

                    break;

                }


                if (count == allTasks.size()-1) {

                    sheet.addMergedRegion(new CellRangeAddress(i+1, count+1, 0, 0));

                    i = count;

                    break;

                }

            }

        }

        int rowIdx = 1;

        for (WorkingTimeReportResponse workingTimeReportResponse : allTasks) {

            Row row = sheet.createRow(rowIdx++);


            row.createCell(0).setCellValue(workingTimeReportResponse.getUserName());

            row.createCell(1).setCellValue(workingTimeReportResponse.getProjectName());

            row.createCell(2).setCellValue(workingTimeReportResponse.getIssue());

            row.createCell(3).setCellValue(workingTimeReportResponse.getTotalHours());

        }


慕容3067478
浏览 79回答 1
1回答

小唯快跑啊

我认为有必要像在标题单元格中那样在所有单元格中设置样式。首先,为常规单元格创建样式:CellStyle regularCellStyle = workbook.createCellStyle();regularCellStyle setAlignment(HorizontalAlignment.CENTER);之后,创建单元格并将其存储在变量中。Cell cellA = row.createCell(0);cellA.setCellValue(workingTimeReportResponse.getUserName());cellA.setCellStyle(regularCellStyle);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java