ExcelUploadUtil
package com.rscode.credits.util; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExcelUtil {
private static final String EXCEL_XLS = "xls";
private static final String EXCEL_XLSX = "xlsx"; /**
* 判断Excel的版本,获取Workbook
* @param in
* @param filename
* @return
* @throws IOException
*/
private static Workbook getWorkbok(File file) throws IOException{
InputStream in = new FileInputStream(file);
Workbook wb = null;
if(file.getName().endsWith(EXCEL_XLS)){ //Excel 2003
wb = new HSSFWorkbook(in);
}else if(file.getName().endsWith(EXCEL_XLSX)){ // Excel 2007/2010
wb = new XSSFWorkbook(in);
}
return wb;
} /**
* 判断文件是否是excel
* @throws Exception
*/
private static void checkExcelVaild(File file) throws Exception{
if(!file.exists()){
throw new Exception("文件不存在");
}
if(!(file.isFile() && (file.getName().endsWith(EXCEL_XLS) || file.getName().endsWith(EXCEL_XLSX)))){
throw new Exception("文件不是Excel");
}
} /**
* 读取Excel测试,兼容 Excel 2003/2007/2010
* @param excelPath 文件路径
* @return
*/
public static List<String[]> getAll(String excelPath){
List<String[]> list = new ArrayList<>();
FileInputStream in = null;//文件流
File excelFile;
try {
// 同时支持Excel 2003、2007
excelFile = new File(excelPath); // 创建文件对象
in = new FileInputStream(excelFile); // 文件流
checkExcelVaild(excelFile);
Workbook workbook = getWorkbok(excelFile);
//Workbook workbook = WorkbookFactory.create(is); // 这种方式 Excel2003/2007/2010都是可以处理的 int sheetCount = workbook.getNumberOfSheets(); // Sheet的数量 for (int i = 0; i < sheetCount; i++) {
/**
* 设置当前excel中sheet的下标:0开始
*/
Sheet sheet = workbook.getSheetAt(i); // 遍历第三个Sheet //获取总行数
// System.out.println(sheet.getLastRowNum()); // 为跳过第一行目录设置count
int count = 0;
for (Row row : sheet) {
try {
// 跳过第一行的目录
if(count < 1 ) {
count++;
continue;
}
//如果当前行没有数据,跳出循环
if(row.getCell(0).toString().equals("")){
continue;
}
//获取总列数(空格的不计算)
//int columnTotalNum = row.getPhysicalNumberOfCells();
// System.out.println("总列数:" + columnTotalNum); // System.out.println("最大列数:" + row.getLastCellNum()); //for循环的,不扫描空格的列
// for (Cell cell : row) {
// System.out.println(cell);
// }
int end = row.getLastCellNum();
String[] rowValue = new String[end];
for (int j = 0; j < end; j++) {
Cell cell = row.getCell(j);
if(cell == null) {
rowValue[j]="";
}
try {
rowValue[j] = getValue(cell).toString();
} catch (Exception e) {
e.printStackTrace();
rowValue[j]= " ";
}
}
list.add(rowValue);
} catch (Exception e) {
e.printStackTrace();
}
}
} } catch (Exception e) {
e.printStackTrace();
}finally {
try {
in.close();
} catch (IOException e) {
System.out.println("关流失败");
e.printStackTrace();
}
}
return list;
} @SuppressWarnings("deprecation")
private static Object getValue(Cell cell) {
Object obj = null;
switch (cell.getCellTypeEnum()) {
case BOOLEAN:
obj = cell.getBooleanCellValue();
break;
case ERROR:
obj = cell.getErrorCellValue();
break;
case NUMERIC:
obj = cell.getNumericCellValue();
break;
case STRING:
obj = cell.getStringCellValue();
break;
default:
break;
}
return obj;
}
// public static void main(String[] args) throws Exception {
//
// List<String[]>list = ExcelUtil.getAll("C:\\Users\\13320\\Downloads\\捐献书籍活动名单.xls");
// for (String[] strings : list) {
// for (int i = 0; i < strings.length; i++) {
// System.out.println(strings[i]);
// }
// }
// }
}
ExcelUploadUtil的更多相关文章
随机推荐
- MapServer Tutorial——MapServer7.2.1教程学习——第一节用例实践:Example1.6 Defining Projections and Extents
MapServer Tutorial——MapServer7.2.1教程学习——第一节用例实践:Example1.6 Defining Projections and Extents 一.前言 当在m ...
- SQL Server2008R2循环语句
单循环语句 declare @i nvarchar(36) declare @LOCNUM nvarchar(36),@OBJECTTYPE nvarchar(36),@LOCDESC nvarcha ...
- python修炼第二天
第二天的课程还是塞得满满的,一天下来充实也疲惫.那天听出金星师傅嗓子有些沙哑,想必是讲课说话太多导致.啥也不说了.唯有努力练功方可回报! 1 序: 今天主要是详细学习了绝大部分的数据类型,字符编码,边 ...
- 【转载】curl 模拟 GET\POST 请求,curl查看响应头 以及 curl post 上传文件
补充说明:curl查看响应头 curl -I "http://www.baidu.com"HTTP/1.1 200 OK #HTTP协议 HTTP 返回码Server: Tengi ...
- 【Java集合系列五】HashMap解析
2017-07-31 19:36:00 一.简介 1.HashMap作用及使用场景 HashMap利用数组+单向链表的方式,实现了key-value型数据的存储功能.HashMap的size永远是2^ ...
- 使用junit和eclemma进行简单的代码测试
1.Junit和Hamcrest的安装 可以在https://mvnrepository.com/上面下载所需要的Junit和Hamcrest的jar包,然后在项目中新建一个lib文件夹,将下载好的j ...
- python dns
- 目标检测(二) SPPNet
引言 先简单回顾一下R-CNN的问题,每张图片,通过 Selective Search 选择2000个建议框,通过变形,利用CNN提取特征,这是非常耗时的,而且,形变必然导致信息失真,最终影响模型的性 ...
- elasticsearch(2) 数据操作——查询
一 文档 ES面向文档,并且使用JSON作为文档序列化格式,对于ES来说,文档特指根对象序列化成的JSON数据,以唯一ID标识并存储于ES中. 文档元数据 三个必须的元数据节点 1._index ...
- mysql无法安装,报 The older version of MySQL Installer - Community cannot be removed. Contact your technical support group
解决办法: C:\Users\Administrator>msiexec /i C:\Users\Administrator\Downloads\mysql-installer-web-comm ...