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通用)的更多相关文章

  1. POI读取excel工具类 返回实体bean集合(xls,xlsx通用)

    本文举个简单的实例 读取上图的 excel文件到 List<User>集合 首先 导入POi 相关 jar包 在pom.xml 加入 <!-- poi --> <depe ...

  2. poi读取excel工具类

    package com.manage.utils; import ch.qos.logback.core.net.SyslogOutputStream; import com.google.gson. ...

  3. 自己封装的poi操作Excel工具类

    自己封装的poi操作Excel工具类 在上一篇文章<使用poi读写Excel>中分享了一下poi操作Excel的简单示例,这次要分享一下我封装的一个Excel操作的工具类. 该工具类主要完 ...

  4. java里poi操作Excel工具类【我改】

    参考原文: https://www.cnblogs.com/yizhang/p/7244917.html 我改: package test; import java.io.File; import j ...

  5. 使用回调方式写POI导入excel工具类

    场景是这样的:为了做一个excel导入的功能,为了尽可能的写一个通用的工具类,将与poi有关的东西都封装起来,以便以其他人员只用关心自己的业务,不用和poi打交道. 写到最后,现在还是会有poi的东西 ...

  6. 使用POI导出EXCEL工具类并解决导出数据量大的问题

    POI导出工具类 工作中常常会遇到一些图表需要导出的功能,在这里自己写了一个工具类方便以后使用(使用POI实现). 项目依赖 <dependency> <groupId>org ...

  7. 关于jquery js读取excel文件内容 xls xlsx格式 纯前端

    附带参考:http://blog.csdn.net/gongzhongnian/article/details/76438555 更详细导入导出:https://www.jianshu.com/p/7 ...

  8. POI生成Excel工具类

    import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.ByteArrayInp ...

  9. Java解析Excel工具类(兼容xls和xlsx)

    依赖jar <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml&l ...

随机推荐

  1. WPF使用RoutedCommand自己定义命令

    主要代码例如以下所看到的: /// <summary> /// 声明并定义命令. /// </summary> RoutedCommand ClearCommand = new ...

  2. Vue深度学习(1)

    Hello World 现在就让我们来写第一个vue.js的实例.如下代码: html代码: <div id="demo"> {{ message }} </di ...

  3. nginx+redis缓存微信的token数据

    上一篇文章我们讲了如何在负载均衡的项目中使用redis来缓存session数据,戳这里. 我们在项目的进展过程中,不仅需要缓存session数据,有时候还需要缓存一些别的数据,比如说,微信的acces ...

  4. Tuxedo:Tuxedo支持的分布式通信方式

    1.RPC:用于远程方法调用.Java中类似的技术有EJB.WebService 2.Conversaction:交流.Java中类似的有JDBC. 3.Message Notification:消息 ...

  5. 分布式系统的消息&服务模式简单总结

    分布式系统的消息&服务模式简单总结 在一个分布式系统中,有各种消息的处理,有各种服务模式,有同步异步,有高并发问题甚至应对高并发问题的Actor编程模型,本文尝试对这些问题做一个简单思考和总结 ...

  6. C# Lock、Monitor避免死锁

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  7. Robotframework-Appium系列:登录操作

    之前Appium的环境已经配置完成(参考Robotframework-Appium系列:安装配置),接下来就是如何使用Appium来完成我们的apk的测试工作. 一.环境准备 所需的软件列表如下 Ro ...

  8. jsDOM编程-乌龟抓小鸡游戏

    <html> <head>  <title>js乌龟抓小鸡游戏 </title>    <meta http-equiv="conten ...

  9. swig官方go Examples 源码勘误

    勘误 在官网下载页面(http://www.swig.org/download.html )下载的swigwin-3.0.12包中go示例源码有个错误(swigwin-3.0.12\Examples\ ...

  10. 使用图片地图减少HTTP请求数量

    前言 最近在看<高性能网站建设>,记录一下所学. 现在很多网站都是图片形式的导航,点击图片跳转到对应的链接.如果导航项目很多的话,图片的数量就会很多,每需要加载一张图片就会多一个HTTP请 ...