poi导出word表格
代码如下:
package com.ksource.pwlp.util; import java.io.FileOutputStream;
import java.math.BigInteger;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.apache.poi.xwpf.usermodel.XWPFTableCell.XWPFVertAlign;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalJc; public class CreateTable { public void createSimpleTable(String savePath) throws Exception {
XWPFDocument xdoc = new XWPFDocument();
XWPFParagraph xp = xdoc.createParagraph();
xp.setSpacingBefore(0);
XWPFRun r1 = xp.createRun();
r1.setText("湖北省人民检察院");
r1.addBreak(); // 换行
r1.setText("证据清单");
r1.setFontFamily("宋体");
r1.setFontSize(16);
r1.setTextPosition(10);
r1.setBold(true);
xp.setAlignment(ParagraphAlignment.CENTER); Integer col_total_count = 4; // 表格最多的列数
Integer data_count = 20; // 需要创建的总条数 XWPFTable xTable = xdoc.createTable(1, col_total_count); CTTbl ttbl = xTable.getCTTbl();
CTTblPr tblPr = ttbl.getTblPr() == null ? ttbl.addNewTblPr() : ttbl
.getTblPr();
CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr
.addNewTblW();
tblWidth.setW(new BigInteger("8600"));
tblWidth.setType(STTblWidth.DXA); // 创建表头数据
int i = 0;
xTable.getRow(i).setHeight(500);
setCellText(xdoc, xTable.getRow(i).getCell(0), "序号", "FFFFFF", getCellWidth(0));
setCellText(xdoc, xTable.getRow(i).getCell(1), "类别", "FFFFFF", getCellWidth(1));
setCellText(xdoc, xTable.getRow(i).getCell(2), "证据名称", "FFFFFF", getCellWidth(2));
setCellText(xdoc, xTable.getRow(i).getCell(3), "备注", "FFFFFF", getCellWidth(3)); // 创建表格内容
i++;
for (int i2 = i; i2 < data_count; i2++) {
XWPFTableRow row = xTable.insertNewTableRow(i2);
row.setHeight(450);
for (int j = 0, j2 = 0; j < col_total_count; j++, j2++) {
XWPFTableCell cell = row.createCell();
CTTc cttc = cell.getCTTc();
CTTcPr cellPr = cttc.addNewTcPr();
cellPr.addNewVAlign().setVal(STVerticalJc.CENTER);
cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);
cellPr.addNewTcW().setW(BigInteger.valueOf(getCellWidth(j2)));
cell.setText("测试" + j);
}
}
FileOutputStream fos = new FileOutputStream(savePath);
xdoc.write(fos);
fos.close();
} /**
* 设置表头内容
*
* @param xDocument
* @param cell
* @param text
* @param bgcolor
* @param width
*/
private static void setCellText(XWPFDocument xDocument, XWPFTableCell cell,
String text, String bgcolor, int width) {
CTTc cttc = cell.getCTTc();
CTTcPr cellPr = cttc.addNewTcPr();
cellPr.addNewTcW().setW(BigInteger.valueOf(width));
cell.setColor(bgcolor);
cell.setVerticalAlignment(XWPFVertAlign.CENTER);
CTTcPr ctPr = cttc.addNewTcPr();
ctPr.addNewVAlign().setVal(STVerticalJc.CENTER);
cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);
cell.setText(text);
} /**
* 设置列宽
*
* @param index
* @return
*/
private static int getCellWidth(int index) {
int cwidth = 1000;
if (index == 0) {
cwidth = 2100;
} else if (index == 1) {
cwidth = 2100;
} else if (index == 2) {
cwidth = 2100;
} else if (index == 3) {
cwidth = 2100;
}
return cwidth;
} public static void main(String[] args) throws Exception {
CreateTable t = new CreateTable();
t.createSimpleTable("g:/"+ System.currentTimeMillis() + ".docx");
}
}
生成word如下:
poi导出word表格的更多相关文章
- poi导出word表格详解 超详细了
转:非常感谢原作者 poi导出word表格详解 2018年07月20日 10:41:33 Z丶royAl 阅读数:36138 一.效果如下 二.js代码 function export_word( ...
- poi导出word表格跨行
DataCommon.java package com.ksource.pwlp.model.statistic; public class DataCommon { private Long id; ...
- 使用POI导出Word(含表格)的实现方式及操作Word的工具类
.personSunflowerP { background: rgba(51, 153, 0, 0.66); border-bottom: 1px solid rgba(0, 102, 0, 1); ...
- poi导出word
最近做了个poi导出word的功能 下面是代码: 一个可以参考的例子: package com.lzb.crm.web; import java.io.FileOutputStream; import ...
- java工具类POI导出word
1.新建一个word,里面填写内容,如: 2.导出wordjava类 /** * POI导出word测试 * @throws Exception */ @RequestMapping(value=&q ...
- PowerDesiger 15逆向生成工程E-R图及导出word表格
应用环境:win8(64位)+oracle10g(32位)服务端+PowerDesigner15 需求:oracle数据库中的表结构是web工程框架hibernate 自动生成,现需要将数据库中已有的 ...
- Maven项目结合POI导出Excl表格Demo-亲测可用
Maven项目结合POI导出Excl表格 一.POM文件添加依赖 <!-- https://mvnrepository.com/artifact/org.apache.poi/poi --> ...
- 使用POI创建word表格合并单元格兼容wps
poi创建word表格合并单元格代码如下: /** * @Description: 跨列合并 */ public void mergeCellsHorizontal(XWPFTable table, ...
- poi导出word时设置兼容性
接上一篇poi导出word http://www.cnblogs.com/xiufengd/p/4708680.html. public static void setAuto(XWPFDocumen ...
随机推荐
- Golang报错:Cannot convert expression of type interface{} to type []byte
在使用golang实现后端登录逻辑的时候,碰到下面的问题:Cannot convert expression of type interface{} to type []byte 首先介绍下问题出现的 ...
- Kafka消费者组静态成员(static consumer member)
Kafka 2.3发布后官网的Consumer参数中增加了一个新的参数:group.instance.id.下面是这个参数的解释: A unique identifier of the consume ...
- MySQL用户及权限
1. MySQL根据对象级别划分的权限类别: 常见的权限类别:库级别.表级别.字段级别.管理类权限.程序类权限 管理类权限: CREATE TEMPORARY TABLES 创建临时表,一般为16M; ...
- 解决pip安装时速度慢的问题
http://blog.csdn.net/wukai0909/article/details/62427437 国内源: 新版ubuntu要求使用https源,要注意. 清华:https://py ...
- Golang: 解析JSON数据之一
JSON 作为目前最流行的数据传输格式, 相信每个程序员都跟它打过交道吧.使用 Go 语言时,也不可避免的要操作 JSON 数据,令人惊喜的是,Go 内置了序列化和反序列化 JSON 的功能,今天就来 ...
- Vue-cli3 中 通过在index.html添加的script js文件 如何在组件内使用不会 xxx is not defined错误
以jQuery 为例 第一种方法 更改webpack配置信息 1.在vue.config.js中(如果没有 请在根目录新建)配置如下信息 // const webpack = require('web ...
- 浅谈Python设计模式 - 代理模式
声明:本系列文章主要参考<精通Python设计模式>一书,并且参考一些资料,结合自己的一些看法来总结而来. 一.在某些应用中,我们想要在访问某个对象之前执行一个或者多个重要的操作,例如,访 ...
- 运维基础——Zabbix:Lack of free swap space on Zabbix server
问题 使用Zabbix监控一些云主机时,可能遇到: Lack of free swap space on Zabbix server 使用命令: free -m 看到: Swap 的total,use ...
- git远程上的分支到本地
先想一个自己要在本地新建的分支名称,qianjinyan git checkout -b qianjinyan origin/SELLER-2248-1018 git branch 查看分支 git ...
- Linux core dump总结
文章链接:https://www.cnblogs.com/Anker/p/6079580.html 1.前言 一直在从事linux下后台开发,经常与core文件打交道.还记得刚开始从事linux下开发 ...