poi判断合并行_POI实战2——实战代码
一、实战整理后的代码如下
@RequestMapping("/batchExport")public void batchExport(HttpServletResponse response, String ids) { String[] idList = ids.split(","); /*创建Excel*/ HSSFWorkbook workbook = new HSSFWorkbook();//创建Excel文件(Workbook) workbook.createInformationProperties();//创建文档信息 DocumentSummaryInformation dsi = workbook.getDocumentSummaryInformation();//摘要信息 dsi.setCategory("Excel文件");//类别 dsi.setManager("");//管理者 dsi.setCompany("");//公司 SummaryInformation si = workbook.getSummaryInformation();//摘要信息// si.setSubject("");//主题 si.setTitle("");//标题 si.setAuthor("");//作者// si.setComments("");//备注 try { for (int i = 0, num = idList.length; i < num; i++) { //获取数据 Listlist = contractService.listProduct(idList[i]); Contract contract = contractService.getContract(idList[i]); if (null!=contract) { HSSFSheet sheet = workbook.createSheet(idList[i]);//创建工作表(Sheet) HSSFRow row = sheet.createRow(0);// 创建行,从0开始 //合并列 HSSFCell cell = row.createCell(0); cell.setCellValue("标题"); CellRangeAddress region1 = new CellRangeAddress(0, 2, 0, 7); sheet.addMergedRegion(region1); HSSFCellStyle style = workbook.createCellStyle(); style.setBorderBottom(BorderStyle.THIN);//下边框 style.setBorderLeft(BorderStyle.THIN);//左边框 style.setBorderRight(BorderStyle.THIN);//右边框 style.setBorderTop(BorderStyle.THIN); //上边框 HSSFFont font = workbook.createFont(); font.setFontHeightInPoints((short) 10);//设置字号// font.setBold(true); //加粗 style.setFont(font); style.setWrapText(true);//換行 Integer l = list.size(); for (int j = 0; j < l; j++) { Product product = list.get(j); HSSFRow rows = sheet.createRow(8 + j); HSSFCell cellj1 = rows.createCell(0); cellj1.setCellValue(product.getProduct_name()); HSSFCell cellj2 = rows.createCell(1); cellj2.setCellValue(product.getProduct_model()); HSSFCell cellj3 = rows.createCell(2); cellj3.setCellValue(product.getProduct_specification()); HSSFCell cellj4 = rows.createCell(3); cellj4.setCellValue(product.getProduct_unit()); HSSFCell cellj5 = rows.createCell(4); cellj5.setCellValue(product.getProduct_num()); HSSFCell cellj6 = rows.createCell(5); cellj6.setCellValue(product.getProduct_unit_price() + "元"); HSSFCell cellj7 = rows.createCell(6); cellj7.setCellValue(product.getMoney() + "元"); HSSFCell cellj8 = rows.createCell(7); cellj8.setCellValue(product.getRemark()); cellj1.setCellStyle(style); cellj2.setCellStyle(style); cellj3.setCellStyle(style); cellj4.setCellStyle(style); cellj5.setCellStyle(style); cellj6.setCellStyle(style); cellj7.setCellStyle(style); cellj8.setCellStyle(style); } //轮到8+l了 HSSFRow row8L = sheet.createRow(8 + l); HSSFCell cell8L_1 = row8L.createCell(0); cell8L_1.setCellValue("合同人民币大写金额:" + NumberToCN.number2CNMontrayUnit(new BigDecimal(contract.getContract_price()))); CellRangeAddress region2 = new CellRangeAddress(8 + l, 8 + l, 0, 5); sheet.addMergedRegion(region2); HSSFCell cell8L_2 = row8L.createCell(6); cell8L_2.setCellValue(contract.getContract_price() + "元"); CellRangeAddress region3 = new CellRangeAddress(8 + l, 8 + l, 6, 7); sheet.addMergedRegion(region3); cell8L_1.setCellStyle(style); cell8L_2.setCellStyle(style); setRegionBorder(BorderStyle.THIN,region1,sheet); setRegionBorder(BorderStyle.THIN,region2,sheet); setRegionBorder(BorderStyle.THIN,region3,sheet); sheet.setColumnWidth(0, 20 * 256);//设置第一列的宽度是20个字符宽度 sheet.setColumnWidth(1, 13 * 256);//设置第二列的宽度是13个字符宽度 sheet.setColumnWidth(2, 13 * 256); sheet.setColumnWidth(3, 8 * 256); sheet.setColumnWidth(4, 8 * 256); sheet.setColumnWidth(5, 13 * 256); sheet.setColumnWidth(6, 13 * 256); sheet.setColumnWidth(7, 13 * 256); row.setHeightInPoints(50);//设置行的高度是50个点 row8L.setHeightInPoints(20);//设置行的高度是20个点 HSSFPrintSetup ps = sheet.getPrintSetup(); ps.setLandscape(false); // 打印方向,true:横向,false:纵向 ps.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE); //纸张 sheet.setMargin(HSSFSheet.BottomMargin, (double) 0.5);// 页边距(下) sheet.setMargin(HSSFSheet.LeftMargin, (double) 0.1);// 页边距(左) sheet.setMargin(HSSFSheet.RightMargin, (double) 0.1);// 页边距(右) sheet.setMargin(HSSFSheet.TopMargin, (double) 0.5);// 页边距(上) sheet.setHorizontallyCenter(true);//设置打印页面为水平居中 sheet.setVerticallyCenter(false);//设置打印页面为垂直居中使用POI输出Excel时打印页面的设置 - TOUGHGUYNEU - @EXPLORER使用POI输出Excel时打印页面的设置 - TOUGHGUYNEU - @EXPLORER } } //将文件存到浏览器设置的下载位置 exportExcelByDownload(workbook, response, "批量导出Excel"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } /** * @Description: 解决合并单元格没有边框(边框被覆盖掉) * @MethodName: setRegionBorder * @Param: [border, region, sheet] * @Return: void * @Date: 2020-11-05 **/ private void setRegionBorder(BorderStyle border, CellRangeAddress region, Sheet sheet) { RegionUtil.setBorderBottom(border, region, sheet); RegionUtil.setBorderLeft(border, region, sheet); RegionUtil.setBorderRight(border, region, sheet); RegionUtil.setBorderTop(border, region, sheet); } /** * excel表格直接下载 */ public static void exportExcelByDownload(HSSFWorkbook wb, HttpServletResponse httpServletResponse, String fileName) throws Exception { //响应类型为application/octet- stream情况下使用了这个头信息的话,那就意味着不想直接显示内容 httpServletResponse.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE); //attachment为以附件方式下载 SimpleDateFormat newsdf = new SimpleDateFormat("yyyyMMddHHmmss"); String date = newsdf.format(new Date()); httpServletResponse.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode( fileName + date + ".xls", "utf-8")); /** * 代码里面使用Content-Disposition来确保浏览器弹出下载对话框的时候。 * response.addHeader("Content-Disposition","attachment");一定要确保没有做过关于禁止浏览器缓存的操作 */ httpServletResponse.setHeader("Cache-Control", "No-cache"); httpServletResponse.flushBuffer(); wb.write(httpServletResponse.getOutputStream()); wb.close(); }/** * @Description: 删除rowIndex行,并且上移其它行 * @MethodName: removeRow * @Param: [sheet, rowIndex] * @Return: void * @Date: 2020-11-05**/private static void removeRow(HSSFSheet sheet, int rowIndex) { int lastRowNum=sheet.getLastRowNum(); if(rowIndex>=0&&rowIndex sheet.shiftRows(rowIndex+1,lastRowNum,-1);//将行号为rowIndex+1一直到行号为lastRowNum的单元格全部上移一行,以便删除rowIndex行 } if(rowIndex==lastRowNum){ HSSFRow removingRow=sheet.getRow(rowIndex); if(removingRow!=null) sheet.removeRow(removingRow); }}
js在首篇文章里说了哟
二、很不错的文章
①JAVA POI的使用_林中静月下仙的博客-CSDN博客
链接:https://blog.csdn.net/qq_21137441/article/details/79226171
②POI3切换为POI4修改内容
链接:https://blog.csdn.net/wang_cheng_/article/details/89947369
写完后,我突然感觉为什么不封装个方法,循环数组即可,内容定义在数组里。虽然我也封装了不少,算了毕竟第一次使用POI,下回再说。
三、总结:最好使用封装POI的工具(比如hutool等)去做导入导出,毕竟几行代码就能搞定,面对复杂的文件导出建议使用模板引擎。如果前者都不适用,那就选POI吧,POI功能强大,无所不能,无法无天,哈哈哈。
喜欢就关注我吧,我会努力更新的!
poi判断合并行_POI实战2——实战代码相关推荐
- java合并sheet行_java poi Excel循环合并行
//Java poi 实现循环合并行,还是第一次遇到这种问题 //在网上查了很多资料,都不是自己想要的 //以下为自己研究后,写的一点东西,给大家分享,希望对大家能有思路上的启发,也希望大家能提出宝贵 ...
- R语言sign函数判断数值为正数或者负数实战
R语言sign函数判断数值为正数或者负数实战 目录 R语言sign函数判断数值为正数或者负数实战 #基本语法 #正负数判断 #基本语法 sign(5) # Basic R syntax of sign ...
- elementUI 表格合并单元格-多层级-合并行
elementUI 表格合并单元格-多层级-合并行 需求:使用vue + elementUI 实现如下表格: 省份 城市 区域 人口 贵州 遵义 汇川区 100 红花岗区 100 播州区 100 贵阳 ...
- 『矩阵论笔记』线性判别分析(LDA)最全解读+python实战二分类代码+补充:矩阵求导可以参考
线性判别分析(LDA)最全解读+python实战二分类代码! 文章目录 一.主要思想! 二.具体处理流程! 三.补充二中的公式的证明! 四.目标函数的求解过程! 4.1.优化问题的转化 4.2.拉格朗 ...
- 语音识别实战(python代码)(一)
语音识别实战 (python :pyttsx.SAPI.SpeechLib实例代码)(一) 本文目录: 一.语音识别的基本原理 (1).语音识别的起源与发展 (2).语音识别的基本原理 (3).语音识 ...
- java操作导出Excel(jxl导出WritableWorkbook)jxl合并单元格,单元格的设置,单元格居中、字体、大小、换行、合并行,列宽、自动换行撑起高度、指定特定字符串样式等
new WritableCellFormat().setWrap(true);//通过调整宽度和高度自动换行 1.1 需求描述 MS的电子表格(Excel)是Office的重要成员,是保存统计 ...
- Hugging Face实战(NLP实战/Transformer实战/预训练模型/分词器/模型微调/模型自动选择/PyTorch版本/代码逐行解析)下篇之模型训练
模型训练的流程代码是不是特别特别多啊?有的童鞋看过Bert那个源码写的特别特别详细,参数贼多,运行一个模型百八十个参数的. Transformer对NLP的理解是一个大道至简的感觉,Hugging F ...
- rowspan 动态变化_rowspan相同值合并 怎么动态的rowspan合并行
php 怎么控制表格,相同数据合并显示一行 Html中colspan属性和rowspan属性合并后该单元格的一个是合并行.一个合并列 colspan=2就代表合并他和他同一列的后一个单元格 rowsp ...
- R语言统计代码运行耗时实战:计算代码运行时间、使用proc.time函数计算代码运行时间
R语言统计代码运行耗时实战:计算代码运行时间.使用proc.time函数计算代码运行时间 目录
最新文章
- libmemcached 1.0.11 发布
- 技术03期:自然语言处理NLP【分词篇】
- win7+iis7.5下的asp.net网站发布系列问题
- darknet: ./src/cuda.c:36: check_error: Assertion `0' failed.
- 算法题:找出一个数组中相加值最大的连续序列元素
- 基于Verilog语言的伪随机码的编写
- Numpy 之 where理解
- 打不过 Chrome 的 Firefox,我为什么要选择?
- [技术帖] js中,双引号,引发的问题
- DxO PhotoLab 5 for mac(raw图片处理软件)
- uvm设计分析——tlm
- 心电自动分析技术综述-Phililps DXL / GE Marquette / Glasgow / HES Hannover / Mortara / 福田 / 光电...
- 成为java高级工程师需要什么
- 单片机C语言编程实例分析详解,单片机C语言编程的八个基本规则解析
- 常用运放做跟随器使用总结
- Word重复操作快捷键F4
- 企业微信机器人还能这么玩?
- 本地应用程序_本地化PHP应用程序的“正确方法”,第4部分
- bigemap手机版app如何加载矢量数据
- 数学公式 —— 向量