Java使用POI修改Excel文件

import cn.hutool.core.collection.CollectionUtil;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Map;

@Slf4j
@UtilityClass
public class WriteExcelPOIUtil {

    /**
     * 读取修改学生数据导入excel单元格内容
     *
     * @param filePath excel本地路径
     * @param fieldMap Map<行, Map<列, 修改内容>>
     */
    public void writeStudentExcelPOI(String filePath, Map<Integer, Map<Integer, String>> fieldMap) {
        if (CollectionUtil.isEmpty(fieldMap)) {
            return;
        }
        try (XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(filePath));) {
            XSSFSheet xSheet = workbook.getSheetAt(0);  //获取excel表的第一个sheet

            //标题在excel中的行数 从0开始
            final int TITLE_ROW = 1;
            //错误详情在excel的列数 从0开始
            final int ERROR_COLUMN = 5;
            //标题行添加错误详情字段
            modifyTitle(xSheet, TITLE_ROW, ERROR_COLUMN);

            //遍历所有需要修改的行
            for (Map.Entry<Integer, Map<Integer, String>> rowMap : fieldMap.entrySet()) {
                Integer row = rowMap.getKey();
                XSSFRow sheetRow = xSheet.getRow(row);
                if (sheetRow == null) {
                    sheetRow = xSheet.createRow(row);
                }
                //遍历当前行的所有列
                for (Map.Entry<Integer, String> columnMap : rowMap.getValue().entrySet()) {
                    Integer column = columnMap.getKey();
                    //使用createCell不管这个单元格有没有数据,会直接创建一个空白的单元格覆盖上
                    XSSFCell xCell = sheetRow.createCell(column);
                    //修改数据
                    xCell.setCellValue(columnMap.getValue());
                    log.debug("第{}行第{}列修改为:{}", row + 1, column + 1, columnMap.getValue());
                    //设置这行数据背景颜色为黄色
                    for (int i = 0; i <= column; i++) {
                        XSSFCell cell = sheetRow.getCell(i);
                        CellStyle cellStyle = workbook.createCellStyle();
                        //使用cloneStyleFrom可以保证不会影响其他单元格格式 因为这些单元格使用的是同一个样式
                        cellStyle.cloneStyleFrom(cell.getCellStyle());
                        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
                        cellStyle.setFillForegroundColor(IndexedColors.YELLOW1.getIndex());
                        cell.setCellStyle(cellStyle);
                    }
                }
            }
            //第五列设置列宽自适应
            xSheet.autoSizeColumn(ERROR_COLUMN);
            try (FileOutputStream out = new FileOutputStream(filePath)) {
                workbook.write(out);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void modifyTitle(XSSFSheet xSheet, int TITLE_ROW, int ERROR_COLUMN) {
        XSSFRow titleRow = xSheet.getRow(TITLE_ROW);
        XSSFCell titleRowCell = titleRow.createCell(ERROR_COLUMN);
        CellCopyPolicy cellCopyPolicy = new CellCopyPolicy();
        cellCopyPolicy.setCopyCellValue(false);
        //复制左边一个单元格格式
        titleRowCell.copyCellFrom(titleRow.getCell(ERROR_COLUMN - 1), cellCopyPolicy);
        XSSFCellStyle titleRowCellCellStyle = titleRowCell.getCellStyle();
        //单元格上方添加黑色的细实线
        titleRowCellCellStyle.setBorderTop(BorderStyle.THIN);
        titleRowCellCellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
        titleRowCell.setCellValue("错误详情");
    }

}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值