我使用Apache POI从Java生成.docx文件。 我的桌子上有像......一样的列。 table without any entry 我使用以下代码在第二行内写入内容。
String itemNames[] = {“Item 1”, “Item Name 2”, "Item 3"};
for(int i=0; i<3; i++) {
//getRow(1) represents second row of the table
//printing sr. no
paragraph = table.getRow(1).getCell(0).addParagraph();
paragraph.setAlignment(ParagraphAlignment.CENTER);
paragraph.setVerticalAlignment(TextAlignment.CENTER);
run = paragraph.createRun();
run.setText(i+1);
//printing item names
paragraph = table.getRow(1).getCell(1).addParagraph();
paragraph.setAlignment(ParagraphAlignment.CENTER);
paragraph.setVerticalAlignment(TextAlignment.CENTER);
run = paragraph.createRun();
run.setText(itemNames[i]);
//printing qty
paragraph = table.getRow(1).getCell(2).addParagraph();
paragraph.setAlignment(ParagraphAlignment.CENTER);
paragraph.setVerticalAlignment(TextAlignment.CENTER);
run = paragraph.createRun();
run.setText(1 + (i*10));
}
即生成如下输出。 Error picture 在这里,正如您所看到的,第2行的项目名称不能写入1行,因为单元格宽度小于内容。因此,有自动换行设置,以便该行完成2行写入。但是,其余内容只需要1行。
因此,当代码打印第三个条目时,它会在sr no,item name和qty中创建一个新段落并写入值。但是,正如您在图像中看到的那样,输出不是应该的。
我试图在段落和运行中获得行数,但我无法得到实际数字,因为项目名称更大,我想在其他列中添加中断以保存它的正确形式。但是如何找到第2行中单元格2的每次运行的行号?
答案 0 :(得分:1)
我要做的是将表格第2行的内容写入已存在的行,然后针对每个更多内容行,使用XWPFTable.createRow创建新的XWPFTableRow
。
示例:
WordTableExample.docx
中的第一个表:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.*;
public class WordInsertTableRows {
public static void main(String[] args) throws Exception {
XWPFDocument doc = new XWPFDocument(new FileInputStream("WordTableExample.docx"));
XWPFTable table = doc.getTableArray(0);
XWPFTableRow row;
XWPFParagraph paragraph;
XWPFRun run;
String itemNames[] = {"Item 1", "Item Name 2", "Item 3"};
for(int i=0; i<itemNames.length; i++) {
row = table.getRow(1+i); //getRow(1) represents second row of the table
if (row == null) row = table.createRow(); //if there is not a row already, then create one
//printing sr. no
paragraph = row.getCell(0).getParagraphArray(0);
if (paragraph == null) paragraph = row.getCell(0).addParagraph();
paragraph.setAlignment(ParagraphAlignment.CENTER);
//paragraph.setVerticalAlignment(TextAlignment.CENTER); //this sets valign for the paragraph
row.getCell(0).setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); //I suspect you wants set valign for the cell
run = paragraph.createRun();
run.setText(""+(i+1));
//printing item names
paragraph = row.getCell(1).getParagraphArray(0);
if (paragraph == null) paragraph = row.getCell(1).addParagraph();
paragraph.setAlignment(ParagraphAlignment.CENTER);
//paragraph.setVerticalAlignment(TextAlignment.CENTER);
row.getCell(1).setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
run = paragraph.createRun();
run.setText(itemNames[i]);
//printing qty
paragraph = row.getCell(2).getParagraphArray(0);
if (paragraph == null) paragraph = row.getCell(2).addParagraph();
//paragraph.setVerticalAlignment(TextAlignment.CENTER);
row.getCell(2).setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
run = paragraph.createRun();
run.setText(""+(1 + (i*10)));
}
doc.write(new FileOutputStream("WordTableExampleNew.docx"));
doc.close();
}
}