1 Star 3 Fork 2

汪少棠 / database-tow-doc-tools

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
WordTableUtils.java 3.50 KB
一键复制 编辑 原始数据 按行查看 历史
汪少棠 提交于 2020-12-30 20:44 . 项目备份
package com.wmx.db2doc.util;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;
/**
* 在 Word 中创建表格工具类
*
* @author wangMaoXiong
* @version 1.0
* @date 2020/12/29 21:15
*/
public class WordTableUtils {
/**
* Word 文档中创建表格
*
* @param xwpfDocument :XWPFDocument 是用于处理 .docx 文件的高级 API
* @param headerTitle :表头文本内容
* @param dataList :表格正文数据
* @throws Exception
*/
public static void createSimpleTable(XWPFDocument xwpfDocument, String[] headerTitle, List<Map<String, Object>> dataList) {
//创建一个指定了行和列的空表
XWPFTable table = xwpfDocument.createTable(dataList.size() + 1, dataList.get(0).size());
//设置表格对齐方式
table.setTableAlignment(TableRowAlign.LEFT);
//设置表格宽度为 100%
table.setWidth("100%");
//设置表头
setHeaderRow(table, headerTitle);
int bodyRowSize = dataList.size();
int bodyColSize = dataList.get(0).size();
for (int i = 0; i < bodyRowSize; i++) {
Map<String, Object> objectMap = dataList.get(i);
for (int j = 0; j < bodyColSize; j++) {
Object[] objects = objectMap.values().toArray();
if (j < objects.length && objects[j] != null) {
table.getRow(i + 1).getCell(j).setText(objects[j].toString());
}
}
}
}
/**
* 设置表头
*
* @param table
*/
private static void setHeaderRow(XWPFTable table, String[] headerTitle) {
int cellIndex = 0;
for (String title : headerTitle) {
XWPFTableCell headCell = table.getRow(0).getCell(cellIndex++);
headCell.setColor("BFC6D1");
headCell.setText(title);
}
}
/**
* 自定义标题样式
*
* @param docxDocument :目标文档
* @param strStyleId :样式id,后期正文中的标题文本会通过样式id进行关联,而且样式名称会显示在文档的标题样式中
* @param headingLevel :样式级别,从0开始,一级标题是0,二级标题是1,依此类推
*/
public static void addCustomHeadingStyle(XWPFDocument docxDocument, String strStyleId, int headingLevel) {
CTStyle ctStyle = CTStyle.Factory.newInstance();
ctStyle.setStyleId(strStyleId);
CTString styleName = CTString.Factory.newInstance();
styleName.setVal(strStyleId);
ctStyle.setName(styleName);
CTDecimalNumber indentNumber = CTDecimalNumber.Factory.newInstance();
indentNumber.setVal(BigInteger.valueOf(headingLevel));
// lower number > style is more prominent in the formats bar
ctStyle.setUiPriority(indentNumber);
CTOnOff onoffnull = CTOnOff.Factory.newInstance();
ctStyle.setUnhideWhenUsed(onoffnull);
// style shows up in the formats bar
ctStyle.setQFormat(onoffnull);
// style defines a heading of the given level
CTPPr ppr = CTPPr.Factory.newInstance();
ppr.setOutlineLvl(indentNumber);
ctStyle.setPPr(ppr);
XWPFStyle style = new XWPFStyle(ctStyle);
// is a null op if already defined
XWPFStyles styles = docxDocument.createStyles();
style.setType(STStyleType.PARAGRAPH);
styles.addStyle(style);
}
}
Java
1
https://gitee.com/wangmx1993/database-tow-doc-tools.git
git@gitee.com:wangmx1993/database-tow-doc-tools.git
wangmx1993
database-tow-doc-tools
database-tow-doc-tools
master

搜索帮助