首页 > 技术文章 > iText、poi操作word2007(读取,生成)

zhxn 2017-06-15 14:29 原文

关于生成word文件以及插入文字、表格、图片等功能,我使用了poi和itext,因为poi插入图片的jar包我在网上查并不是太完全,也可能我没找到如何使用,所以插入图片我用的是itext

iText所需jar包,在pom文件中配置

<!-- https://mvnrepository.com/artifact/com.lowagie/itext -->
        <dependency>
            <groupId>com.lowagie</groupId>
            <artifactId>itext</artifactId>
            <version>4.2.2</version>
        </dependency>

 

1、创建word2007文件并插入表格(poi,所需jar包在我其他博客里有对poi的介绍,或去网上查)

//创建word2007文件插入表格
  public void createWord2007() {
    XWPFDocument doc = new XWPFDocument();
    XWPFParagraph p1 = doc.createParagraph();

    XWPFTable table = doc.createTable(11, 4);
    // CTTblBorders borders=table.getCTTbl().getTblPr().addNewTblBorders();
    CTTblPr tblPr = table.getCTTbl().getTblPr();
    tblPr.getTblW().setType(STTblWidth.DXA);
    tblPr.getTblW().setW(new BigInteger("7000"));

    // 设置上下左右四个方向的距离,可以将表格撑大
    table.setCellMargins(20, 20, 20, 20);

    // 表格
    List<XWPFTableCell> tableCells = table.getRow(0).getTableCells();

    XWPFTableCell cell = tableCells.get(0);
    XWPFParagraph newPara = new XWPFParagraph(cell.getCTTc().addNewP(), cell);
    XWPFRun run = newPara.createRun();
    /** 内容居中显示 **/
    newPara.setAlignment(ParagraphAlignment.CENTER);
    run.getCTR().addNewRPr().addNewColor().setVal("FF0000");/**FF0000红色*/
    run.setUnderline(UnderlinePatterns.THICK);
    run.setText("第1行1列");
    tableCells.get(1).setText("第1行2列");
    tableCells.get(2).setText("第1行3列");
    tableCells.get(3).setText("第1行4列");
    tableCells = table.getRow(1).getTableCells();
    tableCells.get(0).setText("第2行1列");
    tableCells.get(1).setText("第2行2列");
    tableCells.get(2).setText("第2行3列");
    tableCells.get(3).setText("第2行4列");

    // 设置字体对齐方式
    p1.setAlignment(ParagraphAlignment.CENTER);
    p1.setVerticalAlignment(TextAlignment.TOP);

    // 第一页要使用p1所定义的属性
    XWPFRun r1 = p1.createRun();

    // 设置字体是否加粗
    r1.setBold(true);
    r1.setFontSize(20);

    // 设置使用何种字体
    r1.setFontFamily("Courier");

    // 设置上下两行之间的间距
    r1.setTextPosition(20);
    r1.setText("标题");

    FileOutputStream out;
    try {
      out = new FileOutputStream("d:/word2007.docx");
      // 以下代码可进行文件下载
      // response.reset();
      // response.setContentType("application/x-msdownloadoctet-stream;charset=utf-8");
      // response.setHeader("Content-Disposition",
      // "attachment;filename=\"" + URLEncoder.encode(fileName, "UTF-8"));
      // OutputStream out = response.getOutputStream();
      // this.doc.write(out);
      // out.flush();

      doc.write(out);
      out.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
    System.out.println("success");
  }

2、插入图片,文字(iText)

public void exportImg() {
    Document document = null;
    try {
      document = new Document();
      RtfWriter2.getInstance(document, new FileOutputStream("D:/word2007.doc"));
      document.open();
      Paragraph title = new Paragraph("你好,Word!");
      document.add(title);
      try {
        Image image = Image.getInstance("C:\\Users\\zhxn\\Desktop\\1.jpg");
        //插入一个图片
        document.add(image);
      } catch (IOException e) {
        e.printStackTrace();
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (DocumentException e) {
      e.printStackTrace();
    } finally {
      if (document != null) {
        document.close();
      }
    }

  }

 

推荐阅读