JAVA 处理Word模板文件,替换其中的占位符

1.java处理word是个大大的坑,无论是poi还是Java2word 都不是尽善尽美。
2.poi只能进行简单读操作,Java2word需要调用系统的com接口,系统机必须安装office和动态链接库。

3.word从2003版本就可以保存为xml格式,通过操作xml可以满足我们的基本需求。

package com.evaluationinfo.service;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

public class XML2Word {
	/*
	 * 
	 * @Author:Rick
	 * filepath 模板文件路径
	 * tofilepath 要生成的文件路径
	 * items word xml模板文件中的占位符
	 * data  word xml文件要替换的数据
	 */
	  public boolean changeFileText(String filepath,String tofilepath,List items,List data){
		  File file = new File(filepath);
		  String line=null;
		  InputStream is=null;
		  FileOutputStream fos=null;
		  try{
		   is = new FileInputStream(file);
		  StringBuffer  sb=new StringBuffer ();
	      BufferedReader reader = new BufferedReader(new InputStreamReader(is));
	      while((line=reader.readLine()) != null){
	    	  sb.append(line);
	      }
	      String result= String.valueOf(sb);
	      
	      for(int i=0;i


你可能感兴趣的:(Java)