springboot业务功能实战(十二)poi导入excel,并展示数据

举报
小鲍侃java 发表于 2021/09/10 22:38:05 2021/09/10
【摘要】 使用poi导入excel /** * * (读入excel文件,解析后返回) * @param file(文件类型) * @throws IOException * @return List<String[]> */ public static List<St...

使用poi导入excel


  
  1. /**
  2. *
  3. * (读入excel文件,解析后返回)
  4. * @param file(文件类型)
  5. * @throws IOException
  6. * @return List<String[]>
  7. */
  8. public static List<String[]> readExcel(MultipartFile file)
  9. throws IOException {
  10. // 检查文件
  11. checkFile(file);
  12. // 获得Workbook工作薄对象
  13. Workbook workbook = getWorkBook(file);
  14. // 创建返回对象,把每行中的值作为一个数组,所有行作为一个集合返回
  15. List<String[]> list = new ArrayList<String[]>();
  16. if (workbook != null) {
  17. for (int sheetNum = 0; sheetNum < workbook.getNumberOfSheets(); sheetNum++ ) {
  18. // 获得当前sheet工作表
  19. Sheet sheet = workbook.getSheetAt(sheetNum);
  20. if (sheet == null) {
  21. continue;
  22. }
  23. // 获得当前sheet的开始行
  24. int firstRowNum = sheet.getFirstRowNum();
  25. // 获得当前sheet的结束行
  26. int lastRowNum = sheet.getLastRowNum();
  27. // 循环除了第一行的所有行
  28. for (int rowNum = firstRowNum + 1; rowNum <= lastRowNum; rowNum++ ) {
  29. // 获得当前行
  30. Row row = sheet.getRow(rowNum);
  31. if (row == null) {
  32. continue;
  33. }
  34. // 获得当前行的开始列
  35. int firstCellNum = row.getFirstCellNum();
  36. // 获得当前行的列数
  37. int lastCellNum = row.getLastCellNum();
  38. String[] cells = new String[row.getLastCellNum()];
  39. // 循环当前行
  40. for (int cellNum = firstCellNum; cellNum < lastCellNum; cellNum++ ) {
  41. Cell cell=row.getCell(cellNum, Row.MissingCellPolicy.RETURN_BLANK_AS_NULL);
  42. cells[cellNum] = getCellValue(cell);
  43. }
  44. list.add(cells);
  45. }
  46. }
  47. }
  48. return list;
  49. }
  50. /**
  51. *
  52. * (判断文件)
  53. * @param file
  54. * @throws IOException
  55. * @return void
  56. */
  57. public static void checkFile(MultipartFile file)
  58. throws IOException {
  59. // 判断文件是否存在
  60. if (null == file) {
  61. throw new FileNotFoundException("文件不存在!");
  62. }
  63. // 获得文件名
  64. String fileName = file.getOriginalFilename();
  65. // 判断文件是否是excel文件
  66. if (!fileName.endsWith(XLS_TYPE) && !fileName.endsWith(XLSX_TYPE)) {
  67. throw new IOException(fileName + "不是excel文件");
  68. }
  69. }
  70. public static String getCellValue(Cell cell) {
  71. String cellValue = "";
  72. if (cell == null) {
  73. return cellValue;
  74. }
  75. // 把数字当成String来读,避免出现1读成1.0的情况
  76. if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
  77. cell.setCellType(Cell.CELL_TYPE_STRING);
  78. }
  79. // 判断数据的类型
  80. switch (cell.getCellType()) {
  81. // 数字
  82. case Cell.CELL_TYPE_NUMERIC:
  83. cellValue = String.valueOf(cell.getNumericCellValue());
  84. break;
  85. // 字符串
  86. case Cell.CELL_TYPE_STRING:
  87. cellValue = String.valueOf(cell.getStringCellValue());
  88. break;
  89. // Boolean
  90. case Cell.CELL_TYPE_BOOLEAN:
  91. cellValue = String.valueOf(cell.getBooleanCellValue());
  92. break;
  93. // 公式
  94. case Cell.CELL_TYPE_FORMULA:
  95. cellValue = String.valueOf(cell.getCellFormula());
  96. break;
  97. // 空值
  98. case Cell.CELL_TYPE_BLANK:
  99. cellValue = "";
  100. break;
  101. // 故障
  102. case Cell.CELL_TYPE_ERROR:
  103. cellValue = "非法字符";
  104. break;
  105. default:
  106. cellValue = "未知类型";
  107. break;
  108. }
  109. return cellValue;
  110. }
  111. /**
  112. *
  113. * (getWorkBook:(创建WorkBook对象))
  114. * @param file
  115. * @return
  116. * @return Workbook
  117. */
  118. public static Workbook getWorkBook(MultipartFile file) {
  119. // 获得文件名
  120. String fileName = file.getOriginalFilename();
  121. // 创建Workbook工作薄对象,表示整个excel
  122. Workbook workbook = null;
  123. try {
  124. // 获取excel文件的io流
  125. InputStream is = file.getInputStream();
  126. // 根据文件后缀名不同(xls和xlsx)获得不同的Workbook实现类对象
  127. if (fileName.endsWith(XLS_TYPE)) {
  128. // 2003
  129. workbook = new HSSFWorkbook(is);
  130. }
  131. else if (fileName.endsWith(XLSX_TYPE)) {
  132. // 2007
  133. workbook = new XSSFWorkbook(is);
  134. }
  135. }
  136. catch (IOException e) {
  137. e.printStackTrace();
  138. }
  139. return workbook;
  140. }

文章来源: baocl.blog.csdn.net,作者:小黄鸡1992,版权归原作者所有,如需转载,请联系作者。

原文链接:baocl.blog.csdn.net/article/details/83310869

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。