用poi从excel文档导入数据
- import org.apache.commons.lang3.StringUtils;
- import org.apache.poi.hssf.usermodel.HSSFWorkbook;
- import org.apache.poi.ss.usermodel.*;
- import org.apache.poi.xssf.usermodel.XSSFWorkbook;
- import org.springframework.web.multipart.MultipartFile;
- import java.io.IOException;
- import java.math.BigDecimal;
- import java.util.*;
- public class ImportExcelTool {
- public static List<Map<String, String>> readExcel(MultipartFile file) throws IOException {
- Workbook workbook = null;
- String fileName = file.getOriginalFilename();
- if (Objects.requireNonNull(fileName).endsWith(".xlsx"))
- workbook = new XSSFWorkbook(file.getInputStream());
- if (fileName.endsWith(".xls"))
- workbook = new HSSFWorkbook(file.getInputStream());
- Sheet sheet = Objects.requireNonNull(workbook).getSheetAt(0);
- int physicalNumberOfRows = sheet.getPhysicalNumberOfRows();
- Row row = sheet.getRow(1);
- List<String> fieldNames = new ArrayList<>();
- for (int i = 0; i < row.getLastCellNum(); i++)
- fieldNames.add(row.getCell(i).getRichStringCellValue().getString());
- List<Map<String, String>> lsMap = new ArrayList<>();
- for (int j = 3; j < physicalNumberOfRows; j++) {
- Map<String, String> dataMap = new HashMap<>();
- Row dataRow = sheet.getRow(j);
- if (dataRow == null)
- break;
- for (int i = 0; i < dataRow.getLastCellNum(); i++) {
- String val = getCellValue(dataRow.getCell(i));
- dataMap.put(fieldNames.get(i), val);
- }
- if (dataMap.size() == 0)
- break;
- lsMap.add(dataMap);
- }
- return lsMap;
- }
- private static String getCellValue(Cell cell) {
- if (cell == null)
- return "";
- if (cell.getCellType() == CellType.NUMERIC)
- if (DateUtil.isCellDateFormatted(cell))
- return DateUtil.getJavaDate(cell.getNumericCellValue()).toString();
- else
- return new BigDecimal(cell.getNumericCellValue()).toString();
- else if (cell.getCellType() == CellType.STRING)
- return StringUtils.trimToEmpty(cell.getStringCellValue());
- else if (cell.getCellType() == CellType.FORMULA)
- return StringUtils.trimToEmpty(cell.getCellFormula());
- else if (cell.getCellType() == CellType.BLANK)
- return "";
- else if (cell.getCellType() == CellType.BOOLEAN)
- return String.valueOf(cell.getBooleanCellValue());
- else if (cell.getCellType() == CellType.ERROR)
- return "ERROR";
- else
- return cell.toString().trim();
- }
- }
用poi从excel文档导入数据的更多相关文章
- struts2中利用POI导出Excel文档并下载
1.项目组负责人让我实现这个接口,因为以前做过类似的,中间并没有遇到什么太困难的事情.其他不说,先上代码: package com.tydic.eshop.action.feedback; impor ...
- Java之Poi导出Excel文档
一.Poi简介 在后台管理系统中,我们经常要做的导出操作,通常导出为Excel文档的形式,而Poi则提供了这种需要的支持. 二.Workbook/HSSFWorkbook/XSSFWorkbook 1 ...
- POI 读取Excel文档中的数据——兼容Excel2003和Excel2007
Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能. HSSF - 提供读写Microsoft Exce ...
- POI导出Excel文档通用工具方法
import java.lang.reflect.InvocationTargetException; import java.util.List; import java.util.Map; imp ...
- 使用struts2和poi导出excel文档
poi眼下应该是比較流行的操作excel的工具了.这几天做了个struts2和poi结合使用来实现导出excel的功能.个人认为还是比較有用的.代码阅读起来也非常easy.下来就来分享下我的心得 1 ...
- excel文档中数据导入sql server注意事项
进来经常需要对一些基础数据进行更新,而业务方提供的数据源往往都是excel,所以经常需要将excel中数据导入到 数据库临时表,然后再进行处理. 在导入过程中,发现有些数据比如手机号码,如果默认导入, ...
- POI之Excel文档增删改查
需要引用apache第三方lib库poi 支持xls.xlsx格式excel读写操作 package com.hua.excel; import java.io.File;import java.io ...
- 【Java】常用POI生成Excel文档设置打印样式
package poi_test; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi ...
- 怎么对比两个excel文档的数据差异
百度经验: https://jingyan.baidu.com/article/6181c3e0877c7a152ef15304.html
随机推荐
- Kendo UI for jQuery使用教程:操作系统/jQuery支持等
[Kendo UI for jQuery最新试用版下载] Kendo UI目前最新提供Kendo UI for jQuery.Kendo UI for Angular.Kendo UI Support ...
- qt5---资源文件
创建资源文件: 视频教程:https://www.bilibili.com/video/av66748323/ 右击工程目录--->-->--> 添加资源: 右击资源文件--> ...
- 在CentOS/Windows下配置Nginx(以及踩坑)
在CentOS/Windows下配置Nginx(以及踩坑) 1. 序言 因为这类文章网上比较多,实际操作起来也大同小异,所以我并不会着重于详细配置方面,而是将我配置时踩的坑写出来. 2. CentOS ...
- kubernetes运用
1:kubernetes基本介绍 kubernetes 是一个开源的容器集群管理系统,K8s用于容器化应用程序的部署 扩展和管理,提供容器编排 资源调度 弹性伸缩 部署管理 服务发现等一系列功能. ...
- Robot Framework中ride.py打不开的解决方法
1.首先查看wxPython版本是否跟python的版本一致,一般都使用wxPython2.8-win64-unicode-2.8.12.1-py27.exe或者wxPython2.8-win32-u ...
- python 的pip安装
C:\Python27>C:\Python27\Scripts\pip.exe install gevent gevent是安装的模块名
- Linux不同机器文件挂载
由于此前发布项目应用时,需要对两台文件服务器进行文件挂载,所以才实际第一次接触到这个名词,但由于一直以来自己没有真正的去操作过,只是停留在一些理论层次,所以今天记录一下这个实现过程,以备后用. 使用设 ...
- spring cloud禁止输出日志:ConfigClusterResolver : Resolving eureka endpoints via configuration
springcloud的注册中心客户端会每隔一定时间向注册中心服务端发送心跳,用此来判断注册中心服务端是否运行正常. 这样导致不断进行日志输出,不便查看正常的业务日志输出. c.n.d.s.r.aws ...
- 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 ...
- 01Two Sum题解
Tow Sum 原题概述: Given an array of integers, return indices of the two numbers such that they add up to ...