博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java poi导出Excel合并单元格并设置边框
阅读量:4971 次
发布时间:2019-06-12

本文共 3362 字,大约阅读时间需要 11 分钟。

import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFCellStyle;import org.apache.poi.hssf.usermodel.HSSFRow;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.util.CellRangeAddress;import org.apache.poi.ss.util.RegionUtil;//写入Excel,读取模板InputStream    stram= view.getDesktop().getWebApp().getResourceAsStream(File.separator+"Template"+File.separator+"interviewPublicity.xls");HSSFWorkbook workbook = new HSSFWorkbook(stram);HSSFSheet sheet = workbook.getSheetAt(0);//写入数据HSSFRow row = sheet.getRow(0);HSSFCell cell = row.getCell(0);index = 4;//单元格格式row = sheet.getRow(index);cell= row.getCell(0);HSSFCellStyle cellStyle = cell.getCellStyle();int num = 1;for (Entry
>> bmd : bmdMap.entrySet()) { //单位 int deptRowIndex = index; int deptRowEndIndex = deptRowIndex; //岗位集合 for (Entry
> empList : bmd.getValue().entrySet()) { //岗位 int jobRowIndex = index; //考生 for (BasicDBObject emp : empList.getValue()) { if (index > 4) { row = sheet.createRow(index); cell= row.createCell(0); } cell.setCellValue(num++); cell.setCellStyle(cellStyle); cell= row.createCell(3); cell.setCellValue(emp.getString("姓名")); cell.setCellStyle(cellStyle); cell= row.createCell(4); cell.setCellValue(emp.getString("examCardId")); cell.setCellStyle(cellStyle); index++; } row = sheet.getRow(jobRowIndex); if (empList.getValue().size() > 1) { int jobRowEndIndex = jobRowIndex + empList.getValue().size() - 1; //合并单元格 CellRangeAddress cellRange = new CellRangeAddress(jobRowIndex, jobRowEndIndex, (short) 2, (short) 2); sheet.addMergedRegion(cellRange); //添加边框 RegionUtil.setBorderTop(1, cellRange, sheet, workbook); RegionUtil.setBorderBottom(1, cellRange, sheet, workbook); RegionUtil.setBorderLeft(1, cellRange, sheet, workbook); RegionUtil.setBorderRight(1, cellRange, sheet, workbook); } cell= row.createCell(2); cell.setCellValue(empList.getKey()); cell.setCellStyle(cellStyle); deptRowEndIndex += empList.getValue().size(); } row = sheet.getRow(deptRowIndex); if (deptRowEndIndex-1 > deptRowIndex) { //合并单元格 CellRangeAddress cellRange = new CellRangeAddress(deptRowIndex, deptRowEndIndex-1, (short) 1, (short) 1); sheet.addMergedRegion(cellRange); //为合并单元格添加边框 RegionUtil.setBorderTop(1, cellRange, sheet, workbook); RegionUtil.setBorderBottom(1, cellRange, sheet, workbook); RegionUtil.setBorderLeft(1, cellRange, sheet, workbook); RegionUtil.setBorderRight(1, cellRange, sheet, workbook); } cell= row.createCell(1); cell.setCellValue(bmd.getKey()); cell.setCellStyle(cellStyle);}String fileName = getExamPlan().getString("bmbName")+"面试人员名单公示.xls";ByteArrayOutputStream byteOut = new ByteArrayOutputStream();workbook.write(byteOut);byteOut.close();InputStream is = new ByteArrayInputStream(byteOut.toByteArray());Filedownload.save(is, null, fileName);//OFBIZ导出

 

转载于:https://www.cnblogs.com/BobXie85/p/11174787.html

你可能感兴趣的文章
有关去掉谷歌及火狐浏览器文本框 数字类型 上下箭头的方法
查看>>
MySQL数据迁移到SQL Server
查看>>
复杂链表的复制(python)
查看>>
添加日期选择控件
查看>>
jquery.cookie.js操作cookie
查看>>
javascript遍历数组
查看>>
bzoj4765: 普通计算姬 (分块 && BIT)
查看>>
thinkphp5-----模板中函数的使用
查看>>
POJ-3211 Washing Clothes[01背包问题]
查看>>
[BZOJ4832][Lydsy1704月赛]抵制克苏恩
查看>>
数据库三范式
查看>>
看完漫画秒懂区块链
查看>>
开发工具,做一个有效率的开发者
查看>>
对Haskell这门语言的基本认识
查看>>
mysql 安装补充
查看>>
大学里如何学习 ?
查看>>
Oracle命令类别
查看>>
js面试题:关于数组去重的四种方法总结
查看>>
Linux内核分析(三)----初识linux内存管理子系统
查看>>
stc12c5a60s2驱动TEA5767收音机模块硬件调试总结
查看>>