POI往Word中插入字符串--我的实践

代码
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;

ExpandedBlockStart.gifContractedBlock.gif
public   class  POITest  {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** *//**
     * 
@param args
     * 
@throws Exception
     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static void main(String[] args) throws Exception {
        
// TODO Auto-generated method stub
ExpandedSubBlockStart.gifContractedSubBlock.gif
         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中的所有表格
ExpandedSubBlockStart.gifContractedSubBlock.gif
                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++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                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();
                
ExpandedSubBlockStart.gifContractedSubBlock.gif            }
 catch (Exception ex) {
                ex.printStackTrace();
            }


    }

 

}


转载于:https://www.cnblogs.com/monica/archive/2009/11/17/1604703.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值