Besides one cell, is there any way to style the entire table?
Inside the Word, set it up as you see in the image. I wonder if there is a way to designate a basic style like a word table.
Thanks!
The table styles are stored in a separate styles.xml
file in Word
's Office Open XML
file storage. Apache POI
does not creating such a styles document per default. But it supports creating a such using XWPFDocument.createStyles
. If you have that, the need is creating a table style in that styles document. Then link that style to the table using XWPFTable.setStyleID
.
Creating a XWPFStyle
until now is only supported by using contructors which needs a org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyle
object. So one need using low level ooxml-schemas
objects and methods to create such a CTStyle
object. The shortest way to do so is parsing XML
to such an object. The folowing complete example shows this.
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();
}
}
Where do I have the XML
from? I have created a simple table using Word
, then applied the table style "List Table 2" to it. Then I unzipped the resulting *.docx
file and looked into /word/styles.xml
. There I found what XML
was used for table style "List Table 2".