本文介绍了java Apache POI Word 现有表格插入行,带有单元格样式和格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 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);
        }
    }

感谢您的回答.R.

推荐答案

以下代码获取文档中包含的第一个表中第二行的精确副本.然后它更改此行中单元格的文本内容.然后在这个表的第 2 行和第 3 行之间插入这个复制的行.

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.

更改内容必须在 table.addRow 之前完成,因为在将行插入 列出 tableRows 并将其添加到 CTTbl ctTbl.以后的更改不会写入 XML.我真的不明白为什么会这样.

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.

然后代码获取最后一行的副本并将此副本添加到表的末尾.这里也必须在table.addRow之前更改内容.

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

 }
}

这篇关于java Apache POI Word 现有表格插入行,带有单元格样式和格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 12:15