天天看點

Apache POI 4 導出word表格Apache POI 4 導出word表格

Apache POI 4 導出word表格

1. poi依賴

<dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi</artifactId>
      <version>4.1.2</version>
    </dependency>
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml</artifactId>
      <version>4.1.2</version>
    </dependency>
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-scratchpad</artifactId>
      <version>4.1.2</version>
    </dependency>
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml-schemas</artifactId>
      <version>4.1.2</version>
    </dependency>
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-excelant</artifactId>
      <version>4.1.2</version>
    </dependency>
           

2. java代碼

package org.bluewing;

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

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.math.BigInteger;
import java.util.List;

public class ExportWord {


    public static void main(String[] args) throws Exception {
        ExportWord exportWord = new ExportWord();
        XWPFDocument doc = new XWPFDocument();

        doc = exportWord.exportTable(doc, 5, 5);
        // write the file
        OutputStream out = new FileOutputStream("D:\\myTable.docx");
        doc.write(out);
    }

    /**
     * 建立表格
     *
     * @param doc  word對象
     * @param rows 行數
     * @param cols 列數
     * @throws Exception
     */
    public XWPFDocument exportTable(XWPFDocument doc, int rows, int cols) throws Exception {
        XWPFTable table = doc.createTable(rows, cols);

        //擷取表格所有的行
        List<XWPFTableRow> allrows = table.getRows();
        //目前選中的行
        int currRow = 0;
        //目前選中的列
        int currCol = 0;

        //周遊所有的行,處理表格
        for (XWPFTableRow row : allrows) {
            //擷取目前行的屬性對象
            CTTrPr rowProperty = row.getCtRow().addNewTrPr();
            //擷取行高度對象
            CTHeight rowheight = rowProperty.addNewTrHeight();
            //設定行高度
            rowheight.setVal(BigInteger.valueOf(360));


            //擷取目前行中的所有單元格
            List<XWPFTableCell> cells = row.getTableCells();
            //周遊單元格,為單元格指派,設定屬性
            for (XWPFTableCell cell : cells) {
                //擷取單元格屬性對象
                CTTcPr cellProperty = cell.getCTTc().addNewTcPr();

                //擷取單元格對齊的對象
                CTVerticalJc va = cellProperty.addNewVAlign();
                //設定居中對齊
                va.setVal(STVerticalJc.CENTER);

                //擷取單元格顔色屬性對象
                CTShd cellColor = cellProperty.addNewShd();
                cellColor.setColor("auto");
                cellColor.setVal(STShd.CLEAR);
                if (currRow == 0) {
                    //表頭填充色
                    cellColor.setFill("A7BFDE");
                } else if (currRow % 2 == 0) {
                    //偶數行顔色
                    cellColor.setFill("D3DFEE");
                } else {
                    // 奇數行顔色
                    cellColor.setFill("EDF2F8");
                }

                //建立文本段落
                XWPFParagraph para = cell.getParagraphs().get(0);
                //建立文本操作對象
                XWPFRun run = para.createRun();
                if (currRow == 0) {
                    //表頭部分的文字+屬性
                    run.setText("header row, col " + currCol);
                    run.setFontSize(15);
                    run.setBold(true);
                    run.setColor("FFFF00");
                    para.setAlignment(ParagraphAlignment.CENTER);
                } else {
                    //表格資料部分的文字+屬性
                    run.setText("row: " + currRow + ", col: " + currCol);
                    run.setFontSize(12);
                    para.setAlignment(ParagraphAlignment.LEFT);
                }
                currCol++;
            }
            //重置目前選中的列
            currCol = 0;
            currRow++;
        }
        return doc;
    }
}