DataCommon.java

package com.ksource.pwlp.model.statistic;

public class DataCommon {

    private Long id;
private String name;
private String otherName;
private String dataName;
private String otherDataName;
private int value;
private float otherValue;
private double othersValue;
private String dataValue;
private Long otherDataValue;
private int oneResult;
private int twoResult;
private int threeResult;
private int fourResult;
private int fiveResult;
private int sixResult;
private float oneValue;
private float twoValue;
private float threeValue;
private float fourValue;
private float fiveValue;
private float sixValue;
private double oneVal;
private double twoVal;
private double threeVal;
private double fourVal;
private double fiveVal;
private double sixVal;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getOtherName() {
return otherName;
}
public void setOtherName(String otherName) {
this.otherName = otherName;
}
public String getDataName() {
return dataName;
}
public void setDataName(String dataName) {
this.dataName = dataName;
}
public String getOtherDataName() {
return otherDataName;
}
public void setOtherDataName(String otherDataName) {
this.otherDataName = otherDataName;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public float getOtherValue() {
return otherValue;
}
public void setOtherValue(float otherValue) {
this.otherValue = otherValue;
}
public double getOthersValue() {
return othersValue;
}
public void setOthersValue(double othersValue) {
this.othersValue = othersValue;
}
public String getDataValue() {
return dataValue;
}
public void setDataValue(String dataValue) {
this.dataValue = dataValue;
}
public Long getOtherDataValue() {
return otherDataValue;
}
public void setOtherDataValue(Long otherDataValue) {
this.otherDataValue = otherDataValue;
}
public int getOneResult() {
return oneResult;
}
public void setOneResult(int oneResult) {
this.oneResult = oneResult;
}
public int getTwoResult() {
return twoResult;
}
public void setTwoResult(int twoResult) {
this.twoResult = twoResult;
}
public int getThreeResult() {
return threeResult;
}
public void setThreeResult(int threeResult) {
this.threeResult = threeResult;
}
public int getFourResult() {
return fourResult;
}
public void setFourResult(int fourResult) {
this.fourResult = fourResult;
}
public int getFiveResult() {
return fiveResult;
}
public void setFiveResult(int fiveResult) {
this.fiveResult = fiveResult;
}
public int getSixResult() {
return sixResult;
}
public void setSixResult(int sixResult) {
this.sixResult = sixResult;
}
public float getOneValue() {
return oneValue;
}
public void setOneValue(float oneValue) {
this.oneValue = oneValue;
}
public float getTwoValue() {
return twoValue;
}
public void setTwoValue(float twoValue) {
this.twoValue = twoValue;
}
public float getThreeValue() {
return threeValue;
}
public void setThreeValue(float threeValue) {
this.threeValue = threeValue;
}
public float getFourValue() {
return fourValue;
}
public void setFourValue(float fourValue) {
this.fourValue = fourValue;
}
public float getFiveValue() {
return fiveValue;
}
public void setFiveValue(float fiveValue) {
this.fiveValue = fiveValue;
}
public float getSixValue() {
return sixValue;
}
public void setSixValue(float sixValue) {
this.sixValue = sixValue;
}
public double getOneVal() {
return oneVal;
}
public void setOneVal(double oneVal) {
this.oneVal = oneVal;
}
public double getTwoVal() {
return twoVal;
}
public void setTwoVal(double twoVal) {
this.twoVal = twoVal;
}
public double getThreeVal() {
return threeVal;
}
public void setThreeVal(double threeVal) {
this.threeVal = threeVal;
}
public double getFourVal() {
return fourVal;
}
public void setFourVal(double fourVal) {
this.fourVal = fourVal;
}
public double getFiveVal() {
return fiveVal;
}
public void setFiveVal(double fiveVal) {
this.fiveVal = fiveVal;
}
public double getSixVal() {
return sixVal;
}
public void setSixVal(double sixVal) {
this.sixVal = sixVal;
}
}

调用导出word表格代码如下:

List<DataCommon> dataCommonList = new ArrayList<DataCommon>(); //数据是从数据库查出来取需要字段的值存入DataCommon中后存到dataCommonList中

dataCommon.setName("类别列的值");
  dataCommon.setOtherName("证据名称的值");
  dataCommonList.add(dataCommon);

 //dataCommonList中存的值有一定规律,相同类别的是紧邻挨着的

String savePath = this.attachPath + File.separator + ContextUtil.getCurrentUser().getAccount() + "\\" + System.currentTimeMillis() + ".docx";
new CreateTable().createSimpleTable(dataCommonList,savePath);

导出word表格方法如下:

package com.ksource.pwlp.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.util.List;
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.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.STMerge;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalJc; import com.ksource.core.util.ContextUtil;
import com.ksource.pwlp.model.statistic.DataCommon; /**
* 创建证据材料清单表格
* @author dxy
*
*/
public class CreateTable { public void createSimpleTable(List<DataCommon> dataCommonList, String savePath) throws Exception {
String orgName = ContextUtil.getCurrentOrg().getOrgName();
XWPFDocument xdoc = new XWPFDocument();
XWPFParagraph xp = xdoc.createParagraph();
xp.setSpacingBefore(0);
XWPFRun r1 = xp.createRun();
XWPFRun r2 = xp.createRun();
r1.setText(orgName);
r1.setFontFamily("宋体");
r1.setFontSize(18);
r1.addBreak(); // 换行
r2.setText("证据清单");
r2.setFontFamily("宋体");
r2.setFontSize(22);
r2.setTextPosition(10);
r2.setBold(true);
r2.addBreak(); // 换行
xp.setAlignment(ParagraphAlignment.CENTER); Integer col_total_count = 4; // 表格最多的列数 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++;
String preGroupName = "";
String groupName = "";
int flag = 0;
int num = 1;
for (int i2 = i; i2 < dataCommonList.size()+1; 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))); if (j == 0) {
if(preGroupName.equals(groupName)){
cell.setText(String.valueOf(num));
}else{
cell.setText(String.valueOf(++num));
}
}
if (j == 1) {
cell.setText(dataCommonList.get(i2-1).getName());
if(i2 > 1){
preGroupName = dataCommonList.get(i2-2).getName();
}
groupName = dataCommonList.get(i2-1).getName();
}
if (j == 2) {
cell.setText(dataCommonList.get(i2-1).getOtherName());
}
if (j == 3) {
cell.setText("");
}
} if(preGroupName.equals(groupName) && !"".equals(groupName)){
flag++;
if(i2 == dataCommonList.size()){
mergeCellsVertically(xTable, 0, i2-flag, i2);
mergeCellsVertically(xTable, 1, i2-flag, i2);
flag = 0;
}
}else{
if(flag > 0){
if(i2 < dataCommonList.size()){
if(i2 > 1 && !"".equals(preGroupName)){
mergeCellsVertically(xTable, 0, i2-1-flag, i2-1);
mergeCellsVertically(xTable, 1, i2-1-flag, i2-1);
flag = 0;
}
}
if(i2 == dataCommonList.size()){
mergeCellsVertically(xTable, 0, i2-1-flag, i2-1);
mergeCellsVertically(xTable, 1, i2-1-flag, i2-1);
flag = 0;
}
}
}
} 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 xdoc, XWPFTableCell cell,
String text, String bgcolor, int width) {
CTTc cttc = cell.getCTTc();
CTTcPr cellPr = cttc.addNewTcPr();
cellPr.addNewTcW().setW(BigInteger.valueOf(width));
XWPFParagraph paragraph = cell.getParagraphs().get(0);
paragraph.setAlignment(ParagraphAlignment.CENTER); //设置表头单元格居中
XWPFRun run = paragraph.createRun();
run.setFontFamily("仿宋_GB2312");
run.setFontSize(16); //设置表头单元格字号
//run.setBold(true); //设置表头单元格加粗
run.setText(text);
} /**
* 设置列宽
*
* @param index
* @return
*/
private static int getCellWidth(int index) {
int cwidth = 1000;
if (index == 0) {
cwidth = 1000;
} else if (index == 1) {
cwidth = 2100;
} else if (index == 2) {
cwidth = 3200;
} else if (index == 3) {
cwidth = 2100;
}
return cwidth;
} /**
* 跨行合并
*
* @param table
* @param col
* @param fromRow
* @param toRow
*/
public static void mergeCellsVertically(XWPFTable table, int col,
int fromRow, int toRow) {
for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) {
XWPFTableCell cell = table.getRow(rowIndex).getCell(col);
if (rowIndex == fromRow) {
// The first merged cell is set with RESTART merge value
cell.getCTTc().addNewTcPr().addNewVMerge()
.setVal(STMerge.RESTART);
} else {
// Cells which join (merge) the first one, are set with CONTINUE
cell.getCTTc().addNewTcPr().addNewVMerge()
.setVal(STMerge.CONTINUE);
}
}
} /**
* 将文件转换成byte数组
* @param filePath
* @return
*/
public byte[] fileToByte(File file){
byte[] buffer = null;
try
{
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int n;
while ((n = fis.read(b)) != -1)
{
bos.write(b, 0, n);
}
fis.close();
bos.close();
buffer = bos.toByteArray();
}catch (FileNotFoundException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
return buffer;
}
}

导出效果图如下:

poi导出word表格跨行的更多相关文章

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

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

  2. poi导出word表格

    代码如下: package com.ksource.pwlp.util; import java.io.FileOutputStream; import java.math.BigInteger; i ...

  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. 使用POI创建word表格合并单元格兼容wps

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

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

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

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

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

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

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

随机推荐

  1. babel安装及使用

    安装babel npm install babel-cli -g 配置babel babel是用过插件或者预设来编译代码的 新建.babelrc文件 文件中输入一下内容 { "presets ...

  2. 17、在vue中引用移动端框架Vux:

    1:使用vue-cli创建好项目(此处省略步骤)2:在项目中安装vux:npm install vux --save3:​安装vux-loader:npm install vux-loader --s ...

  3. Android实现二维码扫描功能

    1.效果预览 先上图展示效果(模拟器没有摄像头,录出来效果不好,将就看) 2.集成步骤 1.拷贝本项目demo中的com.google.zxing5个包引入到自己的项目中. 2.拷贝本项目demo中的 ...

  4. Android图像滤镜框架GPUImage从配置到应用

    GPUImage简介 GPUImage 是iOS下一个开源的基于GPU的图像处理库,提供各种各样的图像处理滤镜,并且支持照相机和摄像机的实时滤镜.GPUImage for Android是它在Andr ...

  5. Linux 部署 YUM 仓库

    本篇主要写了怎么搭建自定义的YUM源,在一个拥有大量本地网络的主机环境中,可以减少对外网的依赖. Server 安装 vsftp 包 [root@server ~]# yum install vsft ...

  6. Httpd服务入门知识-Httpd服务常见配置案例之日志设定

    Httpd服务入门知识-Httpd服务常见配置案例之日志设定 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.日志类型 [root@node101.yinzhengjie.org ...

  7. Ecshop 2.x-3.x RCE漏洞复现

    说是复现,其实来源于一道CTF题目(Ecshop3.x RCE) 链接:http://www.whalwl.cn:8030 1. 漏洞概述 ECShop的user.php文件中的display函数的模 ...

  8. 1.2 管理 NetBackup 许可证

    关于管理 NetBackup 许可证 NetBackup许可证密钥是在安装软件时添加的.对于需要单独购买的选件,可以稍 后在"许可证密钥"对话框中添加许可证. 注意:在进行任何许可 ...

  9. 缓冲加载图片的 jQuery 插件 lazyload.js 使用方法详解

    在写代码的时候,经常会用到懒加载的模式,以前是通过window.onload的模式去加载,但是图片很多或者用ajax请求的时候,就会很麻烦,现在用lazyload的模式加载方便很多 <!doct ...

  10. 【转载】TCP协议

    首部格式 图释: 各个段位说明: 源端口和目的端口:各占 2 字节.端口是传输层与应用层的服务接口.传输层的复用和分用功能都要通过端口才能实现 序号:占 4 字节.TCP 连接中传送的数据流中的每一个 ...