记录一下表格用poi的导出word

也是网上找的代码http://53873039oycg.iteye.com/blog/2152009,但是横向合并单元格没成功。只能用很蠢的办法建立了好多table

public void fillTable(XWPFTable table) {   for (int rowIndex = 0; rowIndex < table.getNumberOfRows(); rowIndex++) {      XWPFTableRow row = table.getRow(rowIndex);      row.setHeight(380);      for (int colIndex = 0; colIndex < row.getTableCells().size(); colIndex++) {         XWPFTableCell cell = row.getCell(colIndex);         if (rowIndex % 2 == 0) {            setCellText(cell, " cell " + rowIndex + colIndex + " ", "D4DBED", 1000);         } else {            setCellText(cell, " cell " + rowIndex + colIndex + " ", "AEDE72", 1000);         }      }   }}

public void setCellText(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);   CTTcPr ctPr = cttc.addNewTcPr();   CTShd ctshd = ctPr.addNewShd();   ctshd.setFill(bgcolor);   ctPr.addNewVAlign().setVal(STVerticalJc.CENTER);   cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);   cell.setText(text);}

/** * @Description: 跨列合并 */public void mergeCellsHorizontal(XWPFTable table, int row, int fromCell, int toCell) {   for (int cellIndex = fromCell; cellIndex <= toCell; cellIndex++) {      XWPFTableCell cell = table.getRow(row).getCell(cellIndex);      if (cellIndex == fromCell) {         // The first merged cell is set with RESTART merge value         cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);      } else {         // Cells which join (merge) the first one, are set with CONTINUE         cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);      }   }}

/** * @Description: 跨行合并 * @see http://stackoverflow.com/questions/24907541/row-span-with-xwpftable */public  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);      }   }}

public void setTableWidth(XWPFTable table,String width){   CTTbl ttbl = table.getCTTbl();   CTTblPr tblPr = ttbl.getTblPr() == null ? ttbl.addNewTblPr() : ttbl.getTblPr();   CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr.addNewTblW();   CTJc cTJc=tblPr.addNewJc();   cTJc.setVal(STJc.Enum.forString("center"));   tblWidth.setW(new BigInteger(width));   tblWidth.setType(STTblWidth.DXA);}public void fillTable2(XWPFTable table) {   for (int rowIndex = 0; rowIndex < table.getNumberOfRows(); rowIndex++) {      XWPFTableRow row = table.getRow(rowIndex);      row.setHeight(380);      for (int colIndex = 0; colIndex < row.getTableCells().size(); colIndex++) {         XWPFTableCell cell = row.getCell(colIndex);         if (rowIndex % 2 == 0) {            setCellText(cell, " cell " + rowIndex + colIndex + " ", "D4DBED", 1800);         } else {            setCellText(cell, " cell " + rowIndex + colIndex + " ", "AEDE72", 1800);         }      }   }}public void createTable(XWPFDocument doc,String str) {   XWPFTable table = null;   table = doc.createTable(1, 1);   setTableWidth(table, "9000");   XWPFTableRow row = null;   row = table.getRow(0);   row.setHeight(380);   XWPFTableCell cell = null;   cell = row.getCell(0);   setCellText(cell, str, "CCCCCC", 9000);

   }

@Overridepublic void exportWord(ServletOutputStream outputStream) {   // Document 对应一个word应用文件   XWPFDocument doc = new XWPFDocument();

   /*XWPFTable table1 = doc.createTable(6, 8);   setTableWidth(table1, "8000");   fillTable(table1);   mergeCellsVertically(table1, 1, 1,4);   mergeCellsVertically(table1, 4, 2, 4);   mergeCellsHorizontal(table1, 0, 3, 5);   mergeCellsHorizontal(table1, 2, 2, 3);   mergeCellsHorizontal(table1, 2, 6, 7);*/

   createTable(doc,"renyuanxinxi");

   XWPFTable table1 = doc.createTable(1, 6);   XWPFTableRow row0 = table1.getRow(0);   row0.setHeight(380);   XWPFTableCell cell = null;   cell = row0.getCell(0);   setCellText(cell, " 人员编号", "FFFFFF", 1500);   cell = row0.getCell(1);   setCellText(cell, " 文本", "FFFFFF", 1500);   cell = row0.getCell(2);   setCellText(cell, " 姓名", "FFFFFF", 1500);   cell = row0.getCell(3);   setCellText(cell, " 文本", "FFFFFF", 1500);   cell = row0.getCell(4);   setCellText(cell, " 身份证号", "FFFFFF", 1500);   cell = row0.getCell(5);   setCellText(cell, " 文本", "FFFFFF", 1500);

   createTable(doc,"jiancejihua");

   XWPFTable table2 = doc.createTable(2, 6);   XWPFTableRow row =null;   row.setHeight(380);   row = table2.getRow(0);   cell = row.getCell(0);   setCellText(cell, " 处室", "FFFFFF", 1500);   cell = row.getCell(1);   setCellText(cell, " 文本", "FFFFFF", 1500);   cell = row.getCell(2);   setCellText(cell, " 科室", "FFFFFF", 1500);   cell = row.getCell(3);   setCellText(cell, " 文本", "FFFFFF", 1500);   cell = row.getCell(4);   setCellText(cell, " 单位", "FFFFFF", 1500);   cell = row.getCell(5);   setCellText(cell, " 文本", "FFFFFF", 1500);   row = table2.getRow(1);   cell = row.getCell(0);   setCellText(cell, " 生产单元", "FFFFFF", 1500);   cell = row.getCell(1);   setCellText(cell, " 文本", "FFFFFF", 1500);   cell = row.getCell(2);   setCellText(cell, " 接口人", "FFFFFF", 1500);   cell = row.getCell(3);   setCellText(cell, " 文本", "FFFFFF", 1500);   cell = row.getCell(4);   setCellText(cell, " 监测计划状态", "FFFFFF", 1500);   cell = row.getCell(5);   setCellText(cell, " 文本", "FFFFFF", 1500);

   createTable(doc,"异常描述");   XWPFTable table3 = doc.createTable(4, 4);   row = table3.getRow(0);   //row.setHeight(380);   cell = row.getCell(0);   setCellText(cell, " 监测类型", "FFFFFF", 1500);   cell = row.getCell(1);   setCellText(cell, " 文本", "FFFFFF", 3000);   cell = row.getCell(2);   setCellText(cell, " 异常类型", "FFFFFF", 1500);   cell = row.getCell(3);   setCellText(cell, " 文本", "FFFFFF", 3000);   row = table2.getRow(1);   cell = row.getCell(0);   setCellText(cell, " 机组", "FFFFFF", 1500);   cell = row.getCell(1);   setCellText(cell, " 文本", "FFFFFF", 3000);   cell = row.getCell(2);   setCellText(cell, " 时间", "FFFFFF", 1500);   cell = row.getCell(3);   setCellText(cell, " 文本", "FFFFFF", 3000);   row = table2.getRow(2);   cell = row.getCell(0);   setCellText(cell, " 异常值", "FFFFFF", 1500);   cell = row.getCell(1);   setCellText(cell, " 文本", "FFFFFF", 3000);   cell = row.getCell(2);   setCellText(cell, " 异常值单位", "FFFFFF", 1500);   cell = row.getCell(3);   setCellText(cell, " 文本", "FFFFFF", 3000);   row = table2.getRow(3);   cell = row.getCell(0);   setCellText(cell, " 发起人", "FFFFFF", 1500);   cell = row.getCell(1);   setCellText(cell, " 文本", "FFFFFF", 3000);   cell = row.getCell(2);   setCellText(cell, " 发起时间", "FFFFFF", 1500);   cell = row.getCell(3);   setCellText(cell, " 文本", "FFFFFF", 3000);

   createTable(doc,"辐射防护组调查(500个字符)");   XWPFTable table4 = doc.createTable(1, 2);   row = table4.getRow(0);   row.setHeight(900);   cell = row.getCell(0);   setCellText(cell, "调查描述", "FFFFFF", 1500);   cell = row.getCell(1);   setCellText(cell, " 文本", "FFFFFF", 7500);   XWPFTable table5 = doc.createTable(1, 4);   row = table5.getRow(0);   row.setHeight(380);   cell = row.getCell(0);   setCellText(cell, "调查人", "FFFFFF", 1500);   cell = row.getCell(1);   setCellText(cell, " 文本", "FFFFFF", 3000);   cell = row.getCell(2);   setCellText(cell, "日期", "FFFFFF", 1500);   cell = row.getCell(3);   setCellText(cell, "文本", "FFFFFF", 3000);

   createTable(doc,"个人剂量组调查(500个字符)");   XWPFTable table6 = doc.createTable(1, 2);   row = table6.getRow(0);   row.setHeight(900);   cell = row.getCell(0);   setCellText(cell, "调查描述", "FFFFFF", 1500);   cell = row.getCell(1);   setCellText(cell, " 文本", "FFFFFF", 7500);   XWPFTable table7 = doc.createTable(1, 6);   row = table7.getRow(0);   row.setHeight(380);   cell = row.getCell(0);   setCellText(cell, "名义剂量确定方式", "FFFFFF", 2000);   cell = row.getCell(1);   setCellText(cell, " 文本", "FFFFFF", 2000);   cell = row.getCell(2);   setCellText(cell, "名义剂量/最终值", "FFFFFF", 2000);   cell = row.getCell(3);   setCellText(cell, "文本", "FFFFFF", 2000);   cell = row.getCell(4);   setCellText(cell, "名义剂量/最终值单位", "FFFFFF", 2000);   cell = row.getCell(5);   setCellText(cell, "文本", "FFFFFF", 2000);   XWPFTable table8 = doc.createTable(1, 4);   row = table8.getRow(0);   cell = row.getCell(0);   setCellText(cell, "调查人", "FFFFFF", 1500);   cell = row.getCell(1);   setCellText(cell, " 文本", "FFFFFF", 3000);   cell = row.getCell(2);   setCellText(cell, "日期", "FFFFFF", 1500);   cell = row.getCell(3);   setCellText(cell, "文本", "FFFFFF", 3000);

   createTable(doc,"结论与处理意见(200个字符)");   XWPFTable table9 = doc.createTable(2, 2);   row = table9.getRow(0);   cell = row.getCell(0);   setCellText(cell, "跟踪", "FFFFFF", 1500);   cell = row.getCell(1);   setCellText(cell, "", "FFFFFF", 7500);   row = table9.getRow(1);   cell = row.getCell(0);   setCellText(cell, "结论", "FFFFFF", 1500);   cell = row.getCell(1);   setCellText(cell, "", "FFFFFF", 7500);   XWPFTable table10 = doc.createTable(1, 4);   row = table10.getRow(0);   cell = row.getCell(0);   setCellText(cell, "审核人", "FFFFFF", 1500);   cell = row.getCell(1);   setCellText(cell, " 文本", "FFFFFF", 3000);   cell = row.getCell(2);   setCellText(cell, "日期", "FFFFFF", 1500);   cell = row.getCell(3);   setCellText(cell, "文本", "FFFFFF", 3000);

   XWPFTable table11 = doc.createTable(1, 1);   setTableWidth(table11, "9000");   row = table11.getRow(0);   row.setHeight(380);   cell = row.getCell(0);   setCellText(cell, "提交辐射防护组调查   提交个人剂量组调查  提交审核  审核  导出  打印调查尿样标签", "FFFFFF", 9000);

   try {      doc.write(outputStream);      outputStream.flush();      outputStream.close();   } catch (IOException e) {      e.printStackTrace();   }


//加个表格      /*XWPFTable tab = docx.createTable(2,3);      tab.setWidth(10000);      //增加一行      XWPFTableRow row1  = tab.createRow();      row1.setHeight(3500);      //增加列      XWPFTableCell cell1 =null;      cell1 = row1.createCell();      cell1.setText("姓名");      String hql="select t from TPdmisExceptionEntity t where t.userid=‘511021196307179130‘";      List<TPdmisExceptionEntity> tPdmisExceptionEntityList = commonDao.findByQueryString(hql);      cell1 = row1.createCell();      cell1.setText(tPdmisExceptionEntityList.get(0).getUname());

      cell1 = row1.createCell();      XWPFParagraph pIO =cell1.addParagraph();      XWPFRun rIO = pIO.createRun();      rIO.setFontFamily("宋体");      rIO.setFontSize(8);      rIO.setBold(true);      rIO.setText(tPdmisExceptionEntityList.get(0).getUname());

      XWPFParagraph p1 = docx.createParagraph();      XWPFRun r1 = p1.createRun();      r1.setText("hello world");*/

      /*try {         doc.write(outputStream);         outputStream.flush();         outputStream.close();      } catch (IOException e) {         e.printStackTrace();      }*/

      /*//创建一个5行5列的表格      XWPFTable table = doc.createTable(5, 5);      //这里增加的列原本初始化创建的那5行在通过getTableCells()方法获取时获取不到,但通过row新增的就可以。//    table.addNewCol(); //给表格增加一列,变成6列      table.createRow(); //给表格新增一行,变成6行      List<XWPFTableRow> rows = table.getRows();      //表格属性      CTTblPr tablePr = table.getCTTbl().addNewTblPr();      //表格宽度      CTTblWidth width = tablePr.addNewTblW();      width.setW(BigInteger.valueOf(8000));      XWPFTableRow row;      List<XWPFTableCell> cells;      XWPFTableCell cell;      int rowSize = rows.size();      int cellSize;      for (int i = 0; i < rowSize; i++) {         row = rows.get(i);         //新增单元格         row.addNewTableCell();         //设置行的高度         row.setHeight(500);         //行属性//       CTTrPr rowPr = row.getCtRow().addNewTrPr();         //这种方式是可以获取到新增的cell的。//       List<CTTc> list = row.getCtRow().getTcList();         cells = row.getTableCells();         cellSize = cells.size();         for (int j = 0; j < cellSize; j++) {            cell = cells.get(j);            if ((i + j) % 2 == 0) {               //设置单元格的颜色               cell.setColor("ff0000"); //红色            } else {               cell.setColor("0000ff"); //蓝色            }            //单元格属性            CTTcPr cellPr = cell.getCTTc().addNewTcPr();            cellPr.addNewVAlign().setVal(STVerticalJc.CENTER);            if (j == 3) {               //设置宽度               cellPr.addNewTcW().setW(BigInteger.valueOf(3000));            }            cell.setText(i + ", " + j);         }         }*/
时间: 2024-10-12 16:32:37

记录一下表格用poi的导出word的相关文章

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

转载请注明出处:https://www.cnblogs.com/sun-flower1314/p/10128796.html 本篇是关于利用Apache 的POI导出Word的实现步骤.采用XWPFDocument导出Word,结构和样式完全由代码控制,操作起来还是非常的不太方便,只能够创建简单的word,不能设置样式,功能太少.但在这里还是实现一下,毕竟做过. 首先声明一些基本概念: XWPFDocument代表一个docx文档,其可以用来读docx文档,也可以用来写docx文档 XWPFPa

poi导出word表格跨行

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

Struts2利用iText导出word文档(包含表格)以提供下载

J2EE ExcelStrutsXML 在公司实习期间,带我的老师让我实现一功能——在显示课表的页面上上点击“导出文件“时能以word文档形式下载课表.将课表导出到excel里的功能他们已经实现了,用的是Struts2+poi实现的.poi对excel表格操作能力很强,但是对word文档的支持一直没有更新,操作能力有限. iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成PDF或rtf 的文档,而且可以将XML.Ht

利用jQuery-Word-Export导出word (含表格,ECharts)

写在前面的话:写博客的初衷是想把自己学到的知识总结下来,在写的过程中,相当于又把知识梳理了一遍.我坚信有输入,有输出,技术才会进步.我一般都会自己写一个小demo,测试没有问题,再进行整理. 在实际做项目的过程中,遇到问题,也是各种查,所以很感谢把知识分享出来的人,而我也愿意把我在项目中遇到的问题写下来,一来是巩固自己的知识点,二来如果能帮助到别人,那就更好啦. 我写的有些方法,看来有些笨,我也会继续探索和研究.如有更好的方法,可以一起交流. 正文开始~~~~ 一.jQuery-Word-Exp

POI跨行导出excell表格实现.md

# POI跨行导出excell表格实现### 需求:需要导出父子级关系的元素,父级需要跨行显示,子级需要单独显示. ### 由于数据库中的数据存的是子级存父级的ID形式,所以需要将数据包装,自己将装在父级下的list中,本案例将会包装成五级子级形式. ### 第一步:首先写sql ,sql中要计算出父级需要跨的行数.(思路:首先自身内连接,计算出每一级需要跨的行数,在与主表相连将跨行数关联上去) ```xml <select id="findList" resultType=&q

POI跨行导出excell表格实现

POI跨行导出excell表格实现 需求:需要导出父子级关系的元素,父级需要跨行显示,子级需要单独显示. 由于数据库中的数据存的是子级存父级的ID形式,所以需要将数据包装,自己将装在父级下的list中,本案例将会包装成五级子级形式. 第一步:首先写sql ,sql中要计算出父级需要跨的行数.(思路:首先自身内连接,计算出每一级需要跨的行数,在与主表相连将跨行数关联上去) <select id="findList" resultType="OcScope">

freemarker导出word——让表格数据行数 列数自动变化

行数.列数变化只需定义一个List<List<T>> freemarker遍历的话,只需要使用freemarker的标记性语言<#list report.qc_third_agentTable as  table2_tr>遍历即可,如图 实现的效果 freemarker导出word--让表格数据行数 列数自动变化,布布扣,bubuko.com

java将html导出成word(利用的poi包导出)

//导出到word public void exportWord(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception { DBTool tool = null ; try { /*** 查询数据库获得数据 **/ ArrayList records = form.getRecords(); if(

poi导出word模板项目实例(一个文件)

在页面上填写值,然后导出到word模板中,并把页面上的值带到模板中,也就是导出word文档,提前有word 的模板形式, 1.jsp 页面   <table class="formTable"> <TR> <TD class="label">会议地点</TD> <TD class="content"> <INPUT id="meetingSite" type=&