62,614
社区成员
发帖
与我相关
我的任务
分享
public void testWord(String fileString,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 = "第" + i + "行第" + j + "列";
int p = td.numParagraphs();
Paragraph para = td.getParagraph(p);
ParagraphProperties pp = new ParagraphProperties();
System.out.println(para.);
//if(para.){
para.insertBefore(text);
//}
}
}
}
// 在表格外面插入内容
CharacterProperties cp = new CharacterProperties();
cp.setBold(true);
cp.setCharacterSpacing(10);
cp.setChse(cp.SPRM_CHARSCALE);
cp.setCapitalized(true);
//int p = range.numParagraphs();
//Paragraph para = range.getParagraph(p);
//para.insertAfter("test poi successful!", cp);
//para.insertAfter("测试成功", cp);
File outputFile = new File(newPath);
OutputStream output = new FileOutputStream(outputFile);
hwpf.write(output);
output.close();
} catch (Exception ex) {
ex.printStackTrace();
}