如何使用Java Apache POI为MS Word设置默认样式及蓝色表格样式
使用Apache POI设置Word蓝色表格样式方案
你可以通过两种方式实现你需要的蓝色表格样式,以下是具体实现步骤:
一、直接设置单元格样式(适合简单场景)
这种方式可以逐个定义表头、数据行的背景色和字体样式,快速匹配需求:
import org.apache.poi.xwpf.usermodel.*; import java.io.FileOutputStream; import java.io.IOException; import org.openxmlformats.schemas.wordprocessingml.x2006.main.*; public class BlueTableDemo { public static void main(String[] args) throws IOException { XWPFDocument doc = new XWPFDocument(); // 创建3行3列的表格 XWPFTable table = doc.createTable(3, 3); // 设置表头样式:深蓝色背景+白色加粗字体 XWPFTableRow headerRow = table.getRow(0); for (XWPFTableCell cell : headerRow.getTableCells()) { // 设置深蓝色背景(十六进制RGB:145AA0) CTTcPr tcPr = cell.getCTTc().getTcPr(); CTShd shd = tcPr.addNewShd(); shd.setColor("auto"); shd.setFill("145AA0"); shd.setVal(STShd.CLEAR); // 设置白色加粗字体 XWPFRun run = cell.getParagraphs().get(0).createRun(); run.setColor("FFFFFF"); run.setBold(true); run.setText("表头单元格"); } // 设置数据行样式:浅蓝色背景+黑色字体 for (int rowIndex = 1; rowIndex < table.getNumberOfRows(); rowIndex++) { XWPFTableRow dataRow = table.getRow(rowIndex); for (XWPFTableCell cell : dataRow.getTableCells()) { // 设置浅蓝色背景(十六进制RGB:D2E6FA) CTTcPr tcPr = cell.getCTTc().getTcPr(); CTShd shd = tcPr.addNewShd(); shd.setColor("auto"); shd.setFill("D2E6FA"); shd.setVal(STShd.CLEAR); // 设置黑色字体 XWPFRun run = cell.getParagraphs().get(0).createRun(); run.setColor("000000"); run.setText("数据单元格"); } } // 保存生成的文档 try (FileOutputStream out = new FileOutputStream("blue_table.docx")) { doc.write(out); } doc.close(); } }
二、自定义表格样式(适合复用场景)
如果需要在文档中多次使用同一样式,建议先自定义表格样式再应用,便于统一维护:
import org.apache.poi.xwpf.usermodel.*; import java.io.FileOutputStream; import java.io.IOException; import org.openxmlformats.schemas.wordprocessingml.x2006.main.*; public class CustomBlueTableStyle { public static void main(String[] args) throws IOException { XWPFDocument doc = new XWPFDocument(); // 创建自定义表格样式 XWPFStyle tableStyle = doc.createStyle(); tableStyle.setType(STStyleType.TABLE); tableStyle.setStyleId("CustomBlueTable"); tableStyle.setName("蓝色表格样式"); // 配置表头行样式 CTTrPr headerTrPr = tableStyle.getCTStyle().addNewTrPr(); CTShd headerShd = headerTrPr.addNewShd(); headerShd.setFill("145AA0"); headerShd.setColor("auto"); // 配置表头字体样式 CTPr headerTextPr = tableStyle.getCTStyle().addNewRPr(); headerTextPr.addNewColor().setVal("FFFFFF"); headerTextPr.addNewBold().setVal(true); // 配置数据行样式 CTTrPr dataTrPr = tableStyle.getCTStyle().addNewTrPr(); CTShd dataShd = dataTrPr.addNewShd(); dataShd.setFill("D2E6FA"); dataShd.setColor("auto"); // 创建表格并应用自定义样式 XWPFTable table = doc.createTable(3, 3); table.setStyleID("CustomBlueTable"); // 填充表格内容 table.getRow(0).getCell(0).setText("表头1"); table.getRow(0).getCell(1).setText("表头2"); table.getRow(0).getCell(2).setText("表头3"); table.getRow(1).getCell(0).setText("数据1"); table.getRow(1).getCell(1).setText("数据2"); table.getRow(1).getCell(2).setText("数据3"); // 保存文档 try (FileOutputStream out = new FileOutputStream("custom_blue_table.docx")) { doc.write(out); } doc.close(); } }
注意事项
- 颜色值使用十六进制RGB码,你可以根据示例图的色调调整具体数值,精准匹配需求
- 确保使用Apache POI 4.0及以上版本,旧版本对表格样式的支持存在局限性
- 如需添加边框样式,可以通过
CTTcPr.addNewTcBorders()方法配置边框的颜色、粗细等属性
内容的提问来源于stack exchange,提问作者陳昇昇
相关产品推荐
相关产品推荐

