java poi word 实例_Java POI Word 写文档(示例代码)

package apache.poi;

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStream;

import java.util.HashMap;

import java.util.Map;

import org.apache.poi.hwpf.HWPFDocument;

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

import org.apache.poi.poifs.filesystem.DirectoryEntry;

import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class ExportDocTest {

public static void main(String[] args) {

String destFile="D:\\11.doc";

//#####################根据自定义内容导出Word文档#################################################

StringBuffer fileCon=new StringBuffer();

fileCon.append(" 张大炮 男 317258963215223\n" +

"2011 09 2013 07 3\n" +

" 二炮研究 成人\n" +

"2013000001 2013 07 08");

fileCon.append("\n\r\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");

new ExportDocTest().exportDoc(destFile, fileCon.toString());

//##################根据Word模板导出单个Word文档###################################################

Map map=new HashMap();

map.put("name", "Zues");

map.put("sex", "男");

map.put("idCard", "200010");

map.put("year1", "2000");

map.put("month1", "07");

map.put("year2", "2008");

map.put("month2", "07");

map.put("gap", "2");

map.put("zhuanye", "计算机科学与技术");

map.put("type", "研究生");

map.put("bianhao", "2011020301");

map.put("nowy", "2011");

map.put("nowm", "01");

map.put("nowd", "20220301");

//注意biyezheng_moban.doc文档位置,此例中为应用根目录

HWPFDocument document=new ExportDocTest().replaceDoc("biyezheng_moban.doc", map);

ByteArrayOutputStream ostream = new ByteArrayOutputStream();

try {

document.write(ostream);

//输出word文件

OutputStream outs=new FileOutputStream(destFile);

outs.write(ostream.toByteArray());

outs.close();

} catch (IOException e) {

e.printStackTrace();

}

}

/**

*

* @param destFile

* @param fileCon

*/

public void exportDoc(String destFile,String fileCon){

try {

//doc content

ByteArrayInputStream bais = new ByteArrayInputStream(fileCon.getBytes());

POIFSFileSystem fs = new POIFSFileSystem();

DirectoryEntry directory = fs.getRoot();

directory.createDocument("WordDocument", bais);

FileOutputStream ostream = new FileOutputStream(destFile);

fs.writeFilesystem(ostream);

bais.close();

ostream.close();

} catch (IOException e) {

e.printStackTrace();

}

}

/**

* 读取word模板并替换变量

* @param srcPath

* @param map

* @return

*/

public HWPFDocument replaceDoc(String srcPath, Map map) {

try {

// 读取word模板

FileInputStream fis = new FileInputStream(new File(srcPath));

HWPFDocument doc = new HWPFDocument(fis);

// 读取word文本内容

Range bodyRange = doc.getRange();

// 替换文本内容

for (Map.Entry entry : map.entrySet()) {

bodyRange.replaceText("${" + entry.getKey() + "}", entry

.getValue());

}

return doc;

} catch (Exception e) {

e.printStackTrace();

return null;

}

}

/**

* 操作表格

*/

public void OperateTable(){

try {

POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(

"D:\\XBRL.doc"));

HWPFDocument doc = new HWPFDocument(fs);

Range range = doc.getOverallRange();

TableIterator ti = new TableIterator(range);

//            Table table = null;

while (ti.hasNext()) {

System.out.println("Getting table!");

Table table = (Table)ti.next();

System.out.println("Number of rows: " + table.numRows());

for (int a = 0; a 

TableRow row = table.getRow(a);

System.out.println("\tTable row number: " + a);

for (int b = 0; b 

System.out.println("\t\tTable cell number: " + b);

TableCell tablecell = row.getCell(b);

for (int c = 0; c 

Paragraph tablepara = tablecell.getParagraph(c);

for (int d = 0; d 

CharacterRun run = tablepara.getCharacterRun(d);

System.out.println("\t\tText: \"" + run.text()

+ "\"");

}

}

}

}

System.out.println("\n");

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值