62,615
社区成员
发帖
与我相关
我的任务
分享
public void testWord(String file,String newPath) {
//File file = new File(fileString);
try {
FileInputStream in = new FileInputStream(file);
POIFSFileSystem pfs = new POIFSFileSystem(in);
HWPFDocument hwpf = new HWPFDocument(pfs);
Range range = hwpf.getRange();
StyleSheet styleSheet = hwpf.getStyleSheet();
TableIterator it = new TableIterator(range);
// 遍历一个DOC中的所有表格
while (it.hasNext()) {
Table tb = (Table) it.next();
// 遍历表格的行
for (int i = 0; i < tb.numRows(); i++) {
TableRow tr = tb.getRow(i);
// 遍历表格的列
for (int j = 0; j < tr.numCells(); j++) {
// 往表格中插入数据
TableCell td = tr.getCell(j);
String text = "哈哈,输入中文";
int p = td.numParagraphs();
Paragraph para = td.getParagraph(p);
ParagraphProperties pp = new ParagraphProperties();
if(j%2==0){
para.insertBefore(text);
}
}
}
}
// 在表格外面插入内容
CharacterProperties cp = new CharacterProperties();
cp.setBold(true);
cp.setCharacterSpacing(10);
cp.setChse(cp.SPRM_CHARSCALE);
cp.setCapitalized(true);
File outputFile = new File(newPath);
OutputStream output = new FileOutputStream(outputFile);
hwpf.write(output);
output.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}