代码如下:

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表格的更多相关文章

  1. poi导出word表格详解 超详细了

    转:非常感谢原作者 poi导出word表格详解 2018年07月20日 10:41:33 Z丶royAl 阅读数:36138   一.效果如下 二.js代码 function export_word( ...

  2. poi导出word表格跨行

    DataCommon.java package com.ksource.pwlp.model.statistic; public class DataCommon { private Long id; ...

  3. 使用POI导出Word(含表格)的实现方式及操作Word的工具类

    .personSunflowerP { background: rgba(51, 153, 0, 0.66); border-bottom: 1px solid rgba(0, 102, 0, 1); ...

  4. poi导出word

    最近做了个poi导出word的功能 下面是代码: 一个可以参考的例子: package com.lzb.crm.web; import java.io.FileOutputStream; import ...

  5. java工具类POI导出word

    1.新建一个word,里面填写内容,如: 2.导出wordjava类 /** * POI导出word测试 * @throws Exception */ @RequestMapping(value=&q ...

  6. PowerDesiger 15逆向生成工程E-R图及导出word表格

    应用环境:win8(64位)+oracle10g(32位)服务端+PowerDesigner15 需求:oracle数据库中的表结构是web工程框架hibernate 自动生成,现需要将数据库中已有的 ...

  7. Maven项目结合POI导出Excl表格Demo-亲测可用

    Maven项目结合POI导出Excl表格 一.POM文件添加依赖 <!-- https://mvnrepository.com/artifact/org.apache.poi/poi --> ...

  8. 使用POI创建word表格合并单元格兼容wps

    poi创建word表格合并单元格代码如下: /** * @Description: 跨列合并 */ public void mergeCellsHorizontal(XWPFTable table, ...

  9. poi导出word时设置兼容性

    接上一篇poi导出word http://www.cnblogs.com/xiufengd/p/4708680.html. public static void setAuto(XWPFDocumen ...

随机推荐

  1. npm换源成淘宝镜像

    由于node下载第三方依赖包是从国外服务器下载,虽然没有被墙,但是下载的速度是非常的缓慢且有可能会出现异常. 所以为了提高效率,我们还是把npm的镜像源替换成淘宝的镜像源.有几种方式供我们选择 使用c ...

  2. JavaScript 之 location 对象

    一.location 对象 location 对象是 window 对象下的一个属性,使用的时候可以省略 window 对象. 常用属性: location.href = 'http://www.ba ...

  3. php 根据URL下载远程图片、压缩包、pdf等文件到本地

    1.此方法可以下载图片.压缩包.pdf(亲测),应该所有类型的文件都可以下载到本地,可以试一下 //远程路径,名称,文件后缀 function downImgRar($url,$rename,$ext ...

  4. Django 之 rest_framework 分页器使用

    Django rest_framework 之分页器使用以及其源码分析 三种分页方式: 常规分页 -->PageNumberPagination 偏移分页 -->LimitOffsetPa ...

  5. RHEL6+GFS2+MYSQL高可用

    RHCS集群安装部署 组件介绍: luci: luci是一个基于web的,用来管理和配置RHCS集群,通过luci可以轻松的搭建一个功能强大的集群系统,节点主机可以使用ricci来和luci 管理段进 ...

  6. Ansible 常用模块(一)

    一.Ansible简介 Ansible是新出现的自动化运维工具,基于python开发,集合了众多运维工具(puppet(ruby).cfengine.chef.func.fabric.)的优点,实现了 ...

  7. 汇编 JMP 详解

    汇编 JMP 详解 关键词说明 RVA: 相对虚拟地址(Relative Virtual Address),在内存中相对于PE文件装入地址的偏移位置,是一个相对地址. JMP 的 3 种类型 短跳转( ...

  8. xtrabackup 使用

    创建具有完全备份所需的最小权限的数据库用户的SQL示例如下 mysql> CREATE USER 'bkpuser'@'%' IDENTIFIED BY 's3cret';mysql> G ...

  9. Dockerfile 常见指令的意义/常见的使用方式/使用示例/

    一.什么是 Dockerfile ? Dockerfile 就是生成docker镜像的指令集, 通过使用docker工具执行这些指令集可以方便快捷地生成镜像, 并且能不断复用 Dockerfile 指 ...

  10. pm2日志管理插件

    pm2的日志模块默认是每一个服务进程都分配两个默认的日志文件 普通日志 错误日志 这两个日志文件存放于/root/.pm2/logs中,如果pm2管理5个服务,那么该文件夹下总共有10个日志文件,并且 ...