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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

带头大哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值