一、效果如下
二、js代码
function export_word(){ //导出word var url = "czzsca/exportWord.do"; this.export(url); } function export(url){ var currentyear = $("#mainYear").val() * 1; var key_columns = ['xh',"d_name","d_unit","d_number","d_estimate","d_lastyear","jnjh","d_remarks","d_depart","d_executeunit"]; var value_columns = ['序号','项目名称','单位','数量','总概算','至'+(currentyear-1)+'年完成',currentyear+'年计划','附注','责任部门','计划执行单位']; window.location.href= url+"?key_columns="+key_columns+"&value_columns="+value_columns+"&title="+currentyear+"年 更新改造计划 正式计划草案"; }
三、controller代码
(controller代码比较长,主要是格式化每一行的数据,主要是往工具类里传List>参数,泛型的List
//导出word @RequestMapping("/exportWord.do") @ResponseBody public ReturnMsg exportWord(String[] key_columns,String[] value_columns,String title,HttpServletResponse response){ ReturnMsg rm = new ReturnMsg(); try { response.setCharacterEncoding("UTF-8"); response.setContentType("application/msexcle"); response.setHeader("content-disposition", "attachment;filename="+new String("正式计划草案".getBytes("gb2312"),"ISO8859-1")+".doc"); List> cOntent= new ArrayList
>(); List
> head = new ArrayList
>(); String jnjhzh = new BigDecimal(this.map.get("jnjhzh")==null||"".equals(this.map.get("jnjhzh")+"")?"0":this.map.get("jnjhzh")+"").setScale(1, RoundingMode.UP)+""; String trimpro = new BigDecimal(this.map.get("tzjh")==null||"".equals(this.map.get("tzjh")+"")?"0":this.map.get("tzjh")+"").setScale(1, RoundingMode.UP)+""; String temp = new BigDecimal(trimpro).subtract(new BigDecimal(jnjhzh))+""; if("0".equals(temp)) { temp = ""; } head = ExportUtil.getCzzscaList(jnjhzh, trimpro, temp); Map dounit = new HashMap(); dounit.put("dwlx", 0); dounit.put("user_type", 1); List
四、工具类代码
/** * * @Date 2018年7月19日 上午11:19:04 * @Description 导出word * @Fcunction exportWord * @param title * @param value_columns * @param list * @return XWPFDocument * */ public static XWPFDocument exportWord(String title,String[] value_columns, List> list,int[] colWidths,int one,int two) { XWPFDocument doc= new XWPFDocument(); CTDocument1 document = doc.getDocument(); CTBody body = document.getBody(); if(!body.isSetSectPr()){ body.addNewSectPr(); } CTSectPr section = body.getSectPr(); if(!section.isSetPgSz()){ section.addNewPgSz(); } CTPageSz pageSize = section.getPgSz(); pageSize.setW(BigInteger.valueOf(15840)); pageSize.setH(BigInteger.valueOf(12240)); pageSize.setOrient(STPageOrientation.LANDSCAPE); //添加标题 XWPFParagraph titleParagraph = doc.createParagraph(); //设置段落居中 titleParagraph.setAlignment(ParagraphAlignment.CENTER); XWPFRun titleParagraphRun = titleParagraph.createRun(); titleParagraphRun.setText(title); titleParagraphRun.setColor("000000"); titleParagraphRun.setFontSize(20); //表格 XWPFTable ComTable = doc.createTable(); //设置指定宽度 CTTbl ttbl = ComTable.getCTTbl(); CTTblGrid tblGrid = ttbl.addNewTblGrid(); for (int i : colWidths) { CTTblGridCol gridCol = tblGrid.addNewGridCol(); gridCol.setW(new BigInteger(i+"")); } //表头 XWPFTableRow rowHead = ComTable.getRow(0); XWPFParagraph cellParagraph = rowHead.getCell(0).getParagraphs().get(0); cellParagraph.setAlignment(ParagraphAlignment.CENTER); //设置表头单元格居中 XWPFRun cellParagraphRun = cellParagraph.createRun(); cellParagraphRun.setFontSize(10); //设置表头单元格字号 cellParagraphRun.setBold(true); //设置表头单元格加粗 cellParagraphRun.setText(value_columns[0]); for (int i = 1; i
-1) { cellParagraph = rowHead.addNewTableCell().getParagraphs().get(0); cellParagraph.setAlignment(ParagraphAlignment.CENTER); //设置表头单元格居中 cellParagraphRun = cellParagraph.createRun(); cellParagraphRun.setFontSize(10); //设置表头单元格居中 cellParagraphRun.setBold(true); cellParagraphRun.setText("增减(+ / -)"); }else { cellParagraph = rowHead.addNewTableCell().getParagraphs().get(0); cellParagraph.setAlignment(ParagraphAlignment.CENTER); //设置表头单元格居中 cellParagraphRun = cellParagraph.createRun(); cellParagraphRun.setFontSize(10); //设置表头单元格居中 cellParagraphRun.setBold(true); //设置表头单元格加粗 cellParagraphRun.setText(value_columns[i]); } } int rows = list.size(); //表格内容 for (int i = 0; i 0) { return false; } return true; } /** * * @Date 2018年7月19日 上午11:44:48 * @Description 正式计划草案获取前两行 * @Fcunction getCzzscaList * @param jnjhzh * @param trimpro * @param temp * @return List > * */ public static List
> getCzzscaList(String jnjhzh,String trimpro,String temp) { List
> list = new ArrayList
>(); List
l1 = new ArrayList (); //固定第一行 l1.add(""); l1.add("年度总计"); l1.add(""); l1.add(""); l1.add(""); l1.add(""); l1.add(jnjhzh); l1.add(""); l1.add(""); l1.add(""); list.add(l1); List l2 = new ArrayList (); //固定第二行 l2.add(""); l2.add("搜索总计"); l2.add(""); l2.add(""); l2.add(""); l2.add(""); l2.add(""); // l2.add(trimpro+"\r100%"); l2.add(""); l2.add(""); l2.add(""); list.add(l2); return list; }
五、详解:
1.设置纸张大小
(1)创建完XWPFDocument后,默认为A4纸张,如需调整纸张大小,这里我是要用A3大小使用如下代码
XWPFDocument doc= new XWPFDocument(); CTDocument1 document = doc.getDocument(); CTBody body = document.getBody(); if(!body.isSetSectPr()){ body.addNewSectPr(); } CTSectPr section = body.getSectPr(); if(!section.isSetPgSz()){ section.addNewPgSz(); } CTPageSz pageSize = section.getPgSz(); pageSize.setW(BigInteger.valueOf(15840)); pageSize.setH(BigInteger.valueOf(12240)); pageSize.setOrient(STPageOrientation.LANDSCAPE);
(2)默认A4纸张大小的话只用XWPFDocument
XWPFDocument doc= new XWPFDocument();
(3)使用CTPageSz类需要导入ooxml-schemas的jar包(所有需要的jar包maven依赖在底部)
2.单元格列宽度
(1)每一列宽度相等自适应,不需单独设置每一列宽度的话使用如下代码
//表格 XWPFTable ComTable = doc.createTable(); //表格自适应宽度 CTTblWidth comTableWidth = ComTable.getCTTbl().addNewTblPr().addNewTblW(); comTableWidth.setType(STTblWidth.DXA); comTableWidth.setW(BigInteger.valueOf(9072));
(2)单独设置每列宽度
//表格 XWPFTable ComTable = doc.createTable(); //设置指定宽度 CTTbl ttbl = ComTable.getCTTbl(); CTTblGrid tblGrid = ttbl.addNewTblGrid(); int[] colWidths = new int[] { 600, 3000, 500, 500, 800, 800 ,800,3000,800,1500}; for (int i : colWidths) { CTTblGridCol gridCol = tblGrid.addNewGridCol(); gridCol.setW(new BigInteger(i+"")); }
(3)使用CTTblGrid类需要导入poi-ooxml-schemas的jar包(所有需要的jar包maven依赖在底部)
3.单元格内字体样式
(1)不需要设置单元格字体
for (int i = 0; i
(2)设置单元格内字体字号、是否加粗
for (int i = 0; i
4.单元格居中(水平居中、垂直居中)
(1)使用单元格加载内容
for (int i = 0; i
(2)使用单元格的段落加载内容,只用调用垂直居中的方法
for (int i = 0; i
水平居中在3-(2)里调用了段落的方法居中过了
cellParagraphC.setAlignment(ParagraphAlignment.CENTER);
5.单元格合并
(1)跨列合并单元格
public static 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 ) { cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART); } else { cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE); } } }
(2)跨行合并单元格
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 ) { cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART); } else { cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE); } } }
6.单元格内换行,单元格内使用\r换行没用,可以用addBreak方法
for (int i = 0; i-1) { String[] text = content.split("\r"); for (int k = 0; k
六、使用到的Maven依赖
org.apache.xmlbeans xmlbeans 2.6.0 org.apache.poi poi-ooxml 3.9 org.apache.poi ooxml-schemas 1.1 org.apache.poi poi-ooxml-schemas 3.16
以上这篇poi导出word表格的操作讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。