You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot中使用Apache POI单独格式化单个单元格的方法

问题分析

你遇到的核心问题是所有单元格共用了同一个CellStyle实例。POI中的CellStyle是共享对象,一旦修改这个样式的属性(比如设置日期格式setDataFormat((short)14)),所有引用该样式的单元格都会同步变化,这就是其他列被错误设置成日期格式的原因。

你在循环末尾修改了cellStyle的日期格式,而之前所有表头和数据单元格都绑定了这个样式,自然会全部受到影响。

解决方法

创建两个独立的CellStyle:一个用于通用的边框、对齐样式;另一个基于通用样式扩展,添加日期格式,仅给最后一列(到期日列)的单元格使用。

修改后的代码如下:

import com.xxxxxxxxxxxxxxxxxxxxxx.entity.xxxxxxxxx.xxxxxxxxx;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class xxxxxxxxxxxxxxxxxx {

    @Autowired
    private xxxxxxxxxxxxxxxxDTO xxxxxxxxxxxxxxxxxxxxxDTO;

    public static void xxxxxxxx(HttpServletResponse response, List<xxxxxxxxxxxxxxxxxxxxDTO> xxxxxxx) {
        try (Workbook workbook = new XSSFWorkbook()) {
            Sheet sheet = workbook.createSheet("xxxxxx a xxxxxx");

            // 通用样式:边框+左对齐
            CellStyle generalCellStyle = workbook.createCellStyle();
            generalCellStyle.setBorderTop(BorderStyle.MEDIUM);
            generalCellStyle.setBorderRight(BorderStyle.MEDIUM);
            generalCellStyle.setBorderBottom(BorderStyle.MEDIUM);
            generalCellStyle.setBorderLeft(BorderStyle.MEDIUM);
            generalCellStyle.setAlignment(HorizontalAlignment.LEFT);

            // 日期样式:基于通用样式,添加日期格式
            CellStyle dateCellStyle = workbook.createCellStyle();
            dateCellStyle.cloneStyleFrom(generalCellStyle); // 复制通用样式的属性
            dateCellStyle.setDataFormat((short) 14); // 设置日期格式(对应Excel的yyyy/mm/dd)

            // 构建表头
            Row row = sheet.createRow(0);
            Cell cell = row.createCell(0);
            cell.setCellValue("file");
            cell.setCellStyle(generalCellStyle);

            Cell cell1 = row.createCell(1);
            cell1.setCellValue("file1");
            cell1.setCellStyle(generalCellStyle);

            Cell cell2 = row.createCell(2);
            cell2.setCellValue("file2");
            cell2.setCellStyle(generalCellStyle);

            Cell cell3 = row.createCell(3);
            cell3.setCellValue("file3");
            cell3.setCellStyle(generalCellStyle);

            Cell cell4 = row.createCell(4);
            cell4.setCellValue("file4");
            cell4.setCellStyle(generalCellStyle);

            Cell cell5 = row.createCell(5);
            cell5.setCellValue("file5");
            cell5.setCellStyle(generalCellStyle);

            Cell cell6 = row.createCell(6);
            cell6.setCellValue("file6");
            cell6.setCellStyle(generalCellStyle);

            Cell cell7 = row.createCell(7);
            cell7.setCellValue("file7");
            cell7.setCellStyle(generalCellStyle);

            Cell cell8 = row.createCell(8);
            cell8.setCellValue("到期日"); // 建议修改为对应列名
            cell8.setCellStyle(generalCellStyle);

            // 填充数据行
            int rowNum = 1;
            for (xxxxxxxxxxxxxxxDTO integ : xxxxxxx) {
                Row intDataRow = sheet.createRow(rowNum++);

                Cell empCell = intDataRow.createCell(0);
                empCell.setCellStyle(generalCellStyle);
                empCell.setCellValue(integ.getFile());

                Cell filNameCell = intDataRow.createCell(1);
                filNameCell.setCellStyle(generalCellStyle);
                filNameCell.setCellValue(integ.getFile1());

                Cell cdCell = intDataRow.createCell(2);
                cdCell.setCellStyle(generalCellStyle);
                cdCell.setCellValue(integ.getFile2());

                Cell coCell = intDataRow.createCell(3);
                coCell.setCellStyle(generalCellStyle);
                coCell.setCellValue(integ.getFile3());

                Cell poCell = intDataRow.createCell(4);
                poCell.setCellStyle(generalCellStyle);
                poCell.setCellValue(integ.getFile4());

                Cell tiCell = intDataRow.createCell(5);
                tiCell.setCellStyle(generalCellStyle);
                tiCell.setCellValue(integ.getFile5());

                Cell saCell = intDataRow.createCell(6);
                saCell.setCellStyle(generalCellStyle);
                saCell.setCellValue(integ.getFile6());

                Cell comCell = intDataRow.createCell(7);
                comCell.setCellStyle(generalCellStyle);
                comCell.setCellValue(integ.getFile7());

                // 最后一列(到期日)使用日期样式
                Cell venCell = intDataRow.createCell(8);
                venCell.setCellStyle(dateCellStyle);
                // 注意:若integ.getFile8()是Date类型直接赋值;若是字符串需先转换为Date
                venCell.setCellValue(integ.getFile8());
            }

            workbook.write(response.getOutputStream());

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
关键要点
  • 避免共享样式修改:永远不要在已分配给单元格的CellStyle上修改属性,否则所有关联单元格都会受影响。
  • 使用cloneStyleFrom复用样式:通过克隆已有样式创建新样式,既避免重复设置通用属性,又保证新样式的独立性。
  • 针对性分配样式:仅给需要日期格式的单元格绑定日期专用CellStyle。

内容的提问来源于stack exchange,提问作者cesar pereira

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 14:10:34