首页 > 技术文章 > POI操作WORD文档

hotMemo 2019-01-07 15:40 原文

package com.iuit.test.file;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.wp.usermodel.Paragraph;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.junit.Test;

public class FileTest {

/**
* 文档模板 将${xxxx}替换
* @throws IOException
*/
@Test
public void test1() throws IOException {

//准备工作,生成docx对象
String templatePath="D:\\tmp\\sample.docx";
InputStream is=new FileInputStream(templatePath);
XWPFDocument docx=new XWPFDocument(is);



//获取所有的段
List<XWPFParagraph> paragraphs=docx.getParagraphs();

//遍历所有的段
for(int i=0;i<paragraphs.size();i++) {

//获取该段所有的文本对象
List<XWPFRun> runs=paragraphs.get(i).getRuns();
for(int j=0;j<runs.size();j++) {
XWPFRun run=runs.get(j);
//假如该文本对象里包含${name},则使用run.setText(str.replace("${name}","fucker"),0)替换
if(run.toString().contains("${name}")) {
String str=run.toString();
run.setText(str.replace("${name}", "fucker"),0);
}

}

}
//输出到write.docx
OutputStream os=new FileOutputStream("D:\\tmp\\write.docx");
docx.write(os);
is.close();
os.close();

}

/**
* add table
* @author dwg
* 操作表格,增加表格行
*/
@Test
public void test2() throws IOException{

//准备工作,生成docx对象
String templatePath="D:\\tmp\\sample.docx";
InputStream is=new FileInputStream(templatePath);
XWPFDocument docx=new XWPFDocument(is);

//获取所有的Table
List<XWPFTable> tables=docx.getTables();
//定位到第一个Table
XWPFTable table=tables.get(0);
//Table增加一行
table.createRow();
//定位到新新加的行,一开始只有两行,一加变成三行,定位为2
XWPFTableRow row=table.getRow(2);
//添加内容
row.getCell(0).setText("1111");

//输出
OutputStream os=new FileOutputStream("D:\\tmp\\write.docx");
docx.write(os);
is.close();
os.close();
}

/**
*
* 给表格中${xxx}替换为需要的文本
*/
@Test
public void test3() throws IOException {

//准备工作,生成docx对象
String templatePath="D:\\tmp\\sample.docx";
InputStream is=new FileInputStream(templatePath);
XWPFDocument docx=new XWPFDocument(is);

//获取表格
List<XWPFTable> tables=docx.getTables();
//定位到第一个表格
XWPFTable table=tables.get(0);
//遍历该表格所有的行
for(int i=0;i<table.getRows().size();i++) {
XWPFTableRow row=table.getRow(i);
//遍历该行所有的列
for(int j=0;j<row.getTableCells().size();j++) {
XWPFTableCell cell=row.getTableCells().get(j);
//获取该格子里所有的段
List<XWPFParagraph> paragraphs=cell.getParagraphs();
for(XWPFParagraph p:paragraphs) {
//遍历该格子里的段
List<XWPFRun> runs=p.getRuns();
for(XWPFRun run:runs) {
//遍历该段里的所有文本
String str=run.toString();
//如果该段文本包含${like},则替换
if(str.equals("${like}")) {
run.setText("fuck",0);
}
}

}

}

}

//输出
OutputStream os=new FileOutputStream("D:\\tmp\\write.docx");
docx.write(os);
is.close();
os.close();

}

}

推荐阅读