具体java通过POI读写Excel的基本使用方法可参考:
POI读写Excel的基本使用

1.项目导入依赖:

<!--xls-->
<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.9</version>
</dependency><!--xlsx-->
<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.9</version>
</dependency>

2.自己根据需求构建的工具类

/*** 设置单元格样式的工具类可根据自身需求自行构建*/
public class PoiUtil {/**** @param workbook* @param fontSize* @return 表头单元格样式*/private static CellStyle createHeadCellStyle(Workbook workbook, short fontSize) {//创建cellStyle对象CellStyle style = workbook.createCellStyle();//设置边框style.setBorderBottom(CellStyle.BORDER_THIN);style.setBorderLeft(CellStyle.BORDER_THIN);style.setBorderRight(CellStyle.BORDER_THIN);style.setBorderTop(CellStyle.BORDER_THIN);//设置文字居中style.setAlignment(CellStyle.ALIGN_CENTER);//设置字体大小、加粗Font font = workbook.createFont();font.setFontHeightInPoints(fontSize);font.setBoldweight(Font.BOLDWEIGHT_BOLD);//设置文字样式生效style.setFont(font);return style;}/**** @param workbook* @param fontSize* @return 表信息单元格样式*/private static CellStyle createMesCellStyle(Workbook workbook, short fontSize) {//创建cellStyle对象CellStyle style = workbook.createCellStyle();//设置边框style.setBorderBottom(CellStyle.BORDER_THIN);style.setBorderLeft(CellStyle.BORDER_THIN);style.setBorderRight(CellStyle.BORDER_THIN);style.setBorderTop(CellStyle.BORDER_THIN);//设置文字居中style.setAlignment(CellStyle.ALIGN_CENTER);//设置字体大小以及字体水平&垂直居中Font font = workbook.createFont();style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);font.setFontHeightInPoints(fontSize);//设置自动换行style.setWrapText(true);//设置文字样式生效style.setFont(font);return style;}/*** 设置合并单元格后的边框* @param border* @param cellRangeAddress* @param sheet* @param workbook*/private static void setCellRangeAddress(int border, CellRangeAddress cellRangeAddress, Sheet sheet,Workbook workbook){//设置合并单元格的边框RegionUtil.setBorderBottom(border, cellRangeAddress, sheet,workbook);RegionUtil.setBorderTop(border, cellRangeAddress, sheet,workbook);RegionUtil.setBorderLeft(border, cellRangeAddress, sheet,workbook);RegionUtil.setBorderRight(border, cellRangeAddress, sheet,workbook);}/*** 自适应宽度(中文支持)* @param sheet* @param size*/private static void setSizeColumn(Sheet sheet, int size) {for (int columnNum = 0; columnNum < size; columnNum++) {int columnWidth = sheet.getColumnWidth(columnNum) / 256;for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) {Row currentRow;//当前行未被使用过if (sheet.getRow(rowNum) == null) {currentRow = sheet.createRow(rowNum);} else {currentRow = sheet.getRow(rowNum);}if (currentRow.getCell(columnNum) != null) {Cell currentCell = currentRow.getCell(columnNum);if (currentCell.getCellType() == XSSFCell.CELL_TYPE_STRING) {int length = currentCell.getStringCellValue().getBytes().length;if (columnWidth < length) {columnWidth = length;}}}}sheet.setColumnWidth(columnNum, columnWidth * 256);}}}

3.生成表结构的代码

public String generatePlanExcel(@RequestParam(value = "planId") int planId) throws Exception{// 1.创建新的Excel工作簿(workbook)// 1.1 07版本的Excel需要XSSFWorkbook对象Workbook workbook = new XSSFWorkbook();// 2.使用workbook创建sheet// 2.1在Excel工作簿中建一工作表(sheet),其名为缺省值 Sheet0//Sheet sheet = workbook.createSheet();// 2.2如要新建一名为"预案详细信息"的工作表,其语句为:Sheet sheet = workbook.createSheet("预案详细信息");//合并预案名称单元格,起始行,结束行,起始列,结束列CellRangeAddress callRangeAddress = new CellRangeAddress(0,0,0,7);sheet.addMergedRegion(callRangeAddress);//合并资源明细单元格,起始行,结束行,起始列,结束列CellRangeAddress cellResourceAddress = new CellRangeAddress(4,4,0,7);sheet.addMergedRegion(cellResourceAddress);//查询相关预案信息PlanRecordAndResources planRecordAndResources = taskAndPlanService.checkPlanById(planId);//封装预案的值信息List<String> planDataCellList = new ArrayList<String>();planDataCellList.add(planRecordAndResources.getName());planDataCellList.add(planRecordAndResources.getTargetName());planDataCellList.add(planRecordAndResources.getTargetCoordinates());planDataCellList.add(planRecordAndResources.getSupportStartTime());planDataCellList.add(planRecordAndResources.getSupportAutomatedTime());planDataCellList.add(planRecordAndResources.getSupportResourceDetail());planDataCellList.add(planRecordAndResources.getAddDate());planDataCellList.add(planRecordAndResources.getModifyDate());//封装资源的值信息List<Map<Integer,String>> resourceDataCellList = new ArrayList<Map<Integer,String>>();List<PlanResources> resourcesList = planRecordAndResources.getResourcesList();for (PlanResources planResources : resourcesList) {Map<Integer,String> resourceDataMap = new HashMap<Integer,String>();resourceDataMap.put(0,planResources.getHospitalName());resourceDataMap.put(1,planResources.getDepartmentName());resourceDataMap.put(2,planResources.getResourceName());resourceDataMap.put(3,planResources.getResourceNum()+"");resourceDataMap.put(4,planResources.getResourceNote());resourceDataMap.put(5,planResources.getAddDate());resourceDataMap.put(6,planResources.getModifyDate());resourceDataCellList.add(resourceDataMap);}String[] planRowName = {"序号","保障目标名称","保障目标坐标","预案开始时间","保障自动执行时间","预案保障资源明细","预案添加时间","预案修改时间"};String[] resourceRowName = {"序号","医院名称","科室名称","资源类别名称","资源所需数量","备注","资源信息添加时间","资源信息修改时间"};//预案名称的样式CellStyle planHeadStyle = PoiUtil.createHeadCellStyle(workbook,(short)16);//预案名称的rowRow planNameRow = sheet.createRow(0);Cell planNameCell = planNameRow.createCell(0);//加载单元格样式planNameCell.setCellStyle(planHeadStyle);//获取并设置预案信息名planNameCell.setCellValue(planDataCellList.get(0));//设置合并后的单元格边框PoiUtil.setCellRangeAddress(1,callRangeAddress,sheet,workbook);//保障地点信息含义的rowRow planRow = sheet.createRow(1);CellStyle planMeanStyle = PoiUtil.createHeadCellStyle(workbook,(short)12);//保障地点信息的rowRow planDataRow = sheet.createRow(2);CellStyle planDataStyle = PoiUtil.createMesCellStyle(workbook,(short)11);for (int i = 0; i < 8; i++) {//向保障地点信息含义的cell中设置内容Cell planMeanCell = planRow.createCell(i);planMeanCell.setCellValue(planRowName[i]);planMeanCell.setCellStyle(planMeanStyle);//向保障地点信息的cell中设置内容Cell planDataCell = planDataRow.createCell(i);if(i==0){planDataCell.setCellValue(1);planDataCell.setCellStyle(planDataStyle);continue;}planDataCell.setCellValue(planDataCellList.get(i));planDataCell.setCellStyle(planDataStyle);}//资源明细的cellStyleCellStyle resourceHeadStyle = PoiUtil.createHeadCellStyle(workbook,(short)16);Row resourceNameRow = sheet.createRow(4);Cell resourceNameCell = resourceNameRow.createCell(0);//加载单元格样式resourceNameCell.setCellStyle(resourceHeadStyle);//获取并设置预案信息名resourceNameCell.setCellValue("资源调配明细");//设置合并后的单元格边框PoiUtil.setCellRangeAddress(1,cellResourceAddress,sheet,workbook);//资源信息含义的rowRow resourceRow = sheet.createRow(5);CellStyle resourceMeanStyle = PoiUtil.createHeadCellStyle(workbook,(short)12);//资源信息的rowCellStyle resourceDataStyle = PoiUtil.createMesCellStyle(workbook,(short)11);//资源row集合List<Row> resourceDataRowList = new ArrayList<Row>();for (int i = 0; i < resourceDataCellList.size(); i++) {//资源信息的rowRow resourceDataRow = sheet.createRow(6+i);resourceDataRowList.add(resourceDataRow);}int num = 0;for (int i = 0; i < 8; i++) {//向资源信息含义的cell中设置内容Cell cell = resourceRow.createCell(i);cell.setCellValue(resourceRowName[i]);cell.setCellStyle(resourceMeanStyle);for (int j = 0; j < resourceDataCellList.size(); j++) {Cell cell1 = resourceDataRowList.get(j).createCell(i);//向资源信息的cell中设置内容if(i==0){cell1.setCellValue(++num);cell1.setCellStyle(resourceDataStyle);continue;}cell1.setCellValue(resourceDataCellList.get(j).get(i-1));cell1.setCellStyle(resourceDataStyle);}}PoiUtil.setSizeColumn(sheet,8);// 6.新建一输出文件流(注意:要先创建文件夹)FileOutputStream out = new FileOutputStream("F:/PoiTest/国家体育馆预案.xlsx");// 7.把相应的Excel 工作簿存盘workbook.write(out);// 8.操作结束,关闭文件out.close();return ResponseTemplate.response(ResponseModel.SUCCESS);}

4.结果

生成表格的代码逻辑是我自己的,工具类里面有对应解决自适应列宽(中文支持)以及合并单元格后边框的添加还有样式如何设置如何使其生效的方法,自行参考。

java通过poi生成excel表格(自适应列宽、合并单元格后的边框添加)相关推荐

  1. C# Excel 行高,列宽,合并单元格,单元格边框线,冻结

    C# Excel 行高,列宽,合并单元格,单元格边框线,冻结 原文:http://hi.baidu.com/kjkj911/blog/item/0ecc3ec7855dd6d4d100600f.htm ...

  2. C# Excel 行高,列宽,合并单元格,单元格边框线,冻结(转载) - 关于C#操作EXCLE常见操作比较全的

    网上看到的比较全的关于C#操作EXCEL常见操作集合,比较全,写的不错 原文地址:http://hi.baidu.com/kjkj911/blog/item/0ecc3ec7855dd6d4d1006 ...

  3. java导出excel 边框不全_POI 导出Excel合并单元格后部分边框不显示

    用户需要导出自定义表格,其中合并单元格样式遇到的问题,合并后只显示第一行第一列的边框,其他边框不显示,于是遍查百度,寻到一点思路 ①了解Excel绘制原理 ②了解绘制Excel顺序 ③绘制Excel单 ...

  4. 计算机表格单元格合并,excel表格数据拆分和合并单元格-excel中如何将已经合并的单元格拆分,并将该单元格......

    Excel怎么把一个合并单元格的内容拆分为几个单元格... 要直接位置得到,则有俩种可能性 第一种,合元格是格得来的假合并单元格,单元格都有内容,直接解除合并即可 第二种,若确实是真实合并单元格,则需 ...

  5. java导出excel 边框不全_POI导出excel,合并单元格后没有边框

    导出的excel合并单元格有两种方法: 第一种: sheet.addMergedRegion(new CellRangeAddress(开始行, 结束行, 开始列, 结束列)); 这样就可以合并单元格 ...

  6. 关于POI合并单元格后加边框问题,请大家拍砖

    最近由于项目需要用到了POI来生成Excel表格,遇到单元格合并以后怎么加边框,老是解决不了,特意上来请教一下 在网上找了一堆,都不行. 自己写了一个笨方法,可以实现列合并加边框,但是行合并就不行了, ...

  7. html table 合并单元格 分页,Word表格跨页,合并单元格后,希望跨页的每一页都显示合并单元格的内容(有图)...

    回答: 一.使用Excel开始菜单中的合并功能 Excel表格中合并单元格有很多中形式,比如:合并居中.合并单元格.按行合并.跨列居中.合并相同单元格和合并内容.接下来看看具体操作步骤吧! 步骤:选中 ...

  8. Vue 导出Excel表格,并实现合并单元格方法

    安装依赖 xlsx , file-saver , script-loader 下载Export2Excel.js文件 在src下创建目录vassets/Export2Excel.js 下载好的: /* ...

  9. java使用poi生成Excel文件并合并单元格

    java使用poi生成Excel文件并合并单元格        业务需要根据 分管部门 字段进行合并,现在提供一种思路. controller层 @Inject(target = "/inf ...

最新文章

  1. 为pony程序添加IACA标记(二)
  2. html文档表示表格的标记,【单选题】在HTML文档中用于表示表格的标记对是( )...
  3. Gartner:2017年应用和基础设施中间件软件市场收入将突破270亿美元
  4. 图解weblogic安装
  5. 自动化测试8大元素定位之xpath语法
  6. ubuntu中snap包管理器的安装、更新删除与简单使用
  7. 局域网打印机反应慢_为什么你的Excel这么慢,这些原因必须要知道!
  8. 漫步线性代数七——特殊矩阵和应用
  9. 编程基础 之 位运算专题
  10. 命名实体识别(NER)资料收集
  11. Leetcode每日一题:976.largest-perimeter-triangle(三角形的最大周长)
  12. 现在做抖音书单挣钱吗?
  13. AppCan VS PhoneGap - 对比两大移动开发平台
  14. python跳出if_python跳出if
  15. NOR Flash、NAND Flash、SPI Flash、CFI Flash
  16. 【目标检测】2020年遥感图像目标检测综述
  17. 关于无法完全停止windowsUpdate的解决方法
  18. 如何用计算机做曲面图,#平面设计图#在电脑上怎么做设计图?
  19. PHPwind9.01傻瓜图解安装教程
  20. epub是什么文件?epub文件怎么打开?

热门文章

  1. cad模型轻量化_BIM模型轻量化到底有啥用?三种解决方案按需挑选!
  2. HAT:Overcoming Catastrophic Forgetting with Hard Attention to the Task
  3. GitHub常见开源协议
  4. 百度AI to B这一年:2019年都做对了什么
  5. 当数据库遇到分布式,你会怎么做?
  6. LINUX目录——FHS标准
  7. R语言|绘制简单小提琴图
  8. redhat 7部署squid(代理)服务
  9. iOS申请上传RCA证书出错 CertificateSigningRequest.certSigningRequest 无效-Invalid CSR Invalid Certi
  10. python破解zip密码