POI3.8操作WORD(动态添加文字)

转: http://www.2cto.com/kf/201208/148382.html

最近由于项目需要实现,在上传的word附件里面动态的添加文字辨识,来区分版本,所以本人做了 一些研究,由于安全性考虑项目都是部署在linux上的,所以一些依赖windows dll组件的实现方案全部被我放弃了,再说了,那些方案也不安全,因为我这是合同系统,有第三方的组件是很危险的.但是 除了dll组件之外的方案,都不尽人意,最好的poi以前只是勉强读取之类的,想要坐到修改里面的内容,并且不影响里面格式,还是挺困难的.经过一番艰难险阻,最终成功找出了一个方案,虽然只是支持2007以上版本,我觉得也够用了 毕竟现在都2012年了,ie6也马上下台了,html5,javascript2,nodejs,nosql,各种技术不断雀跃,我们要是在固步自封,不知道天朝合适能成为天堂,

不扯了,下面是代码:

[java] 
package test; 
 
import java.io.File; 
import java.io.FileOutputStream; 
 
import org.apache.poi.POIXMLDocument; 
import org.apache.poi.openxml4j.opc.OPCPackage; 
import org.apache.poi.xwpf.usermodel.ParagraphAlignment; 
import org.apache.poi.xwpf.usermodel.XWPFDocument; 
import org.apache.poi.xwpf.usermodel.XWPFParagraph; 
import org.apache.poi.xwpf.usermodel.XWPFRun; 
 
public class TestPoi { 
 
    /**
     * 
     * @param args
     */ 
 
    public static void main(String[] args) { 
        try { 
            //获得word的pack对象  
            OPCPackage pack = POIXMLDocument.openPackage("files\\11.docx"); 
            //获得XWPFDocument对象  
            XWPFDocument doc = new XWPFDocument(pack); 
            //输出doc body中包含的元素个数  
            System.out.println(doc.getBodyElements().size()); 
            //输出pack中包含的的part个数,这里就是word文档的页数  
            System.out.println(pack.getParts().size()); 
            //获得第一个段落对象  
            XWPFParagraph paragraph = doc.getParagraphs().get(0); 
            //段落的格式,下面及个设置,将使新添加的文字向左对其,无缩进.  
            paragraph.setIndentationLeft(0); 
            paragraph.setIndentationHanging(0); 
            paragraph.setAlignment(ParagraphAlignment.LEFT); 
            paragraph.setWordWrap(true); 
            //在段落中新插入一个run,这里的run我理解就是一个word文档需要显示的个体,里面可以放文字,参数0代表在段落的最前面插入  
            XWPFRun run = paragraph.insertNewRun(0); 
            //设置run内容  
            run.setText("finish"); 
            run.setFontFamily("宋体"); 
            run.setBold(true); 
            //因为不支持直接保存会原有对象,或者我不会,只能新保存一个文件,然后将原来的删除,将新的文件重命名回原文件名.  
            File oldFile = new File("files\\11.docx"); 
            File newFile = new File("files\\22.docx"); 
            FileOutputStream fos = new FileOutputStream(newFile); 
            doc.write(fos); 
            fos.flush(); 
            fos.close(); 
            pack.close(); 
            System.out.println(oldFile.delete()); 
            System.out.println(newFile.renameTo(oldFile)); 
        } catch (Exception e) { 
            e.printStackTrace(); 
 
        } 
 
    } 
 
} 

package test;

import java.io.File;
import java.io.FileOutputStream;

import org.apache.poi.POIXMLDocument;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

public class TestPoi {

 /**
  *
  * @param args
  */

 public static void main(String[] args) {
  try {
   //获得word的pack对象
   OPCPackage pack = POIXMLDocument.openPackage("files\\11.docx");
   //获得XWPFDocument对象
   XWPFDocument doc = new XWPFDocument(pack);
   //输出doc body中包含的元素个数
   System.out.println(doc.getBodyElements().size());
   //输出pack中包含的的part个数,这里就是word文档的页数
   System.out.println(pack.getParts().size());
   //获得第一个段落对象
   XWPFParagraph paragraph = doc.getParagraphs().get(0);
   //段落的格式,下面及个设置,将使新添加的文字向左对其,无缩进.
   paragraph.setIndentationLeft(0);
   paragraph.setIndentationHanging(0);
   paragraph.setAlignment(ParagraphAlignment.LEFT);
   paragraph.setWordWrap(true);
   //在段落中新插入一个run,这里的run我理解就是一个word文档需要显示的个体,里面可以放文字,参数0代表在段落的最前面插入
   XWPFRun run = paragraph.insertNewRun(0);
   //设置run内容
   run.setText("finish");
   run.setFontFamily("宋体");
   run.setBold(true);
   //因为不支持直接保存会原有对象,或者我不会,只能新保存一个文件,然后将原来的删除,将新的文件重命名回原文件名.
   File oldFile = new File("files\\11.docx");
   File newFile = new File("files\\22.docx");
   FileOutputStream fos = new FileOutputStream(newFile);
   doc.write(fos);
   fos.flush();
   fos.close();
   pack.close();
   System.out.println(oldFile.delete());
   System.out.println(newFile.renameTo(oldFile));
  } catch (Exception e) {
   e.printStackTrace();

  }

 }

}注释写了很明白了,因为poi的文档相当于没有,里面的每个函数,我都是自己试出来的,没办法,不知道为什么他们不维护api,就算是试验阶段,也应该写出来,要不然就扔出来一个jar,谁知道你是干什么的.

代码很简单,就是实现了在word文档的第一行插入"finish"字样,这里是插入不会影响同一行的文字,我没有找到直接保存修改的功能,只能saveas一个新文件,然后重命名回去.如果有童鞋知道怎么实现,还望给我回复.
 

 


作者:qq413041153

猜你喜欢

转载自newerdragon.iteye.com/blog/1675643