poi控制简单的word

时间:2022-03-07 03:21:51

官方model网址:http://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/xwpf/usermodel/examples/。

需要jar包:

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.15</version>
</dependency>

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>

根據官方model可以做很多事情,一下是我改變的例子,可以直接運行:

package test.poi;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.math.BigInteger;
import java.util.List;

import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.UnderlinePatterns;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHeight;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTShd;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTString;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTrPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTVerticalJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STShd;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalJc;

public class WordTest {
public static void main(String[] args) throws Exception {
try {
createSimpleTable();
} catch (Exception e) {
System.out.println("创建word失败");
throw (e);
}
try {
createStyledTable();
} catch (Exception e) {
System.out.println("创建word失败");
throw (e);
}
}

public static void createSimpleTable() throws Exception {
XWPFDocument doc = new XWPFDocument();

try {
XWPFTable table = doc.createTable(3, 3);

table.getRow(1).getCell(1).setText("EXAMPLE OF TABLE");

XWPFParagraph p1 = table.getRow(0).getCell(0).getParagraphs().get(0);

XWPFRun r1 = p1.createRun();
r1.setBold(true);
r1.setText("The quick brown fox");
r1.setItalic(true);
r1.setFontFamily("Courier");
r1.setUnderline(UnderlinePatterns.DOT_DOT_DASH);// 下划线
r1.setTextPosition(100);

table.getRow(2).getCell(2).setText("only text");

OutputStream out = new FileOutputStream("D:\\simpleTable.docx");
try {
doc.write(out);
} finally {
out.close();
}
} finally {
doc.close();
}
}

public static void createStyledTable() throws Exception {
XWPFDocument doc = new XWPFDocument();

try {
int nRows = 6;
int nCols = 3;
XWPFTable table = doc.createTable(nRows, nCols);

CTTblPr tblPr = table.getCTTbl().getTblPr();
CTString styleStr = tblPr.addNewTblStyle();
styleStr.setVal("StyledTable");// 目前不知道这是干嘛用的,因为不写这一句,word的样式也没有变化。知道的朋友欢迎交流
tblPr.addNewJc().setVal(STJc.CENTER);// 表格居中
System.out.println("STJc.CENTER:" + STJc.CENTER);
List<XWPFTableRow> rows = table.getRows();
int rowCt = 0;
int colCt = 0;
for (XWPFTableRow row : rows) {// 遍历行
CTTrPr trPr = row.getCtRow().addNewTrPr();
// set row height; units = twentieth of a point, 360 = 0.25"
CTHeight ht = trPr.addNewTrHeight();
ht.setVal(BigInteger.valueOf(360));

List<XWPFTableCell> cells = row.getTableCells();
for (XWPFTableCell cell : cells) {// 便利单元格
CTTcPr tcpr = cell.getCTTc().addNewTcPr();
CTVerticalJc va = tcpr.addNewVAlign();
va.setVal(STVerticalJc.CENTER);
System.out.println("STVerticalJc.CENTER:" + STVerticalJc.CENTER);
CTShd ctshd = tcpr.addNewShd();
ctshd.setColor("auto");
ctshd.setVal(STShd.CLEAR);
if (rowCt == 0) {
ctshd.setFill("A7BFDE");
} else if (rowCt % 2 == 0) {
ctshd.setFill("D3DFEE");
} else {
ctshd.setFill("EDF2F8");
}

XWPFParagraph para = cell.getParagraphs().get(0);
XWPFRun rh = para.createRun();
if (colCt == nCols - 1) {
rh.setFontSize(10);
rh.setFontFamily("Courier");
}
if (rowCt == 0) {
// 表头
rh.setText("header row, col " + colCt);
rh.setBold(true);
para.setAlignment(ParagraphAlignment.CENTER);
} else {
rh.setText("row " + rowCt + ", col " + colCt);
para.setAlignment(ParagraphAlignment.LEFT);
}
colCt++;
}
colCt = 0;
rowCt++;
}

OutputStream out = new FileOutputStream("D:\\styledTable.docx");
try {
doc.write(out);
} finally {
out.close();
}
} finally {
doc.close();
}
}
}