代码 package lili.com; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.OutputStream; import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.extractor.WordExtractor; import org.apache.poi.hwpf.model.StyleSheet; import org.apache.poi.hwpf.usermodel.CharacterProperties; import org.apache.poi.hwpf.usermodel.CharacterRun; import org.apache.poi.hwpf.usermodel.Paragraph; import org.apache.poi.hwpf.usermodel.Range; import org.apache.poi.hwpf.usermodel.Table; import org.apache.poi.hwpf.usermodel.TableCell; import org.apache.poi.hwpf.usermodel.TableIterator; import org.apache.poi.hwpf.usermodel.TableRow; import org.apache.poi.poifs.filesystem.DirectoryEntry; import org.apache.poi.poifs.filesystem.DocumentEntry; import org.apache.poi.poifs.filesystem.POIFSFileSystem; public class POITest { /** *//** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { // TODO Auto-generated method stub try { FileInputStream in = new FileInputStream(new File("E:\\demo1.doc")); 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(); System.out.println(tb.numRows()); // 遍历表格的行// for (int i = 0; i < tb.numRows(); i++) {// TableRow tr = tb.getRow(i);// // 遍历表格的列// System.out.println(tr.numCells());// for (int j = 0; j < tr.numCells(); j++) {// // 往表格中插入数据// TableCell td = tr.getCell(j);// // CharacterRun chr1= td.getCharacterRun(0);// // System.out.println("td--"+chr1.getFontName());// // String text = "第" + i + "行第" + j + "列";// // int p = td.numParagraphs();// System.out.println("td.numParagraphs :"+p);// for(int a=0;a<p;a++)// {// Paragraph para =td.getParagraph(a);// para.insertBefore(text+" ");// /// para.insertAfter(text);// int q=para.numCharacterRuns();// System.out.println("numCharacterRuns--"+q);// for(int b=0;b<q;b++)// {// CharacterRun chr= para.getCharacterRun(b);// // System.out.println("para.getCharacterRun-- "+ chr.getFontName()+" text--"+chr.text().trim()+" str--"+chr.toString());// // // ParagraphProperties pp = new ParagraphProperties();// // // if(j%2==0){// // // }// }// }// }// } } // WordExtractor we=new WordExtractor(in); String text=range.text(); System.out.println(text); // 在表格外面插入内容 CharacterProperties cp = new CharacterProperties(); cp.setBold(true); cp.setCharacterSpacing(10); cp.setChse(cp.SPRM_CHARSCALE); cp.setCapitalized(true); int p=range.numParagraphs(); for(int i=0;i<p;i++) { String name="飞舞"; String age="23"; Paragraph para=range.getParagraph(i); // System.out.println(p); // para.insertAfter("--插入 成功!!",cp); para.replaceText("姓名:————————————","姓名:"+name); para.replaceText("年龄:————————————","年龄:"+age); CharacterRun chr=para.getCharacterRun(0); System.out.println("第"+i+"段: "+chr.text()); } System.out.println("\n插入后 生成新文档demo1-----------------------------------------\n"); text=range.text(); System.out.println(text); // File outputFile = new File("C:/456.doc");// OutputStream output = new FileOutputStream(outputFile);// hwpf.write(output);// output.close();// // byte b[] = content.getBytes("ISO-8859-1"); // String content="这是固定的字符2"; byte b[] = text.getBytes(); ByteArrayInputStream bais = new ByteArrayInputStream(b); POIFSFileSystem fs = new POIFSFileSystem(); DirectoryEntry directory = fs.getRoot(); DocumentEntry de = directory.createDocument("WordDocument", bais); FileOutputStream ostream = new FileOutputStream("e:/demo.doc"); fs.writeFilesystem(ostream); bais.close(); ostream.close(); } catch (Exception ex) { ex.printStackTrace(); } } } 转载于:https://www.cnblogs.com/monica/archive/2009/11/17/1604703.html