apache poi 修改docx表格_poi操作修改excel、word模板工具

这个工具简化了使用Apache POI修改Excel和Word模板的过程。通过提供便利的方法,可以方便地替换模板中的文本内容和表格数据。示例代码展示了如何修改Excel单元格和Word文档中的文本及表格。
摘要由CSDN通过智能技术生成

这是一个操作excel或者word的工具,个人认为使用起来还是比较方便的,本工具值针对操作文本内容的。

jdk7

poi使用的版本是 3.10-FINAL

先说Excel模板的修改,这是修改前:

431106e64396588adbbbfff2349d4a5f.png

测试代码:

public class TestOfficeUtils {

public static void main(String[] args) throws IOException {

//创建数据

Map data = new HashMap<>();

data.put("name","刘凯");

data.put("age","25");

//获取模板

File file= new File("F:/test.xlsx");

InputStream stream = new FileInputStream(file);

XSSFWorkbook xwb = new XSSFWorkbook(stream);

//修改的模板数据在sheet0里面

OfficeUtils.changeExcelSheet(xwb, data, 0);

//输出到新位置

File fileNew = new File("F:/test_new.xlsx");

FileOutputStream streamOut = new FileOutputStream(fileNew);

xwb.write(streamOut);

stream.close();

}

}

运行后:

49b3819d44590436bd80f561a680e186.png

下面是修word的,,word中的数据修改分为两种,一种操作纯文本的,有一种是操作文档中表格数据的:

运行前:

dfea59cc38cbb365b09f75f2678364fd.png

测试代码:

public static void main(String[] args) throwsIOException {//创建数据

Map data = new HashMap<>();

data.put("name","刘凯");

data.put("age","25");//获取模板

File file= new File("F:/test.docx");

InputStream stream= newFileInputStream(file);

XWPFDocument xwb= newXWPFDocument(stream);//修改word中文本

OfficeUtils.changeText(xwb, data);//修改word中表格

OfficeUtils.changeTables(xwb, data);//输出到新位置

File fileNew = new File("F:/test_new.docx");

FileOutputStream streamOut= newFileOutputStream(fileNew);

xwb.write(streamOut);

stream.close();

}

运行后:

b42d2692e86ccd0561ada7991782954e.png

下面是工具类的代码:FileUtils是我们项目里的工具类,删掉或者替换掉即可:

packagecom.xpsd.cloud.office;importcom.xpsd.cloud.commons.utils.FileUtils;importorg.apache.poi.POIXMLDocument;importorg.apache.poi.xssf.usermodel.XSSFSheet;importorg.apache.poi.xssf.usermodel.XSSFWorkbook;import org.apache.poi.xwpf.usermodel.*;importjava.io.File;importjava.io.FileOutputStream;importjava.io.IOException;importjava.util.List;importjava.util.Map;importjava.util.Set;/*** Office操作工具

* @LewKAY

* 20180410*/

public classOfficeUtils {/*** 修改Doc中文本域内对象

* 处理对象${name}类型*/

public static void changeText(XWPFDocument document, Mapmap) {

List paragraphs =document.getParagraphs();for(XWPFParagraph paragraph : paragraphs) {//判断此段落时候需要进行替换

String text =paragraph.getText();if(checkText(text)) {

List runs =paragraph.getRuns();for(XWPFRun run : runs) {

run.setText(changeValue(run.toString(), map),0);

}

}

}

}/*** 修改Doc中Tables text对象

* 处理对象${name}类型

*

*@paramdocument*/

public static voidchangeTables(XWPFDocument document, Map map) {

List tables =document.getTables();for (int i = 0; i < tables.size(); i++) {

XWPFTable table=tables.get(i);if (table.getRows().size() >= 1) {if(checkText(table.getText())) {

List rows =table.getRows();

eachTable(rows, map);

}

}

}

}/*** 遍历表格*/

public static void eachTable(List rows, MaptextMap) {for(XWPFTableRow row : rows) {

List cells =row.getTableCells();for(XWPFTableCell cell : cells) {//判断单元格是否需要替换

if(checkText(cell.getText())) {

List paragraphs =cell.getParagraphs();for(XWPFParagraph paragraph : paragraphs) {

List runs =paragraph.getRuns();for(XWPFRun run : runs) {

run.setText(changeValue(run.toString(), textMap),0);

}

}

}

}

}

}/*** 匹配传入信息集合与模板

*

*@paramvalue 模板需要替换的区域*/

public static String changeValue(String value, MaptextMap) {

value.trim();

Set> textSets =textMap.entrySet();for (Map.EntrytextSet : textSets) {//匹配模板与替换值 格式${key}

String key = "${" + textSet.getKey() + "}";if (value.indexOf(key) != -1) {

value=textSet.getValue();

}

}//模板未匹配到区域替换为空

if(checkText(value)) {

value= "";

}returnvalue;

}/*** 判断文本中时候包含$*/

public static booleancheckText(String text) {boolean check = false;if (text.indexOf("$") != -1) {

check= true;

}returncheck;

}public static void createOfficeFile(POIXMLDocument source, String dirs, String fileName) throwsIOException {

FileUtils.createDirectory(dirs);

File file= new File(dirs +fileName);

FileOutputStream stream= newFileOutputStream(file);

source.write(stream);

stream.close();

}/*** 修改Excell 值*/

public static void changeExcelSheet(XSSFWorkbook xwb, Map data, intsheet) {

XSSFSheet xSheet=xwb.getSheetAt(sheet);int begin =xSheet.getFirstRowNum();int end =xSheet.getLastRowNum();for (int i = begin; i <= end; i++) {if (null ==xSheet.getRow(i)) {continue;

}int beginCell =xSheet.getRow(i).getFirstCellNum();int endCell =xSheet.getRow(i).getLastCellNum();for (int j = beginCell; j <= endCell; j++) {if (xSheet.getRow(i).getCell(j) == null) {continue;

}

String value=changeValue(xSheet.getRow(i).getCell(j).getStringCellValue(), data);if(!value.equals("")){

xSheet.getRow(i).getCell(j).setCellValue(value);

}

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值