如何使用 apache POI 在 ms word doc 中创建带有 rowspan 和 colspan 的表格? [英] How to create table in ms word doc with rowspan and colspan using apache POI?

查看:26
本文介绍了如何使用 apache POI 在 ms word doc 中创建带有 rowspan 和 colspan 的表格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 APACHE POI 创建包含表格的 word doc.表格如下所示,我想创建具有 rowspan 和 colspan 的单元格(如图所示).

I am using APACHE POI to create word doc containing table. The table looks like as follows, I want to create cells with rowspan and colspan(as shown in figure).

是否可以使用 APACHE POI?

Is it possible using APACHE POI ?

是否有其他相同的 java 库.任何帮助表示赞赏.

Is there any other java library for the same. Any help is appreciated.

推荐答案

你可以试试这个.

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

       } 

这篇关于如何使用 apache POI 在 ms word doc 中创建带有 rowspan 和 colspan 的表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆