【发布时间】:2020-10-27 08:05:41
【问题描述】:
【问题讨论】:
标签: java apache-poi xwpf
【问题讨论】:
标签: java apache-poi xwpf
表格样式存储在Word 的Office Open XML 文件存储中的单独styles.xml 文件中。 Apache POI 默认情况下不会创建这样的样式文档。但它支持使用XWPFDocument.createStyles 创建这样的。如果有,则需要在该样式文档中创建表格样式。然后使用XWPFTable.setStyleID将该样式链接到表格。
到目前为止,只有使用需要 org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyle 对象的构造函数才支持创建 XWPFStyle。因此需要使用低级别的ooxml-schemas 对象和方法来创建这样一个CTStyle 对象。最短的方法是将XML 解析为这样的对象。下面的完整示例显示了这一点。
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
public class CreateWordTable {
private static XWPFStyle createTableStyle(XWPFStyles styles, String styleId) throws Exception {
if (styles == null || styleId == null) return null;
String tableStyleXML =
"<w:style xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" w:styleId=\"" + styleId + "\" w:type=\"table\">"
+ "<w:name w:val=\"" + styleId + "\"/>"
+ "<w:pPr><w:spacing w:lineRule=\"auto\" w:line=\"240\" w:after=\"0\"/></w:pPr>"
+ "<w:tblPr>"
+ "<w:tblStyleRowBandSize w:val=\"1\"/><w:tblStyleColBandSize w:val=\"1\"/>"
+ "<w:tblBorders>"
+ "<w:top w:val=\"single\" w:themeTint=\"99\" w:themeColor=\"text1\" w:color=\"666666\" w:space=\"0\" w:sz=\"4\"/>"
+ "<w:bottom w:val=\"single\" w:themeTint=\"99\" w:themeColor=\"text1\" w:color=\"666666\" w:space=\"0\" w:sz=\"4\"/>"
+ "<w:insideH w:val=\"single\" w:themeTint=\"99\" w:themeColor=\"text1\" w:color=\"666666\" w:space=\"0\" w:sz=\"4\"/>"
+ "</w:tblBorders>"
+ "</w:tblPr>"
+ "<w:tblStylePr w:type=\"firstRow\"><w:rPr><w:b/><w:bCs/></w:rPr></w:tblStylePr>"
+ "<w:tblStylePr w:type=\"lastRow\"><w:rPr><w:b/><w:bCs/></w:rPr></w:tblStylePr>"
+ "<w:tblStylePr w:type=\"firstCol\"><w:rPr><w:b/><w:bCs/></w:rPr></w:tblStylePr>"
+ "<w:tblStylePr w:type=\"lastCol\"><w:rPr><w:b/><w:bCs/></w:rPr></w:tblStylePr>"
+ "<w:tblStylePr w:type=\"band1Vert\"><w:tblPr/><w:tcPr><w:shd w:val=\"clear\" w:color=\"auto\" w:themeFillTint=\"33\" w:themeFill=\"text1\" w:fill=\"CCCCCC\"/></w:tcPr></w:tblStylePr>"
+ "<w:tblStylePr w:type=\"band1Horz\"><w:tblPr/><w:tcPr><w:shd w:val=\"clear\" w:color=\"auto\" w:themeFillTint=\"33\" w:themeFill=\"text1\" w:fill=\"CCCCCC\"/></w:tcPr></w:tblStylePr>"
+ "</w:style>";
CTStyles ctStyles = CTStyles.Factory.parse(tableStyleXML);
CTStyle ctStyle = ctStyles.getStyleArray(0);
XWPFStyle style = styles.getStyle(styleId);
if (style == null) {
style = new XWPFStyle(ctStyle, styles);
styles.addStyle(style);
} else {
style.setStyle(ctStyle);
}
return style;
}
public static void main(String[] args) throws Exception {
XWPFDocument document = new XWPFDocument();
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText("The table");
XWPFTable table = document.createTable(6, 4);
for (int r = 0; r < 6; r++) {
for (int c = 0; c < 4; c++) {
XWPFTableCell cell = table.getRow(r).getCell(c);
cell.setText("row " + (r+1) + ", col " + (c+1));
}
}
table.removeBorders();
XWPFStyles styles = document.createStyles();
XWPFStyle style = createTableStyle(styles, "ListTableStyle");
table.setStyleID(style.getStyleId());
FileOutputStream out = new FileOutputStream("CreateWordTable.docx");
document.write(out);
out.close();
document.close();
}
}
我从哪里获得XML?我使用Word 创建了一个简单的表格,然后对其应用表格样式“列表表2”。然后我解压生成的*.docx 文件并查看/word/styles.xml。在那里,我发现 XML 用于表格样式“列出表格 2”。
【讨论】: