1. import org.apache.commons.lang3.StringUtils;
  2. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  3. import org.apache.poi.ss.usermodel.*;
  4. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  5. import org.springframework.web.multipart.MultipartFile;
  6.  
  7. import java.io.IOException;
  8. import java.math.BigDecimal;
  9. import java.util.*;
  10.  
  11. public class ImportExcelTool {
  12. public static List<Map<String, String>> readExcel(MultipartFile file) throws IOException {
  13. Workbook workbook = null;
  14. String fileName = file.getOriginalFilename();
  15. if (Objects.requireNonNull(fileName).endsWith(".xlsx"))
  16. workbook = new XSSFWorkbook(file.getInputStream());
  17. if (fileName.endsWith(".xls"))
  18. workbook = new HSSFWorkbook(file.getInputStream());
  19. Sheet sheet = Objects.requireNonNull(workbook).getSheetAt(0);
  20. int physicalNumberOfRows = sheet.getPhysicalNumberOfRows();
  21.  
  22. Row row = sheet.getRow(1);
  23. List<String> fieldNames = new ArrayList<>();
  24. for (int i = 0; i < row.getLastCellNum(); i++)
  25. fieldNames.add(row.getCell(i).getRichStringCellValue().getString());
  26.  
  27. List<Map<String, String>> lsMap = new ArrayList<>();
  28.  
  29. for (int j = 3; j < physicalNumberOfRows; j++) {
  30. Map<String, String> dataMap = new HashMap<>();
  31. Row dataRow = sheet.getRow(j);
  32. if (dataRow == null)
  33. break;
  34. for (int i = 0; i < dataRow.getLastCellNum(); i++) {
  35. String val = getCellValue(dataRow.getCell(i));
  36. dataMap.put(fieldNames.get(i), val);
  37. }
  38. if (dataMap.size() == 0)
  39. break;
  40.  
  41. lsMap.add(dataMap);
  42. }
  43. return lsMap;
  44. }
  45.  
  46. private static String getCellValue(Cell cell) {
  47. if (cell == null)
  48. return "";
  49. if (cell.getCellType() == CellType.NUMERIC)
  50. if (DateUtil.isCellDateFormatted(cell))
  51. return DateUtil.getJavaDate(cell.getNumericCellValue()).toString();
  52. else
  53. return new BigDecimal(cell.getNumericCellValue()).toString();
  54. else if (cell.getCellType() == CellType.STRING)
  55. return StringUtils.trimToEmpty(cell.getStringCellValue());
  56. else if (cell.getCellType() == CellType.FORMULA)
  57. return StringUtils.trimToEmpty(cell.getCellFormula());
  58. else if (cell.getCellType() == CellType.BLANK)
  59. return "";
  60. else if (cell.getCellType() == CellType.BOOLEAN)
  61. return String.valueOf(cell.getBooleanCellValue());
  62. else if (cell.getCellType() == CellType.ERROR)
  63. return "ERROR";
  64. else
  65. return cell.toString().trim();
  66.  
  67. }
  68. }

用poi从excel文档导入数据的更多相关文章

  1. struts2中利用POI导出Excel文档并下载

    1.项目组负责人让我实现这个接口,因为以前做过类似的,中间并没有遇到什么太困难的事情.其他不说,先上代码: package com.tydic.eshop.action.feedback; impor ...

  2. Java之Poi导出Excel文档

    一.Poi简介 在后台管理系统中,我们经常要做的导出操作,通常导出为Excel文档的形式,而Poi则提供了这种需要的支持. 二.Workbook/HSSFWorkbook/XSSFWorkbook 1 ...

  3. POI 读取Excel文档中的数据——兼容Excel2003和Excel2007

    Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能. HSSF - 提供读写Microsoft Exce ...

  4. POI导出Excel文档通用工具方法

    import java.lang.reflect.InvocationTargetException; import java.util.List; import java.util.Map; imp ...

  5. 使用struts2和poi导出excel文档

    poi眼下应该是比較流行的操作excel的工具了.这几天做了个struts2和poi结合使用来实现导出excel的功能.个人认为还是比較有用的.代码阅读起来也非常easy.下来就来分享下我的心得 1  ...

  6. excel文档中数据导入sql server注意事项

    进来经常需要对一些基础数据进行更新,而业务方提供的数据源往往都是excel,所以经常需要将excel中数据导入到 数据库临时表,然后再进行处理. 在导入过程中,发现有些数据比如手机号码,如果默认导入, ...

  7. POI之Excel文档增删改查

    需要引用apache第三方lib库poi 支持xls.xlsx格式excel读写操作 package com.hua.excel; import java.io.File;import java.io ...

  8. 【Java】常用POI生成Excel文档设置打印样式

    package poi_test; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi ...

  9. 怎么对比两个excel文档的数据差异

    百度经验: https://jingyan.baidu.com/article/6181c3e0877c7a152ef15304.html

随机推荐

  1. Kendo UI for jQuery使用教程:操作系统/jQuery支持等

    [Kendo UI for jQuery最新试用版下载] Kendo UI目前最新提供Kendo UI for jQuery.Kendo UI for Angular.Kendo UI Support ...

  2. qt5---资源文件

    创建资源文件: 视频教程:https://www.bilibili.com/video/av66748323/ 右击工程目录--->-->--> 添加资源: 右击资源文件--> ...

  3. 在CentOS/Windows下配置Nginx(以及踩坑)

    在CentOS/Windows下配置Nginx(以及踩坑) 1. 序言 因为这类文章网上比较多,实际操作起来也大同小异,所以我并不会着重于详细配置方面,而是将我配置时踩的坑写出来. 2. CentOS ...

  4. kubernetes运用

    1:kubernetes基本介绍 kubernetes 是一个开源的容器集群管理系统,K8s用于容器化应用程序的部署 扩展和管理,提供容器编排 资源调度 弹性伸缩 部署管理  服务发现等一系列功能. ...

  5. Robot Framework中ride.py打不开的解决方法

    1.首先查看wxPython版本是否跟python的版本一致,一般都使用wxPython2.8-win64-unicode-2.8.12.1-py27.exe或者wxPython2.8-win32-u ...

  6. python 的pip安装

    C:\Python27>C:\Python27\Scripts\pip.exe install gevent gevent是安装的模块名

  7. Linux不同机器文件挂载

    由于此前发布项目应用时,需要对两台文件服务器进行文件挂载,所以才实际第一次接触到这个名词,但由于一直以来自己没有真正的去操作过,只是停留在一些理论层次,所以今天记录一下这个实现过程,以备后用. 使用设 ...

  8. spring cloud禁止输出日志:ConfigClusterResolver : Resolving eureka endpoints via configuration

    springcloud的注册中心客户端会每隔一定时间向注册中心服务端发送心跳,用此来判断注册中心服务端是否运行正常. 这样导致不断进行日志输出,不便查看正常的业务日志输出. c.n.d.s.r.aws ...

  9. gulp[13124]: c:\ws\src\node_contextify.cc:626: Assertion `args[1]->IsString()' failed

    在执行gulp sass时报下面错误,又或者执行ionic serve时报这个错,选择低一点版本的node,建议8v; gulp[13124]: c:\ws\src\node_contextify.c ...

  10. 01Two Sum题解

    Tow Sum 原题概述: Given an array of integers, return indices of the two numbers such that they add up to ...