java读写wordjava动态写入模板文件

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

importjava.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.HashMap;

import java.util.Iterator;

import java.util.Map;

import org.apache.poi.hwpf.HWPFDocument;

import org.apache.poi.hwpf.model.FieldsDocumentPart;

import org.apache.poi.hwpf.usermodel.Field;

import org.apache.poi.hwpf.usermodel.Fields;

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;/**

* @author zhangchaochao

* @date 2015-12-4上午10:30:59*/publicclassMSWordPoi4

{/**

* @param args*/publicstaticvoidmain(String[] args)

{

Map<String, String> map =newHashMap<String, String>();

map.put("${sub}","湖南大学");

map.put("${item2.school}","湖南大学");

map.put("${item2.address}","湖南");//map.put("${item1.name}", "王五1");//map.put("${item1.numberStudent}", "编号002");//map.put("${item1.sex}", "男2");//map.put("${item1.age}", "19");String srcPath ="D:\\铸造工艺卡.doc";

readwriteWord(srcPath, map);

}/**

* 实现对word读取和修改操作

*

* @param filePath

* word模板路径和名称

* @param map

* 待填充的数据,从数据库读取*/publicstaticvoidreadwriteWord(String filePath, Map<String, String>map)

{//读取word模板//String fileDir = new//File(base.getFile(),"http://www.cnblogs.com/http://www.cnblogs.com/../doc/").getCanonicalPath();FileInputStreamin=null;try{in=newFileInputStream(newFile(filePath));

}catch(FileNotFoundException e1)

{

e1.PRintStackTrace();

}

HWPFDocument hdt=null;try{

hdt=newHWPFDocument(in);

}catch(IOException e1)

{

e1.printStackTrace();

}

Fields fields=hdt.getFields();

Iterator<Field> it =fields.getFields(FieldsDocumentPart.MAIN)

.iterator();while(it.hasNext())

{

System.out.println(it.next().getType());

}//读取word文本内容Range range =hdt.getRange();

TableIterator tableIt=newTableIterator(range);//迭代文档中的表格intii =0;while(tableIt.hasNext()) {

Table tb=(Table) tableIt.next();

ii++;

System.out.println("第"+ii+"个表格数据...................");//迭代行,默认从0开始for(inti =0; i < tb.numRows(); i++) {

TableRow tr=tb.getRow(i);//只读前8行,标题部分if(i >=8)

break;//迭代列,默认从0开始for(intj =0; j < tr.numCells(); j++) {

TableCell td= tr.getCell(j);//取得单元格//取得单元格的内容for(intk=0;k<td.numParagraphs();k++){

Paragraph para=td.getParagraph(k);

String s=para.text();

System.out.println(s);

}//end for}//end for}//end for}//end while//System.out.println(range.text());//替换文本内容for(Map.Entry<String, String>entry : map.entrySet())

{

range.replaceText(entry.getKey(), entry.getValue());

}

ByteArrayOutputStream ostream=newByteArrayOutputStream();

String fileName=""+System.currentTimeMillis();

fileName+=".doc";

FileOutputStreamout=null;try{out=newFileOutputStream("D:/"+ fileName,true);

}catch(FileNotFoundException e)

{

e.printStackTrace();

}try{

hdt.write(ostream);

}catch(IOException e)

{

e.printStackTrace();

}//输出字节流try{out.write(ostream.toByteArray());

}catch(IOException e)

{

e.printStackTrace();

}try{out.close();

}catch(IOException e)

{

e.printStackTrace();

}try{

ostream.close();

}catch(IOException e)

{

e.printStackTrace();

}

}

}

java 读写word java 动态写入 模板文件下载地址

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航