对一二三四行每一行都做了样式处理


java
package org.springblade.modules.api.controller;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* 2.2.10版本:修改本地Excel文件的行样式
* 支持为不同行设置独立的样式(字体、行高、背景色、对齐方式、边框等)
* 所有行统一使用细实线边框(BorderStyle.THIN)+ 黑色边框颜色
*/
public class LocalExcelRowStyleModifierHang {
// 定义行样式配置类:封装单行的所有样式属性(包含边框配置)
public static class RowStyleConfig {
private boolean bold; // 是否加粗
private String fontName; // 字体名称
private short fontSize; // 字号
private short fontColor; // 字体颜色
private short bgColor; // 背景色
private float rowHeight; // 行高(磅)
private HorizontalAlignment horizontalAlign; // 水平对齐
private VerticalAlignment verticalAlign; // 垂直对齐
// 边框配置(所有行统一为细实线边框)
private BorderStyle borderStyle; // 边框样式(当前全量为细实线THIN)
private short borderColor; // 边框颜色(当前全量为黑色)
// 全参构造器(包含边框参数)
public RowStyleConfig(boolean bold, String fontName, short fontSize, short fontColor, short bgColor,
float rowHeight, HorizontalAlignment horizontalAlign, VerticalAlignment verticalAlign,
BorderStyle borderStyle, short borderColor) {
this.bold = bold;
this.fontName = fontName;
this.fontSize = fontSize;
this.fontColor = fontColor;
this.bgColor = bgColor;
this.rowHeight = rowHeight;
this.horizontalAlign = horizontalAlign;
this.verticalAlign = verticalAlign;
this.borderStyle = borderStyle;
this.borderColor = borderColor;
}
// Getter方法
public boolean isBold() { return bold; }
public String getFontName() { return fontName; }
public short getFontSize() { return fontSize; }
public short getFontColor() { return fontColor; }
public short getBgColor() { return bgColor; }
public float getRowHeight() { return rowHeight; }
public HorizontalAlignment getHorizontalAlign() { return horizontalAlign; }
public VerticalAlignment getVerticalAlign() { return verticalAlign; }
public BorderStyle getBorderStyle() { return borderStyle; }
public short getBorderColor() { return borderColor; }
}
/**
* 核心方法:为多行设置不同样式(所有行统一应用细实线边框)
* @param sourceFilePath 原Excel路径
* @param targetFilePath 保存路径
* @param sheetIndex 工作表索引(0开始)
* @param rowStyleMap 行号-样式配置的映射(key=行号,value=该行的样式)
* @throws IOException 文件异常
*/
public static void modifyMultiRowWithDifferentStyle(String sourceFilePath, String targetFilePath,
int sheetIndex, Map<Integer, RowStyleConfig> rowStyleMap) throws IOException {
// 1. 读取Excel文件
File sourceFile = new File(sourceFilePath);
if (!sourceFile.exists()) {
throw new IOException("原文件不存在:" + sourceFilePath);
}
FileInputStream fis = new FileInputStream(sourceFile);
Workbook workbook = new XSSFWorkbook(fis); // 仅支持.xlsx,.xls替换为HSSFWorkbook
Sheet sheet = workbook.getSheetAt(sheetIndex);
// 2. 遍历每一行,应用对应的样式(含细边框)
for (Map.Entry<Integer, RowStyleConfig> entry : rowStyleMap.entrySet()) {
int rowNum = entry.getKey();
RowStyleConfig styleConfig = entry.getValue();
// 定位目标行(不存在则创建)
Row targetRow = sheet.getRow(rowNum);
if (targetRow == null) {
targetRow = sheet.createRow(rowNum);
}
// 3. 根据配置创建该行专属的字体和单元格样式
Font font = workbook.createFont();
font.setBold(styleConfig.isBold());
font.setFontName(styleConfig.getFontName());
font.setFontHeightInPoints(styleConfig.getFontSize());
font.setColor(styleConfig.getFontColor());
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
cellStyle.setFillForegroundColor(styleConfig.getBgColor());
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cellStyle.setAlignment(styleConfig.getHorizontalAlign());
cellStyle.setVerticalAlignment(styleConfig.getVerticalAlign());
// 设置边框样式(上下左右统一为细实线边框)
cellStyle.setBorderTop(styleConfig.getBorderStyle());
cellStyle.setBorderBottom(styleConfig.getBorderStyle());
cellStyle.setBorderLeft(styleConfig.getBorderStyle());
cellStyle.setBorderRight(styleConfig.getBorderStyle());
// 设置边框颜色(统一为黑色)
cellStyle.setTopBorderColor(styleConfig.getBorderColor());
cellStyle.setBottomBorderColor(styleConfig.getBorderColor());
cellStyle.setLeftBorderColor(styleConfig.getBorderColor());
cellStyle.setRightBorderColor(styleConfig.getBorderColor());
// 4. 设置行高
targetRow.setHeightInPoints(styleConfig.getRowHeight());
// 5. 应用样式到该行所有单元格(含空单元格)
int lastCellNum = targetRow.getLastCellNum();
// 处理已存在的单元格
for (int i = 0; i < (lastCellNum == -1 ? 0 : lastCellNum); i++) {
Cell cell = targetRow.getCell(i);
if (cell == null) {
cell = targetRow.createCell(i);
}
cell.setCellStyle(cellStyle);
}
}
// 6. 保存并关闭资源
FileOutputStream fos = new FileOutputStream(targetFilePath);
workbook.write(fos);
// 关闭流(按逆序关闭)
fos.close();
workbook.close();
fis.close();
System.out.println("多行多样式(全量细边框)修改完成!修改后文件路径:" + targetFilePath);
}
// 保留原有单一行修改方法(兼容旧逻辑,默认细实线边框)
public static void modifyRowStyle(String sourceFilePath, String targetFilePath, int sheetIndex, int targetRowNum) throws IOException {
// 构建默认样式(含细实线边框:黑色细边框)
RowStyleConfig defaultConfig = new RowStyleConfig(
true, "黑体", (short)22, IndexedColors.BLACK.getIndex(),
IndexedColors.WHITE.getIndex(), 47.6f,
HorizontalAlignment.CENTER, VerticalAlignment.CENTER,
BorderStyle.THIN, IndexedColors.BLACK.getIndex() // 细实线边框+黑色
);
Map<Integer, RowStyleConfig> rowStyleMap = new HashMap<>();
rowStyleMap.put(targetRowNum, defaultConfig);
modifyMultiRowWithDifferentStyle(sourceFilePath, targetFilePath, sheetIndex, rowStyleMap);
}
public static void main(String[] args) {
try {
String sourcePath = "F:\\machine\\noModelWrite1773198654174.xlsx";
String targetPath = "F:\\machine\\test_modified_" + System.currentTimeMillis() + ".xlsx";
Map<Integer, RowStyleConfig> rowStyleMap = new HashMap<>();
// 第0行(表头):黑体、22号、加粗、白色背景、居中、行高47.6 + 黑色细边框
rowStyleMap.put(0, new RowStyleConfig(
true,
"黑体",
(short) 22,
IndexedColors.BLACK.getIndex(),
IndexedColors.WHITE.getIndex(),
47.6f,
HorizontalAlignment.CENTER,
VerticalAlignment.CENTER,
BorderStyle.THIN, // 细实线边框
IndexedColors.BLACK.getIndex() // 黑色边框
));
// 第1行:宋体、16号、加粗、白色背景、右对齐、行高47.6 + 黑色细边框
rowStyleMap.put(1, new RowStyleConfig(
true,
"宋体",
(short) 16,
IndexedColors.BLACK.getIndex(),
IndexedColors.WHITE.getIndex(),
47.6f,
HorizontalAlignment.RIGHT,
VerticalAlignment.CENTER,
BorderStyle.THIN, // 细实线边框
IndexedColors.BLACK.getIndex() // 黑色边框
));
// 第2行:楷体、16号、加粗、白色背景、居中、行高34.8 + 黑色细边框
rowStyleMap.put(2, new RowStyleConfig(
true,
"楷体_GB2312",
(short) 16,
IndexedColors.BLACK.getIndex(),
IndexedColors.WHITE.getIndex(),
34.8f,
HorizontalAlignment.CENTER,
VerticalAlignment.CENTER,
BorderStyle.THIN, // 细实线边框
IndexedColors.BLACK.getIndex() // 黑色边框
));
// 第3行:楷体、12号、加粗、白色背景、居中、行高34.8 + 黑色细边框
rowStyleMap.put(3, new RowStyleConfig(
true,
"楷体_GB2312",
(short) 12,
IndexedColors.BLACK.getIndex(),
IndexedColors.WHITE.getIndex(),
34.8f,
HorizontalAlignment.CENTER,
VerticalAlignment.CENTER,
BorderStyle.THIN, // 细实线边框
IndexedColors.BLACK.getIndex() // 黑色边框
));
modifyMultiRowWithDifferentStyle(sourcePath, targetPath, 0, rowStyleMap);
} catch (IOException e) {
e.printStackTrace();
System.out.println("修改失败:" + e.getMessage());
}
}
}