POI操作WORD文档

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();

}

}

转载于:https://www.cnblogs.com/hotMemo/p/10233614.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Apache POI 是一个 Java API,可以用来操作 Microsoft Office 格式的文件,包括 Word、Excel 和 PowerPoint 等。在本文中,我们将重点介绍如何使用 Apache POI 操作 Word 文档。 1. 添加 Maven 依赖 首先,我们需要在项目中添加 Apache POI 的依赖。在 pom.xml 文件中添加以下代码: ```xml <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> ``` 2. 创建 Word 文档 要创建一个新的 Word 文档,我们需要使用 XWPFDocument 类。以下是创建一个空白文档的示例代码: ```java XWPFDocument document = new XWPFDocument(); ``` 3. 添加段落 要向文档中添加段落,我们需要使用 XWPFParagraph 类。以下是向文档中添加一个段落的示例代码: ```java XWPFParagraph paragraph = document.createParagraph(); paragraph.setAlignment(ParagraphAlignment.CENTER); XWPFRun run = paragraph.createRun(); run.setText("Hello, World!"); ``` 4. 添加表格 要向文档中添加表格,我们需要使用 XWPFTable 类。以下是向文档中添加一个表格的示例代码: ```java XWPFTable table = document.createTable(); XWPFTableRow row = table.getRow(0); row.getCell(0).setText("Name"); row.addNewTableCell().setText("Age"); row.addNewTableCell().setText("Gender"); XWPFTableRow row1 = table.createRow(); row1.getCell(0).setText("Tom"); row1.getCell(1).setText("25"); row1.getCell(2).setText("Male"); ``` 5. 保存文档 要保存文档,我们需要使用 FileOutputStream 类将文档写入文件中。以下是将文档保存为名为“example.docx”的文件的示例代码: ```java FileOutputStream out = new FileOutputStream("example.docx"); document.write(out); out.close(); document.close(); ``` 完整的示例代码: ```java import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.xwpf.usermodel.*; public class WordTest { public static void main(String[] args) throws IOException { XWPFDocument document = new XWPFDocument(); // 添加段落 XWPFParagraph paragraph = document.createParagraph(); paragraph.setAlignment(ParagraphAlignment.CENTER); XWPFRun run = paragraph.createRun(); run.setText("Hello, World!"); // 添加表格 XWPFTable table = document.createTable(); XWPFTableRow row = table.getRow(0); row.getCell(0).setText("Name"); row.addNewTableCell().setText("Age"); row.addNewTableCell().setText("Gender"); XWPFTableRow row1 = table.createRow(); row1.getCell(0).setText("Tom"); row1.getCell(1).setText("25"); row1.getCell(2).setText("Male"); // 保存文档 FileOutputStream out = new FileOutputStream("example.docx"); document.write(out); out.close(); document.close(); } } ``` 这是一个简单的示例,介绍了如何使用 Apache POI 操作 Word 文档。您可以进一步研究该 API,以了解更多高级功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值