POI读取excel工具类(xls,xlsx通用)
package com.boot.utils; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.hssf.usermodel.HSSFDataFormatter;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory; public class POIExcel { private int totalRows = ;// 总行数
private int totalCells = ;// 总列数 public Map<String, List<List<String>>> read(String fileName) {
Map<String, List<List<String>>> maps = new HashMap<String, List<List<String>>>();
if (fileName == null || !fileName.matches("^.+\\.(?i)((xls)|(xlsx))$"))
return maps;
File file = new File(fileName);
if (file == null || !file.exists())
return maps;
try {
Workbook wb = WorkbookFactory.create(new FileInputStream(file));
maps = read(wb);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (InvalidFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return maps;
} public int getTotalRows() {
return totalRows;
} public int getTotalCells() {
return totalCells;
} private Map<String, List<List<String>>> read(Workbook wb) {
Map<String, List<List<String>>> maps = new HashMap<String, List<List<String>>>();
int number = wb.getNumberOfSheets();
if (number > ) {
for (int i = ; i < number; i++) { // 循环每个工作表
List<List<String>> list = new ArrayList<List<String>>();
int delnumber = ;// 第一页去除行数
Sheet sheet = wb.getSheetAt(i);
this.totalRows = sheet.getPhysicalNumberOfRows() - delnumber; // 获取工作表中行数
if (this.totalRows >= && sheet.getRow(delnumber) != null) {
this.totalCells = sheet.getRow()
.getPhysicalNumberOfCells(); // 得到当前行的所有单元格
for (int j = ; j < totalRows; j++) {
List<String> rowLst = new ArrayList<String>();
for (int f = ; f < totalCells; f++) {
if (totalCells > ) {
String value = getCell(sheet.getRow(j).getCell(f));
rowLst.add(value);
}
}
list.add(rowLst);
}
}
maps.put(sheet.getSheetName(), list);
}
}
return maps;
} /*
* private String getRightStr(String sNum) { DecimalFormat decimalFormat =
* new DecimalFormat("##.00"); String resultStr = decimalFormat.format(new
* Double(sNum)); if (resultStr.matches("^[-+]?\\d+\\.[0]+$")) { resultStr =
* resultStr.substring(0, sNum.indexOf(".")); } return resultStr; }
*/ public String getCell(Cell cell) {
String cellValue = null;
/*
* if (Cell.CELL_TYPE_NUMERIC == cell.getCellType()) { if
* (HSSFDateUtil.isCellDateFormatted(cell)) { cellValue =
* getRightStr(cell.getDateCellValue() + ""); } else {
*
* cellValue = getRightStr(cell.getNumericCellValue() + ""); } } else if
* (Cell.CELL_TYPE_STRING == cell.getCellType()) { cellValue =
* cell.getStringCellValue(); } else if (Cell.CELL_TYPE_BOOLEAN ==
* cell.getCellType()) { cellValue = cell.getBooleanCellValue() + ""; }
* else { cellValue = cell.getStringCellValue(); }
*/
HSSFDataFormatter hSSFDataFormatter = new HSSFDataFormatter();
cellValue = hSSFDataFormatter.formatCellValue(cell); // 使用EXCEL原来格式的方式取得值
return cellValue;
} public static void main(String[] args) {
try {
Map<String, List<List<String>>> map = new POIExcel()
.read("d:\\user.xlsx");
System.out.println(map);
} catch (Exception e) {
e.printStackTrace();
}
}
}
excel通用工具类,支持多sheet
150字,150字,150字,150字,
150字,150字,150字,150字,
150字,150字,150字,150字,
150字,150字,150字,150字,
150字,150字,150字,150字,
150字,150字,150字,150字,
150字,150字,150字,150字,
150字,150字,150字,150字,
150字,150字,150字,150字,
150字,150字,150字,150字,
POI读取excel工具类(xls,xlsx通用)的更多相关文章
- POI读取excel工具类 返回实体bean集合(xls,xlsx通用)
本文举个简单的实例 读取上图的 excel文件到 List<User>集合 首先 导入POi 相关 jar包 在pom.xml 加入 <!-- poi --> <depe ...
- poi读取excel工具类
package com.manage.utils; import ch.qos.logback.core.net.SyslogOutputStream; import com.google.gson. ...
- 自己封装的poi操作Excel工具类
自己封装的poi操作Excel工具类 在上一篇文章<使用poi读写Excel>中分享了一下poi操作Excel的简单示例,这次要分享一下我封装的一个Excel操作的工具类. 该工具类主要完 ...
- java里poi操作Excel工具类【我改】
参考原文: https://www.cnblogs.com/yizhang/p/7244917.html 我改: package test; import java.io.File; import j ...
- 使用回调方式写POI导入excel工具类
场景是这样的:为了做一个excel导入的功能,为了尽可能的写一个通用的工具类,将与poi有关的东西都封装起来,以便以其他人员只用关心自己的业务,不用和poi打交道. 写到最后,现在还是会有poi的东西 ...
- 使用POI导出EXCEL工具类并解决导出数据量大的问题
POI导出工具类 工作中常常会遇到一些图表需要导出的功能,在这里自己写了一个工具类方便以后使用(使用POI实现). 项目依赖 <dependency> <groupId>org ...
- 关于jquery js读取excel文件内容 xls xlsx格式 纯前端
附带参考:http://blog.csdn.net/gongzhongnian/article/details/76438555 更详细导入导出:https://www.jianshu.com/p/7 ...
- POI生成Excel工具类
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.ByteArrayInp ...
- Java解析Excel工具类(兼容xls和xlsx)
依赖jar <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml&l ...
随机推荐
- cxf,两个声明导致 ObjectFactory 类中发生冲突
说明先,这里不管是client还是server端都是用java语言编写,如有写得不好,望原谅! 问题 http://localhost:8080/WEB-SMVC/cxf/userService?ws ...
- MyBatis_关联关系查询
一.关联查询 当查询的内容涉及到具有关联关系的多个表时,就需要使用关联查询.根据表与表间的关联关系的不同.关联查询分为四种: 一对一关联查询: 一对多关联查询: 多对一关联查询: 多对多关联查询: 二 ...
- redis基础(一)
redis是一种流行的非关系内存型数据库,拥有非常高的读写性能,下面是本人学习的总结. redis的类型 键:redis的所有的键都是string类型: 值:五种类型 string:字符串类型:一个s ...
- Linux查看系统信息(CentOS 7中测试通过)
以下命令运载CentOS7中测试通过 Linux查看服务器系统信息 CentOS版本 [root@blog ~]# cat /etc/os-release NAME="CentOS Linu ...
- org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
异常信息 org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable represen ...
- 【转】Android开发之数据库SQL
android中的应用开发很难避免不去使用数据库,这次就和大家聊聊android中的数据库操作. 一.android内的数据库的基础知识介绍 1.用了什么数据库 android中采用的数据库是SQLi ...
- JDBC结果集rs.next()注意事项
写在前面: 用JDBC从数据库中查询数据要用到结果集ResultSet,其中我们在获取结果的时候经常用到rs.next()方法来判断是否查询到了数据. 但是要特别注意,next()方法用一次,游标就往 ...
- 【十九】require和include的区别
1.require是一个函数,include是一个关键字 2.require是无返回值,include有返回值 3.include()会产生一个警告,而require()则导致一个致命的错误(出现错误 ...
- sqlserver 存储过程 删除
--删除(delete from) CREATE PROCEDURE [dbo].[DeleteMessage] @strtable varchar(),--要删除信息的表名 @strwhere va ...
- [array] leetcode-55. Jump Game - Medium
leetcode-55. Jump Game - Medium descrition Given an array of non-negative integers, you are initiall ...