如何使用Apache POI在Microsoft Word文档中创建具有行合并和列合并的表格?

3
我正在使用APACHE POI来创建包含表格的Word文档。该表格如下所示,我想要创建具有rowspan和colspan的单元格(如图所示)。 enter image description here 是否可以通过APACHE POI实现?
是否有其他适用于此功能的Java库?任何帮助将不胜感激。

1
我猜,POI仍在改进POI,我可能需要更长时间等待答案。 - Pradeep
1个回答

3
你可以尝试这个。
    public class Word2Doc {
   public static void main(String aaa[]){
System.out.println("This is Word To Document Class");

File file = null; 
       FileOutputStream fos = null; 
       XWPFDocument document = null; 
       XWPFParagraph para = null; 
       XWPFRun run = null; 
       try { 
           // Create the first paragraph and set it's text. 
           document = new XWPFDocument(); 
           para = document.createParagraph(); 

           para.setAlignment(ParagraphAlignment.CENTER); 

           para.setSpacingAfter(100); 

           para.setSpacingAfterLines(10);
           run = para.createRun(); 
           run.addBreak();    // similar to new line
           run.addBreak();

           XWPFTable table = document.createTable(4, 3);

           table.setRowBandSize(1);
           table.setWidth(1);
           table.setColBandSize(1);
           table.setCellMargins(1, 1, 100, 30);

           table.setStyleID("finest");


           table.getRow(1).getCell(1).setText("EXAMPLE OF TABLE");
           table.getRow(2).getCell(1).setText("fine");
           XWPFParagraph p1 = table.getRow(0).getCell(0).getParagraphs().get(0);
           p1.setAlignment(ParagraphAlignment.CENTER);
                   XWPFRun r1 = p1.createRun();
                   r1.setBold(true);
                   r1.setText("Test Name");
                   r1.setItalic(true);
                   r1.setFontFamily("Courier");
                   r1.setUnderline(UnderlinePatterns.DOT_DOT_DASH);
                   r1.setTextPosition(100);

          //Locating the cell values
                    table.getRow(0).getCell(1).setText("Value"); 
                    table.getRow(0).getCell(2).setText("Normal Ranges"); 

                   table.getRow(2).getCell(2).setText("numeric values");

                    table.setWidth(120);

           file = new File("c:\\nwhpe.docx"); 
           if(file.exists())
               file.delete();


           FileOutputStream out = new FileOutputStream(file);
           document.write(out);
           out.close();
       } catch(Exception e){e.printStackTrace();}
    }

       } 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接