当前位置:   article > 正文

java || poi操作excel文件【xlsx,xls】_poi-ooxml3.17添加

poi-ooxml3.17添加

导入依赖包,poi和poi-ooxml  jar版本最好一样 

  1. <dependency>
  2. <groupId>org.apache.poi</groupId>
  3. <artifactId>poi-ooxml</artifactId>
  4. <version>3.17</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.apache.poi</groupId>
  8. <artifactId>poi</artifactId>
  9. <version>3.17</version>
  10. </dependency>

------------------------------------------------------------------------------------------------------------------ 

操作excel文件

  1. //xls
  2. public static void showXLSExcel() throws Exception {
  3. HSSFWorkbook workbook=new HSSFWorkbook(new FileInputStream(new File("F:\111.xls")));
  4. HSSFSheet sheet=null;
  5. for (int i = 0; i < workbook.getNumberOfSheets(); i++) {//获取每个Sheet表
  6. sheet=workbook.getSheetAt(i);
  7. for (int j = 0; j < sheet.getPhysicalNumberOfRows(); j++) {//获取每行
  8. HSSFRow row=sheet.getRow(j);
  9. for (int k = 0; k < row.getPhysicalNumberOfCells(); k++) {//获取每个单元格
  10. HSSFCell cell = row.getCell(k);
  11. System.out.print(row.getCell(k)+"\t");
  12. }
  13. System.out.println("---Sheet表"+i+"处理完毕---");
  14. }
  15. }
  16. }
  17. //xlsx
  18. public static void showXLSXExcel() throws Exception {
  19. XSSFWorkbook workbook=new XSSFWorkbook(new FileInputStream(new File("F:\111.xlsx")));
  20. XSSFSheet sheet=null;
  21. for (int i = 0; i < workbook.getNumberOfSheets(); i++) {//获取每个Sheet表
  22. sheet=workbook.getSheetAt(i);
  23. for (int j = 0; j < sheet.getPhysicalNumberOfRows(); j++) {//获取每行
  24. XSSFRow row=sheet.getRow(j);
  25. for (int k = 0; k < row.getPhysicalNumberOfCells(); k++) {//获取每个单元格
  26. XSSFCell cell = row.getCell(k);
  27. System.out.print(row.getCell(k)+"\t");
  28. }
  29. System.out.println("---Sheet表"+i+"处理完毕---");
  30. }
  31. }
  32. }

执行结果:

------------------------------------------------------------------------------------------------------------------

excel读取时兼容处理 

  1. public R importRegion(MultipartFile file,HttpServletResponse response) throws IOException {
  2. String fileName = file.getOriginalFilename();
  3. String cell1= "";
  4. String cell2="";
  5. if(fileName.indexOf(".xlsx") != -1){
  6. XSSFWorkbook workbook=new XSSFWorkbook(file.getInputStream());
  7. XSSFSheet sheet=workbook.getSheetAt(0);
  8. cell1= sheet.getRow(0).getCell(0).toString();
  9. cell2= sheet.getRow(1).getCell(0).toString();
  10. }else{
  11. HSSFWorkbook workbook=new HSSFWorkbook(file.getInputStream());
  12. HSSFSheet sheet=workbook.getSheetAt(0);
  13. cell1= sheet.getRow(0).getCell(0).toString();
  14. cell2= sheet.getRow(1).getCell(0).toString();
  15. }
  16. }

------------------------------------------------------------------------------------------------------------------

做导出时poi操作 多个sheet页

  1. @RequestMapping("etcFXExport")
  2. @ApiOperationSupport(order = 10)
  3. @ApiOperation(value = "导出列表")
  4. public void etcFXExport(HttpServletResponse response, EtcPassRateVO etcPassRate) throws Exception{
  5. List<EtcPassRateVO> list1 ;
  6. String time = LocalDate.now().getYear()+""+LocalDate.now().getMonth()+""+LocalDate.now().getDayOfYear();
  7. String titleName = "****文件"+time;
  8. ServletOutputStream outputStream = response.getOutputStream();
  9. response.setContentType("application/octet-stream; charset=utf-8");
  10. response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(titleName+".xls","UTF-8"));
  11. HSSFWorkbook workbook=new HSSFWorkbook();
  12. //合并单元格
  13. CellRangeAddress region=null;
  14. //设置样式
  15. HSSFCellStyle style = workbook.createCellStyle(); //标题区样式
  16. HSSFCellStyle style2 = workbook.createCellStyle();//填充值区样式
  17. style.setAlignment(HorizontalAlignment.CENTER);//水平靠左
  18. style.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
  19. style2.setAlignment(HorizontalAlignment.LEFT);//水平靠左
  20. style2.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
  21. // 自动换行
  22. style.setWrapText(true);
  23. style2.setWrapText(true);
  24. style.setBorderBottom(BorderStyle.THIN); //下边框
  25. style.setBorderLeft(BorderStyle.THIN);//左边框
  26. style.setBorderTop(BorderStyle.THIN);//上边框
  27. style.setBorderRight(BorderStyle.THIN);//右边框
  28. style2.setBorderBottom(BorderStyle.THIN); //下边框
  29. style2.setBorderLeft(BorderStyle.THIN);//左边框
  30. style2.setBorderTop(BorderStyle.THIN);//上边框
  31. style2.setBorderRight(BorderStyle.THIN);//右边框
  32. // 生成一个字体
  33. HSSFFont font = workbook.createFont();
  34. font.setFontHeightInPoints((short) 14);
  35. font.setBold(true);
  36. font.setColor(HSSFColor.BLACK.index);
  37. font.setFontName("宋体");
  38. HSSFFont font2 = workbook.createFont();
  39. font2.setFontHeightInPoints((short) 12);
  40. font2.setColor(HSSFColor.BLACK.index);
  41. font2.setFontName("宋体");
  42. // 把字体 应用到当前样式
  43. style.setFont(font);
  44. style2.setFont(font2);
  45. HSSFSheet sheet=workbook.createSheet("封闭式出入口");
  46. HSSFRow row = sheet.createRow(0);
  47. HSSFCell cell = null;
  48. cell = row.createCell(0);
  49. cell.setCellValue("封闭式高速出入口车道ETC通过率排名");
  50. cell.setCellStyle(style);
  51. sheet = getTitle(region,sheet,row,cell,etcPassRate,style);
  52. etcPassRate.setVitrualFlag("1");
  53. etcPassRate.setDirecton("0,1");
  54. list1 = etcPassRateService.selectEtcPassRatePage(etcPassRate);
  55. sheet = returnSheet(sheet,workbook,list1,row,cell,style2);
  56. sheet=workbook.createSheet("封闭式入口");
  57. row = sheet.createRow(0);
  58. cell = row.createCell(0);
  59. cell.setCellValue("封闭式高速入口车道ETC通过率排名");
  60. cell.setCellStyle(style);
  61. sheet = getTitle(region,sheet,row,cell,etcPassRate,style);
  62. etcPassRate.setVitrualFlag("1");
  63. etcPassRate.setDirecton("0");
  64. list1 = etcPassRateService.selectEtcPassRatePage(etcPassRate);
  65. sheet = returnSheet(sheet,workbook,list1,row,cell,style2);
  66. sheet=workbook.createSheet("封闭式出口");
  67. row = sheet.createRow(0);
  68. cell = row.createCell(0);
  69. cell.setCellValue("封闭式高速出口车道ETC通过率排名");
  70. cell.setCellStyle(style);
  71. sheet = getTitle(region,sheet,row,cell,etcPassRate,style);
  72. etcPassRate.setVitrualFlag("1");
  73. etcPassRate.setDirecton("1");
  74. list1 = etcPassRateService.selectEtcPassRatePage(etcPassRate);
  75. sheet = returnSheet(sheet,workbook,list1,row,cell,style2);
  76. sheet=workbook.createSheet("开放式出入口");
  77. row = sheet.createRow(0);
  78. cell = row.createCell(0);
  79. cell.setCellValue("开放式高速出入口车道ETC通过率排名");
  80. cell.setCellStyle(style);
  81. sheet = getTitle(region,sheet,row,cell,etcPassRate,style);
  82. etcPassRate.setVitrualFlag("2");
  83. etcPassRate.setDirecton("0,1");
  84. list1 = etcPassRateService.selectEtcPassRatePage(etcPassRate);
  85. sheet = returnSheet(sheet,workbook,list1,row,cell,style2);
  86. sheet=workbook.createSheet("委托管理出入口");
  87. row = sheet.createRow(0);
  88. cell = row.createCell(0);
  89. cell.setCellValue("委托管理高速出入口车道ETC通过率排名");
  90. cell.setCellStyle(style);
  91. sheet = getTitle(region,sheet,row,cell,etcPassRate,style);
  92. etcPassRate.setVitrualFlag("3");
  93. etcPassRate.setDirecton("0,1");
  94. list1 = etcPassRateService.selectEtcPassRatePage(etcPassRate);
  95. sheet = returnSheet(sheet,workbook,list1,row,cell,style2);
  96. sheet=workbook.createSheet("委托管理入口");
  97. row = sheet.createRow(0);
  98. cell = row.createCell(0);
  99. cell.setCellValue("委托管理高速入口车道ETC通过率排名");
  100. cell.setCellStyle(style);
  101. sheet = getTitle(region,sheet,row,cell,etcPassRate,style);
  102. etcPassRate.setVitrualFlag("3");
  103. etcPassRate.setDirecton("0");
  104. list1 = etcPassRateService.selectEtcPassRatePage(etcPassRate);
  105. sheet = returnSheet(sheet,workbook,list1,row,cell,style2);
  106. sheet=workbook.createSheet("委托管理出口");
  107. row = sheet.createRow(0);
  108. cell = row.createCell(0);
  109. cell.setCellValue("委托管理高速出口车道ETC通过率排名");
  110. cell.setCellStyle(style);
  111. sheet = getTitle(region,sheet,row,cell,etcPassRate,style);
  112. etcPassRate.setVitrualFlag("3");
  113. etcPassRate.setDirecton("1");
  114. list1 = etcPassRateService.selectEtcPassRatePage(etcPassRate);
  115. sheet = returnSheet(sheet,workbook,list1,row,cell,style2);
  116. //导出
  117. workbook.write(outputStream);
  118. outputStream.close();
  119. }
  120. //表格前三行
  121. public HSSFSheet getTitle(CellRangeAddress region,HSSFSheet sheet,HSSFRow row,HSSFCell cell,EtcPassRateVO etcPassRate,HSSFCellStyle style){
  122. sheet.setColumnWidth(1, 25 * 256);
  123. sheet.setColumnWidth(2, 25 * 256);
  124. sheet.setColumnWidth(3, 25 * 256);
  125. sheet.setColumnWidth(4, 25 * 256);
  126. sheet.setColumnWidth(5, 25 * 256);
  127. //合并单元格
  128. region=new CellRangeAddress(0, 0, 0, 8);
  129. sheet.addMergedRegion(region);
  130. //添加样式
  131. row.setHeightInPoints(35);
  132. row = sheet.createRow(1);
  133. cell = row.createCell(0);
  134. cell.setCellValue("汇总时间:"+etcPassRate.getStartTime()+"至"+etcPassRate.getEndTime());
  135. cell.setCellStyle(style);
  136. region=new CellRangeAddress(1, 1, 0, 8);
  137. sheet.addMergedRegion(region);
  138. row = sheet.createRow(2);
  139. cell = row.createCell(0);
  140. cell.setCellValue("序号");
  141. cell.setCellStyle(style);
  142. cell = row.createCell(1);
  143. cell.setCellValue("分公司");
  144. cell.setCellStyle(style);
  145. cell = row.createCell(2);
  146. cell.setCellValue("收费所");
  147. cell.setCellStyle(style);
  148. cell = row.createCell(3);
  149. cell.setCellValue("路段");
  150. cell.setCellStyle(style);
  151. cell = row.createCell(4);
  152. cell.setCellValue("站点");
  153. cell.setCellStyle(style);
  154. cell = row.createCell(5);
  155. cell.setCellValue("广场");
  156. cell.setCellStyle(style);
  157. cell = row.createCell(6);
  158. cell.setCellValue("车道");
  159. cell.setCellStyle(style);
  160. cell = row.createCell(7);
  161. cell.setCellValue("交易成功率");
  162. cell.setCellStyle(style);
  163. cell = row.createCell(8);
  164. cell.setCellValue("交易通过率");
  165. cell.setCellStyle(style);
  166. return sheet;
  167. }
  168. //数据填充
  169. public HSSFSheet returnSheet(HSSFSheet sheet,HSSFWorkbook workbook,List<EtcPassRateVO> list1,HSSFRow row,HSSFCell cell,HSSFCellStyle style2){
  170. HSSFCellStyle style = workbook.createCellStyle();
  171. style.setAlignment(HorizontalAlignment.CENTER);
  172. int y=3,i=1;
  173. for(EtcPassRateVO etc:list1){
  174. row = sheet.createRow(y);
  175. row.setHeightInPoints(18);
  176. cell = row.createCell(0);
  177. cell.setCellStyle(style);
  178. cell.setCellValue(i);
  179. cell.setCellStyle(style2);
  180. cell = row.createCell(1);
  181. cell.setCellStyle(style);
  182. cell.setCellValue(etc.getDeptName2());
  183. cell.setCellStyle(style2);
  184. cell = row.createCell(2);
  185. cell.setCellStyle(style);
  186. cell.setCellValue(etc.getDeptName());
  187. cell.setCellStyle(style2);
  188. cell = row.createCell(3);
  189. cell.setCellStyle(style);
  190. cell.setCellValue(etc.getRoadName());
  191. cell.setCellStyle(style2);
  192. cell = row.createCell(4);
  193. cell.setCellStyle(style);
  194. cell.setCellValue(etc.getStationName());
  195. cell.setCellStyle(style2);
  196. cell = row.createCell(5);
  197. cell.setCellStyle(style);
  198. cell.setCellValue(etc.getPlazaName());
  199. cell.setCellStyle(style2);
  200. cell = row.createCell(6);
  201. cell.setCellStyle(style);
  202. cell.setCellValue(etc.getLane());
  203. cell.setCellStyle(style2);
  204. cell = row.createCell(7);
  205. cell.setCellStyle(style);
  206. cell.setCellValue(etc.getSuccessRate());
  207. cell.setCellStyle(style2);
  208. cell = row.createCell(8);
  209. cell.setCellStyle(style);
  210. cell.setCellValue(etc.getDealPassRate());
  211. cell.setCellStyle(style2);
  212. y=y+1;
  213. i=i+1;
  214. }
  215. return sheet;
  216. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/170192
推荐阅读
相关标签
  

闽ICP备14008679号