(4) POI往Word插入字符串

时间:2022-05-12 21:40:56

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