比较好的实现方式是通过提前做好excel文件,然后使用这个做好的excel文件作为模板,使用poi读入这个文件,将需要的值填入;这样就不要编程来设置行宽的样式;

效果:

代码:

  1. //时间
  2. String checkTime = "yyyy/MM/dd";
  3.  
  4. //人员查询
  5. Set preParticipantNames = new HashSet();
  6.  
  7. HSSFWorkbook wb = new HSSFWorkbook();
  8.  
  9. // 生成一个样式
  10. HSSFCellStyle style = wb.createCellStyle();
  11. this.setHSSFCellBorder(style);
  12. style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 设置单元格为水平对齐的类型
  13. style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
  14. HSSFFont songFont = createSongFont(wb);
  15. style.setFont(songFont);
  16.  
  17. HSSFCellStyle songPoint14FontStyle = wb.createCellStyle();
  18. this.setHSSFCellBorder(songPoint14FontStyle);
  19. songPoint14FontStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
  20. songPoint14FontStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 设置单元格为水平对齐的类型
  21. HSSFFont songPoint14Font = createSongFont(wb);
  22. songPoint14Font.setFontHeightInPoints((short) 14); //设置字号
  23. songPoint14FontStyle.setFont(songPoint14Font);
  24.  
  25. HSSFCellStyle arialFontStyle = wb.createCellStyle();
  26. this.setHSSFCellBorder(arialFontStyle);
  27. arialFontStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
  28. arialFontStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 设置单元格为水平对齐的类型
  29. HSSFFont arialFont = createSongFont(wb);
  30. arialFont.setFontName("Arial");
  31. arialFontStyle.setFont(arialFont);
  32.  
  33. HSSFCellStyle rightBoldStyle = wb.createCellStyle();
  34. rightBoldStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
  35. rightBoldStyle.setAlignment(HSSFCellStyle.ALIGN_RIGHT);// 设置单元格为水平对齐的类型
  36. HSSFFont boldSongFont = createSongFont(wb);
  37. boldSongFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示
  38. rightBoldStyle.setFont(boldSongFont);
  39.  
  40. HSSFCellStyle songPoint22RedFontStyle = wb.createCellStyle();
  41. songPoint22RedFontStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
  42. songPoint22RedFontStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 设置单元格为水平对齐的类型
  43. HSSFFont songPoint22RedFont = createSongFont(wb);
  44. songPoint22RedFont.setFontHeightInPoints((short) 22); //设置字号
  45. songPoint22RedFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示
  46. songPoint22RedFont.setColor(HSSFColor.RED.index);
  47. songPoint22RedFontStyle.setFont(songPoint22RedFont);
  48.  
  49. HSSFCellStyle contentDefaultStyle = wb.createCellStyle();
  50. this.setHSSFCellBorder(contentDefaultStyle);
  51. contentDefaultStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
  52. contentDefaultStyle.setFont(songFont);
  53.  
  54. HSSFCellStyle verticalStyle = wb.createCellStyle();
  55. this.setHSSFCellBorder(verticalStyle);
  56. verticalStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 设置单元格为水平对齐的类型
  57. verticalStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
  58. verticalStyle.setFont(songFont);
  59.  
  60. // 建立新的sheet对象(excel的表单)
  61. HSSFSheet sheet = wb.createSheet(excelFileName);
  62. sheet.setColumnWidth(0,15* 256);
  63. sheet.setColumnWidth(1,15* 256);
  64. sheet.setColumnWidth(2,60* 256);
  65. sheet.setColumnWidth(3,15* 256);
  66.  
  67. sheet.addMergedRegion(new CellRangeAddress(0,1,0,3));
  68. sheet.addMergedRegion(new CellRangeAddress(2,3,0,3));
  69. sheet.addMergedRegion(new CellRangeAddress(4,5,0,0));
  70. sheet.addMergedRegion(new CellRangeAddress(4,5,1,1));
  71. sheet.addMergedRegion(new CellRangeAddress(4,5,2,2));
  72. sheet.addMergedRegion(new CellRangeAddress(4,5,3,3));
  73.  
  74. int beginRowIndex = 6;
  75. int endRowIndex = 6;
  76.  
  77. //要获总记录,下一步来确定合并的单元格的数量
  78. List<TbCheckSamplEventReportVo> detailList = this.checkCountDao.getCheckResultDaily(checkEventCountDto);
  79.  
  80. if (detailList != null && detailList.size() > 0) {
  81. endRowIndex += detailList.size() -1;
  82. log.info("detailList.size() " + detailList.size());
  83. }
  84. sheet.addMergedRegion(new CellRangeAddress(beginRowIndex,endRowIndex,0,0));
  85.  
  86. int sumScore = 0;
  87. for (int i = beginRowIndex; i <= endRowIndex; i++) {
  88. int j = i - beginRowIndex;
  89. sheet.addMergedRegion(new CellRangeAddress(i,i,1,3));
  90. HSSFRow row = sheet.createRow(i);
  91. if (i == beginRowIndex) {
  92. HSSFCell problemTextCell = row.createCell(0);
  93. problemTextCell.setCellValue("问题");
  94. problemTextCell.setCellStyle(verticalStyle);
  95. }
  96.  
  97. StringBuilder contentDetail = new StringBuilder();
  98. if (detailList != null && detailList.size() > 0) {
  99. BigDecimal checkScore = detailList.get(j).getCheckScore() == null ? null : detailList.get(j).getCheckScore();
  100. if (checkScore != null) {
  101. sumScore += checkScore.intValue();
  102. }
  103. if (detailList.get(j).getPreParticipantName() != null) {
  104. preParticipantNames.add(detailList.get(j).getPreParticipantName());
  105. }
  106.  
  107. contentDetail.append(j+1).append(".").append(detailList.get(j).getDesc()).append(" ").append("xxx").append(checkScore == null ? "" : checkScore.intValue())
  108. .append("xxx(").append(detailList.get(j).getCheckTypeName()).append(")");
  109. }
  110.  
  111. HSSFCell cell = row.createCell(1);
  112. cell.setCellValue(contentDetail.toString());
  113. cell.setCellStyle(contentDefaultStyle);
  114.  
  115. HSSFCell cell2 = row.createCell(2);
  116. cell2.setCellStyle(contentDefaultStyle);
  117.  
  118. HSSFCell cell3 = row.createCell(3);
  119. cell3.setCellStyle(contentDefaultStyle);
  120.  
  121. }
  122.  
  123. //合计
  124. sheet.addMergedRegion(new CellRangeAddress(endRowIndex+1,endRowIndex+1,1,3));
  125. //人员
  126. sheet.addMergedRegion(new CellRangeAddress(endRowIndex+2,endRowIndex+2,1,3));
  127.  
  128. HSSFRow row0 = sheet.createRow(0);
  129. HSSFCell row0Cell0 = row0.createCell(0);
  130. row0Cell0.setCellValue("xxxxxx");
  131. row0Cell0.setCellStyle(songPoint22RedFontStyle);
  132.  
  133. HSSFRow row1 = sheet.createRow(2);
  134. HSSFCell row1Cell0 = row1.createCell(0);
  135. row1Cell0.setCellValue(checkEventCountDto.getCompanyName());
  136. row1Cell0.setCellStyle(rightBoldStyle);
  137.  
  138. //5行
  139. HSSFRow row2 = sheet.createRow(4);
  140. HSSFCell row2Cell0 = row2.createCell(0);
  141. row2Cell0.setCellStyle(style);
  142.  
  143. HSSFCell row2Cell1 = row2.createCell(1);
  144. row2Cell1.setCellStyle(style);
  145.  
  146. HSSFCell row2Cell2 = row2.createCell(2);
  147. row2Cell2.setCellStyle(style);
  148. row2Cell2.setCellValue("时间");
  149.  
  150. HSSFCell row2Cell3 = row2.createCell(3);
  151. row2Cell3.setCellValue(checkTime);
  152. row2Cell3.setCellStyle(arialFontStyle);
  153.  
  154. //6行
  155. HSSFRow row3 = sheet.createRow(5);
  156. HSSFCell row3Cell0 = row3.createCell(0);
  157. row3Cell0.setCellStyle(style);
  158.  
  159. HSSFCell row3Cell1 = row3.createCell(1);
  160. row3Cell1.setCellStyle(style);
  161.  
  162. HSSFCell row3Cell2 = row3.createCell(2);
  163. row3Cell2.setCellStyle(style);
  164.  
  165. HSSFCell row3Cell3 = row3.createCell(3);
  166. row3Cell3.setCellStyle(style);
  167.  
  168. //xxxxx
  169. HSSFRow row5 = sheet.createRow(endRowIndex+1);
  170. HSSFCell row5Cell0 = row5.createCell(0);
  171. row5Cell0.setCellValue("合计");
  172. row5Cell0.setCellStyle(style);
  173. HSSFCell row5Cell1 = row5.createCell(1);
  174. row5Cell1.setCellValue(new StringBuilder("xx").append(sumScore).append("xxx").toString());
  175. row5Cell1.setCellStyle(songPoint14FontStyle);
  176. HSSFCell row5Cell2 = row5.createCell(2);
  177. row5Cell2.setCellStyle(songPoint14FontStyle);
  178. HSSFCell row5Cell3 = row5.createCell(3);
  179. row5Cell3.setCellStyle(songPoint14FontStyle);
  180.  
  181. //人员
  182. HSSFRow row6 = sheet.createRow(endRowIndex+2);
  183. HSSFCell row6Cell0 = row6.createCell(0);
  184. row6Cell0.setCellValue("人员");
  185. row6Cell0.setCellStyle(style);
  186.  
  187. StringBuilder tmpNames = new StringBuilder();
  188. if (preParticipantNames.size() > 0){
  189. preParticipantNames.forEach(item -> tmpNames.append(item).append(","));
  190. }
  191. HSSFCell row6Cell1 = row6.createCell(1);
  192. row6Cell1.setCellValue(tmpNames.toString().length() > 0 ? (tmpNames.toString().substring(0,tmpNames.toString().length() -1)) : "");
  193. row6Cell1.setCellStyle(style);
  194.  
  195. HSSFCell row6Cell2 = row6.createCell(2);
  196. row6Cell2.setCellStyle(style);
  197.  
  198. HSSFCell row6Cell3 = row6.createCell(3);
  199. row6Cell3.setCellStyle(style);
  200.  
  201. return wb;

  代码里面的问题:

1、关于cell的style可以使用for循环来做,代码更加简洁;

2、每列的宽度没有做子适应展开,现在四列是用了固定值来设置列宽的。

3、...

java poi3.10.1基本excel使用的更多相关文章

  1. (6) 如何用Apache POI操作Excel文件-----POI-3.10的一个和注解(comment)相关的另外一个bug

    如果POI-3.10往一个工作表(sheet)里面插入数据的话,需要注意了,其有一个不太被容易发现的bug. 被插入的工作表(sheet)里面的单元格没有包含任何的注解(comment)的时候,插入一 ...

  2. [转]Java中导入、导出Excel

    原文地址:http://blog.csdn.net/jerehedu/article/details/45195359 一.介绍 当前B/S模式已成为应用开发的主流,而在企业办公系统中,常常有客户这样 ...

  3. Java中导入、导出Excel

    原文:Java中导入.导出Excel 一.介绍 当前B/S模式已成为应用开发的主流,而在企业办公系统中,常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统.银行系统).或者是:我们已 ...

  4. 我是陌生人 Java中导入、导出Excel

    我是陌生人 Java中导入.导出Excel 一.介绍 当前B/S模式已成为应用开发的主流,而在企业办公系统中,常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统.银行系统).或者是: ...

  5. java的poi技术读取Excel数据到MySQL

    这篇blog是介绍java中的poi技术读取Excel数据,然后保存到MySQL数据中. 你也可以在 : java的poi技术读取和导入Excel了解到写入Excel的方法信息 使用JXL技术可以在 ...

  6. Java利用POI导入导出Excel中的数据

         首先谈一下今天发生的一件开心的事,本着一颗android的心我被分配到了PB组,身在曹营心在汉啊!好吧,今天要记录和分享的是Java利用POI导入导出Excel中的数据.下面POI包的下载地 ...

  7. java的poi技术读取Excel数据

    这篇blog主要是讲述java中poi读取excel,而excel的版本包括:2003-2007和2010两个版本, 即excel的后缀名为:xls和xlsx. 读取excel和MySQL相关: ja ...

  8. java的poi技术写Excel的Sheet

    在这之前写过关于java读,写Excel的blog如下: Excel转Html java的poi技术读,写Excel[2003-2007,2010] java的poi技术读取Excel[2003-20 ...

  9. java的poi技术读取Excel[2003-2007,2010]

    这篇blog主要是讲述java中poi读取excel,而excel的版本包括:2003-2007和2010两个版本, 即excel的后缀名为:xls和xlsx. 读取excel和MySQL相关: ja ...

随机推荐

  1. ajax默认是异步的

    jquery中的ajax 默认情况下为异步请求,即 async:true,可以通过设置参数 asycn:false 到使其同步 $.ajax({ url: 'www.test.com/test/tes ...

  2. @AUTORELEASEPOOL

    Swift 在内存管理上使用的是自动引用计数 (ARC) 的一套方法,在 ARC 中虽然不需要手动地调用像是 retain,release 或者是 autorelease 这样的方法来管理引用计数,但 ...

  3. 用win-acme给windows服务器添加SSL(Let's Encrypt)证书

    本文是我今天用win-acme给windows服务器添加SSL(Let's Encrypt)证书的一个过程,主要是给我自己备忘的. 1.首先先在github上下载最新版的win-acme. 下载地址: ...

  4. MongoDB与RoboMongo的安装+python基本操作MongoDB

        MongoDB(来自于英文单词“Humongous”,中文含义为“庞大”)是可以应用于各种规模的企业.各个行业以及各类应用程序的开源数据库.作为一个适用于敏捷开发的数据库,MongoDB的数据 ...

  5. 字节组数(二进制流)、Base64、图片(文件)、二进制相互之间转换

    using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; ...

  6. javascript创建对象的几种方式?

    javascript创建对象简单的说,无非就是使用内置对象或各种自定义对象,当然还可以用JSON:但写法有很多种,也能混合使用. 1.对象字面量的方式 person={ firstname:" ...

  7. C# 读取文件内容

    读取文件内容有三种方式 全部读取到字符串变量中 一次读取一行 全部读取到字符串数组中,每个数组元素存储一行文本 全部读取到字符串变量 string text = System.IO.File.Read ...

  8. 123457---com.threeObj.Baobaoshizi01--- 宝宝识字01

    com.threeObj.Baobaoshizi01--- 宝宝识字01

  9. PAT 甲级 1037 Magic Coupon (25 分) (较简单,贪心)

    1037 Magic Coupon (25 分)   The magic shop in Mars is offering some magic coupons. Each coupon has an ...

  10. EXCEL生成SQL脚本

    有如下一张数据库表: 当然,Excel也是这个样子的,然后需要通过Excel的命令实现如下的脚本: insert into student(name,age) values('张三',15); 实现的 ...