java 操作 word 表格和样式,java Apache POI Word现有表插入具有单元格样式和格式的行...

I'am a beginner in Apache POI and want to extend an existing table in the word template file with some rows.

If I use that code below, the table will be extended with row bit the new row's cells created normal style.

My goal is the cells have the same table cells style etc (font, with, height...)

XWPFDocument doc = new XWPFDocument(openFile(fileName));

XWPFTable tbl = doc.getTableArray(tableIndex);

XWPFTableRow lastRow = tbl.getRows().get(tbl.getNumberOfRows() - 1);

cellCounter = lastRow.getTableICells().size();

XWPFTableRow newRow = tbl.createRow();

for (int i = 0; i < data.size() && cellCounter <= data.size(); i++) {

String text = data.get(i);

XWPFTableCell cell = newRow.getCell(i);

if (cell != null) {

cell.setText(text);

}

}

Thanks for your answer.

R.

解决方案

The following code gets an exact copy of the second row in first table contained in document. Then it changes the text contents of the cells in this row. And then inserts this copied row between row 2 and 3 of this table.

The changing the content must be done before table.addRow since the row must be complete before inserting it in List tableRows and adding it to the TrArray of the CTTbl ctTbl. Later changings will not be written into the XML. I've not really got the reason why this is the case.

Then the code gets a copy of the last row and adds this copy at the end of the table. Here also the changing the content must be done before table.addRow.

import java.io.*;

import org.apache.poi.xwpf.usermodel.*;

import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow;

public class WordInsertTableRow {

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

XWPFDocument doc = new XWPFDocument(new FileInputStream("source.docx"));

XWPFTable table = doc.getTableArray(0);

//insert new row, which is a copy of row 2, as new row 3:

XWPFTableRow oldRow = table.getRow(1);

CTRow ctrow = CTRow.Factory.parse(oldRow.getCtRow().newInputStream());

XWPFTableRow newRow = new XWPFTableRow(ctrow, table);

int i = 1;

for (XWPFTableCell cell : newRow.getTableCells()) {

for (XWPFParagraph paragraph : cell.getParagraphs()) {

for (XWPFRun run : paragraph.getRuns()) {

run.setText("New row 3 cell " + i++, 0);

}

}

}

table.addRow(newRow, 2);

//insert new last row, which is a copy previous last row:

XWPFTableRow lastRow = table.getRows().get(table.getNumberOfRows() - 1);

ctrow = CTRow.Factory.parse(lastRow.getCtRow().newInputStream());

newRow = new XWPFTableRow(ctrow, table);

i = 1;

for (XWPFTableCell cell : newRow.getTableCells()) {

for (XWPFParagraph paragraph : cell.getParagraphs()) {

for (XWPFRun run : paragraph.getRuns()) {

run.setText("New last row cell " + i++, 0);

}

}

}

table.addRow(newRow);

doc.write(new FileOutputStream("result.docx"));

doc.close();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用JavaPOI操作.doc Word模板替换并循环插入表格的步骤如下: 1. 导入POI库的相关依赖,例如Apache POIApache POI-OOXML。 2. 创建一个Word文档(.docx),该文档将作为模板使用。 3. 使用Apache POI的XWPFDocument类加载模板文档。 ```java XWPFDocument doc = new XWPFDocument(new FileInputStream("template.docx")); ``` 4. 声明一个XWPFTable对象,用于向文档中插入表格。 ```java XWPFTable table; ``` 5. 使用XWPFDocument类的getTables()方法获取文档中的所有表格,通常一个模板只有一个表格。 ```java List<XWPFTable> tables = doc.getTables(); table = tables.get(0); // 假设我们要操作表格是第一个表格 ``` 6. 使用XWPFTable对象的removeRow()方法删除表格中的所有,这样就可以将模板中的删除,以便后面插入新的。 ```java for (int i = table.getRows().size() - 1; i > 0; i--) { table.removeRow(i); } ``` 7. 使用XWPFTable对象的createRow()方法创建新的,并使用XWPFTableRow对象的createCell()方法创建单元格。 ```java for (int i = 0; i < data.size(); i++) { XWPFTableRow newRow = table.createRow(); // 将data中的数据添加到新单元格中 for (int j = 0; j < data.get(i).size(); j++) { XWPFTableCell newCell = newRow.getCell(j); newCell.setText(data.get(i).get(j)); } } ``` 8. 将替换完表格的文档保存为新的文档。 ```java FileOutputStream out = new FileOutputStream("output.docx"); doc.write(out); out.close(); ``` 这样,你就可以使用JavaPOI操作.doc Word模板,替换表格并循环插入新的表格了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值