java poi生成word 并插入 表格

spring boot 项目

java:

public static void main(String[] args) {
    // 文档生成方法
    XWPFDocument doc = new XWPFDocument();
    XWPFParagraph p1 = doc.createParagraph(); // 创建段落
    XWPFRun r1 = p1.createRun(); // 创建段落文本
    r1.setText("目录"); // 设置文本
    FileOutputStream out = null; // 创建输出流
    try {
        // 向word文档中添加内容
        XWPFParagraph p3 = doc.createParagraph(); // 创建段落
        XWPFRun r3 = p3.createRun(); // 创建段落文本
        r3.addTab();// tab
        r3.addBreak();// 换行
        r3.setBold(true);
        XWPFParagraph p2 = doc.createParagraph(); // 创建段落
        XWPFRun r2 = p2.createRun(); // 创建段落文本
        // 设置文本
        r2.setText("表名");
        r2.setFontSize(14);
        r2.setBold(true);
        XWPFTable table1 = doc.createTable(8, 10);
        table1.setWidthType(TableWidthType.AUTO);

        // 获取到刚刚插入的行
        XWPFTableRow row1 = table1.getRow(0);
        // 设置单元格内容
        row1.getCell(0).setText("字段名");
        row1.getCell(1).setText("字段说明");
        row1.getCell(2).setText("数据类型");
        row1.getCell(3).setText("长度");
        row1.getCell(4).setText("索引");
        row1.getCell(5).setText("是否为空");
        row1.getCell(6).setText("主键");
        row1.getCell(7).setText("外键");
        row1.getCell(8).setText("缺省值");
        row1.getCell(9).setText("备注");

        doc.setTable(0, table1);
        String filePath = "F:\\simple.docx";
        out = new FileOutputStream(new File(filePath));
        doc.write(out);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {

        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {

                e.printStackTrace();
            }
        }

    }

}

 

pom.xml:

 


    
        org.springframework.boot
        spring-boot-starter-web
    

    
        org.springframework.boot
        spring-boot-starter-test
        test
        
            
                org.junit.vintage
                junit-vintage-engine
            
        
    

    
        
        
        
    

    
        org.apache.poi
        poi-excelant
        3.14
    

    
        org.apache.poi
        poi-examples
        4.0.0
    
    
        org.apache.poi
        poi-ooxml-schemas
        3.10-FINAL
    

    
        org.apache.poi
        poi-ooxml
        4.0.0
    

   
    
        org.apache.poi
        poi
        4.0.0
    

    
        org.apache.xmlbeans
        xmlbeans
        3.1.0
    

    
        com.deepoove
        poi-tl
        1.6.0-beta1
    
    


    
        
        
        
    
    
        org.junit.platform
        junit-platform-commons
        RELEASE
        compile
    

    
        org.apache.commons
        commons-lang3
        3.4
    

    
        joda-time
        joda-time
        2.9.4
    

    
        jacob
        jacob
        system
        ${basedir}/lib/jacob.jar
    

    
        ooxml-schemas
        ooxml-schemas
        system
        ${basedir}/lib/ooxml-schemas-1.1.jar
    

    
        spire.doc.free-2.7.3
        spire.doc.free-2.7.3
        system
        ${basedir}/lib/spire.doc.free-2.7.3.jar
    

 

你可能感兴趣的:(开发)