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. Winform开发框架中工作流模块之审批会签操作(2)

    前面随笔介绍了请假申请单和报销申请单两个不同的业务表单的流程处理,一个是单表信息,一个包含明细的主从表信息,后者包含了条件流程的处理,在流程审批中,一般还有一种流程处理就是会签的操作,会签处理是几个审 ...

  2. 自学Python1.2-环境的搭建:Pycharm及python安装详细教程

    Python几乎可以在任何平台下运行,如我们所熟悉的:Windows/Unix/Linux/Macintosh 一.windows下安装Python 1. 从python官方网站:http://www ...

  3. Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.5.6.RELEASE:repackage failed: Unable to find main class

    异常 [INFO] --- spring-boot-maven-plugin:1.5.6.RELEASE:repackage (default) @ spring-boot-starter-log - ...

  4. springboot命令启动

    gradle打jar包命令 jar { doFirst { def jarFiles = ''; configurations.compile.collect { jarFiles += it.nam ...

  5. Android查缺补漏(View篇)--自定义 View 中 wrap_content 无效的解决方案

    自定义 View 中 wrap_content 无效的解决方案 做过自定义 View 的童鞋都会发现,直接继承 View 的自定义控件需要重写 onMeasure() 方法,并设置 wrap_cont ...

  6. case

    case $变量 in "值1") 执行语句; ;; "值2") 执行语句; ;; ... *) 默认执行语句 ;; esac #!/bin/bash read ...

  7. Java零碎总结

    获取当前类运行的根目录(即classpath,如bin.classes.AppName等)的方式有: 1.Thread.currentThread().getContextClassLoader(). ...

  8. SpringMVC 返回json的两种方式

    前后台数据交互使用json是一种很重要的方式.本文主要探讨SpringMVC框架使用json传输的技术. 请注意,本文所提到的项目使用Spring 版本是4.1.7,其他版本在具体使用上可能有不一样的 ...

  9. MySQL 如何存储长度较大的varchar与blob

    本文同时发表在https://github.com/zhangyachen/zhangyachen.github.io/issues/96 最近,在工作中遇到了MySQL中如何存储长度较长的字段类型问 ...

  10. python 动态加载类对象

    第一步 加载模块 module  =__import__("modulename",fromlist=['']) 第二部 加载类对象 cls = getattr(module, & ...