首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >XWPFTable创建新行(循环)

XWPFTable创建新行(循环)
EN

Stack Overflow用户
提问于 2022-05-29 09:23:09
回答 1查看 279关注 0票数 0

关于如何使用XWPFTable循环数据,我有一个问题?我有一些问题;

  1. 我不明白为什么XWPFRun examRun = examInfoRowP.createRun();(自动读取第二列,而不是第一列第一)
  2. 我的编码结构,我认为它在使用XWPFTable的效率较低,如何使它更干净的代码

注意: List listLHA是我使用Apache-poi3.8的数据

代码语言:javascript
运行
复制
XWPFTable Table = document.getTableArray(16);
            
            XWPFTableRow getRow1 = Table.getRow(1);
            
            
            XWPFTableRow getRow0 = Table.getRow(0);
            
            //baris 1
            for(int i = 0; i < listLHA.size(); i++) {
                getRow0.getCell(0).setText(listLHA.get(0).getKeyProsses()+ " KEY PROSES");
                break;
            }
            
            //baris 2
            
            for(int i = 0; i < listLHA.size(); i++) {
                getRow1.getCell(0).setText(listLHA.get(0).getRiskRating());
                getRow1.getCell(1).setText(listLHA.get(0).getAuditObservationTitle()+ " AO TITLE");
                break;
            }
            
            XWPFTableRow examInfoRow = Table.createRow();
            XWPFTableCell cellRowInfo = examInfoRow.addNewTableCell();

            XWPFParagraph examInfoRowP = cellRowInfo.getParagraphs().get(0);
            XWPFRun examRun = examInfoRowP.createRun(); //problem 1
            
            examInfoRowP.setAlignment(ParagraphAlignment.LEFT);
            //list Action plan
            examRun.setText("Action Plan:");
            examRun.addBreak();
            for (AuditEngagementLHA lha : listLHA) {
                int i = listLHA.indexOf(lha);
                examRun.setText(i+1 +"."+lha.getDescAP().replaceAll("\\<[^>]*>",""));
                examRun.addBreak();
            }
            for(int i = 0; i < listLHA.size(); i++) {
                examRun.setText("Target Date: ");
                examRun.setText(listLHA.get(0).getTargetDateAP());
                examRun.addBreak();
                break;
            }
            
            examRun.addBreak();
            for(int i = 0; i < listLHA.size(); i++) {
                examInfoRow.getCell(0).setText(listLHA.get(0).getDescAO()+" Desc AO");
                examRun.addBreak();
                break;
            }
            //List penanggung jawab
            examRun.setText("Penanggung Jawab:");
            examRun.addBreak();
            for (AuditEngagementLHA lha : listLHA) {
                int i = listLHA.indexOf(lha);
                
                examRun.setText(i+1 +"."+lha.getPicAP()+" - ");
                examRun.setText(lha.getJabatanPicAP());
                examRun.addBreak();
            }

*我的字模是这样的

*从现在开始,结果是这样的

*结果应该是这样的和整洁的

EN

Stack Overflow用户

回答已采纳

发布于 2022-05-29 15:29:53

Apache正在开发中。因此,版本很快就过时了。apache poi 3.8版本于2012年发布。十年后的2022年,它太旧了,无法使用。

关于你的第一个问题:

根据文档,XWPFTable.createRow创建了一个新的XWPFTableRow对象,该对象的单元格数与当时定义的列数相同。

因此,在您的示例中,XWPFTableRow examInfoRow = Table.createRow();创建了一个至少已经有两列的examInfoRow,因为表中至少包含了来自上面的行的两列。

然后,XWPFTableCell cellRowInfo = examInfoRow.addNewTableCell();通过向该行添加一个新的表单元格来添加一个额外的列。这就是为什么cellRowInfo不会出现在第一栏的原因。

你的第二个问题太宽泛了,无法在这里完全回答。

我能告诉你的是,单词表是可怕的东西。它们有一个定义列的底层表格网格。如果行需要不同的列大小,那么设置列跨度是必要的,就像在HTML表中一样。表包含三列,显示为想要的结果。在第一行中,第一列跨越其他列。在第二行中,第二列跨越第三列。在第三行中,第一列跨越第二列。这是为每一行生成这些不同列的唯一方法。

了解这一点应该避免在插入和/或添加或创建表单元格时出现混乱。如果模板是可能的,那么该模板应该已经包含了所有可能的行模板。然后,只有复制行是必要的,而不是混乱插入单元格。

完整的例子:

模板:

代码:

代码语言:javascript
运行
复制
import java.io.FileOutputStream;
import java.io.FileInputStream;

import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow;

import java.util.List; 
import java.util.ArrayList; 

public class WordInsertContentInTable {
    
 static void setText(XWPFTableCell cell, String text) {
  String[] lines = text.split("\n");
  List<XWPFParagraph> paragraphs = cell.getParagraphs();
  for (int i = 0; i < lines.length; i++) {
   String line = lines[i];
   XWPFParagraph paragraph = null;
   if (paragraphs.size() > i) paragraph = paragraphs.get(i);
   if (paragraph == null) paragraph = cell.addParagraph();
   XWPFRun run = null;
   if (paragraph.getRuns().size() > 0) run = paragraph.getRuns().get(0);
   if (run == null) run = paragraph.createRun();
   run.setText(line, 0);
  }
  for (int i = paragraphs.size()-1; i >= lines.length; i--) {
   cell.removeParagraph(i);
  }
 }
    
 static void insertContentInTable(XWPFTable table, List<POJO> listOfPOJOs) throws Exception {
  XWPFTableRow titleRowTemplate = table.getRow(0);
  if (titleRowTemplate == null) throw new Exception("Table template does not match: No title row.");
  if (titleRowTemplate.getTableCells().size() != 1) throw new Exception("Table template does not match: Wrong title row column count.");
  XWPFTableRow subTitleRowTemplate = table.getRow(1);
  if (subTitleRowTemplate == null) throw new Exception("Table template does not match: No sub title row.");
  if (subTitleRowTemplate.getTableCells().size() != 2) throw new Exception("Table template does not match: Wrong sub title row column count.");
  XWPFTableRow contentRowTemplate = table.getRow(2);
  if (contentRowTemplate == null) throw new Exception("Table template does not match: No content row.");
  if (contentRowTemplate.getTableCells().size() != 2) throw new Exception("Table template does not match: Wrong content row column count.");
  
  XWPFTableRow titleRow = titleRowTemplate;
  XWPFTableRow subTitleRow = subTitleRowTemplate;
  XWPFTableRow contentRow = contentRowTemplate;
  XWPFTableCell cell;
  for (int i = 0; i < listOfPOJOs.size(); i++) {
   POJO pojo = listOfPOJOs.get(i); 
   if (i > 0) {
    titleRow = new XWPFTableRow((CTRow)titleRowTemplate.getCtRow().copy(), table);
    subTitleRow = new XWPFTableRow((CTRow)subTitleRowTemplate.getCtRow().copy(), table);
    contentRow = new XWPFTableRow((CTRow)contentRowTemplate.getCtRow().copy(), table);
   }
   String titleRowText = pojo.getTitleRowText();
   cell = titleRow.getCell(0);
   setText(cell, titleRowText);   
   String subTitleRowLeftText = pojo.getSubTitleRowLeftText();   
   String subTitleRowLeftColor = pojo.getSubTitleRowLeftColor();   
   String subTitleRowRightText = pojo.getSubTitleRowRightText();   
   cell = subTitleRow.getCell(0);
   setText(cell,subTitleRowLeftText);   
   cell.setColor(subTitleRowLeftColor);   
   cell = subTitleRow.getCell(1);
   setText(cell,subTitleRowRightText);
   String contentRowLeftText = pojo.getContentRowLeftText();   
   String contentRowRightText = pojo.getContentRowRightText();   
   cell = contentRow.getCell(0);
   setText(cell, contentRowLeftText);   
   cell = contentRow.getCell(1);
   setText(cell, contentRowRightText);
   if (i > 0) {
    table.addRow(titleRow);
    table.addRow(subTitleRow);
    table.addRow(contentRow);
   }
  }     
 }

 public static void main(String[] args) throws Exception {
     
  List<POJO> listOfPOJOs = new ArrayList<POJO>();
  listOfPOJOs.add(new POJO("Title row text 1", 
                           "Sub title row left text 1", "FF0000", "Sub title row right text 1\nSub title row right text 1\nSub title row right text 1", 
                           "Content row left text 1\nContent row left text 1\nContent row left text 1", 
                           "Content row right text 1\nContent row right text 1\nContent row right text 1"));
  listOfPOJOs.add(new POJO("Title row text 2", 
                           "Sub title row left text 2", "00FF00", "Sub title row right text 2\nSub title row right text 2", 
                           "Content row left text 2\nContent row left text 2",
                           "Content row right text 2\nContent row right text 2"));
  listOfPOJOs.add(new POJO("Title row text 3", 
                           "Sub title row left text 3", "0000FF", "Sub title row right text 3", 
                           "Content row left text 3", 
                           "Content row right text 3"));

  XWPFDocument document = new XWPFDocument(new FileInputStream("./WordTenplate.docx"));

  XWPFTable table = document.getTableArray(0);
  
  insertContentInTable(table, listOfPOJOs);

  FileOutputStream out = new FileOutputStream("./WordResult.docx");
  document.write(out);
  out.close();
  document.close();
 }
 
 static class POJO {
  private String titleRowText;
  private String subTitleRowLeftText;
  private String subTitleRowLeftColor;
  private String subTitleRowRightText;
  private String contentRowLeftText;
  private String contentRowRightText;
  public POJO ( String titleRowText,
                String subTitleRowLeftText,
                String subTitleRowLeftColor,
                String subTitleRowRightText,
                String contentRowLeftText,
                String contentRowRightText ) {
   this.titleRowText = titleRowText;
   this.subTitleRowLeftText = subTitleRowLeftText;
   this.subTitleRowLeftColor = subTitleRowLeftColor;
   this.subTitleRowRightText = subTitleRowRightText;
   this.contentRowLeftText = contentRowLeftText;
   this.contentRowRightText = contentRowRightText;     
  }
  public String getTitleRowText() {
   return this.titleRowText;
  }
  public String getSubTitleRowLeftText() {
   return this.subTitleRowLeftText;
  }
  public String getSubTitleRowLeftColor() {
   return this.subTitleRowLeftColor;
  }
  public String getSubTitleRowRightText() {
   return this.subTitleRowRightText;
  }
  public String getContentRowLeftText() {
   return this.contentRowLeftText;
  }
  public String getContentRowRightText() {
   return this.contentRowRightText;
  }
 }
}

结果:

这段代码被最小化,以显示原则。如果需要文本格式设置,则需要扩展setText(XWPFTableCell cell, String text)方法。Word中的文本格式需要每个文本的XWPFRuns,这些文本的格式应该不同。当然,POJO也需要字段来确定所需的格式。

票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72422419

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档