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

随机推荐

  1. windows—IOCP

    一.重叠I/O回声服务器端 服务端: #include <stdio.h> #include <stdlib.h> #include <WinSock2.h> #d ...

  2. echarts 去掉 x轴坐标

    symbol:'none', //这句就是去掉点的 smooth:true,

  3. vue做的第二个app

    用vue做应用最好的还是组件的复用上次做饿了吗的app封装了一个评分star的组件只要引入组件传入size大小和score分数就行了,这次做豆瓣直接就就用上了不用重复写代码.不过vue做单页应用全部挂 ...

  4. SQL Server2008R2循环语句

    单循环语句 declare @i nvarchar(36) declare @LOCNUM nvarchar(36),@OBJECTTYPE nvarchar(36),@LOCDESC nvarcha ...

  5. 初学python类编的一个求矩形小程序

    简单的程序不简单,里面包含类定义类,传参,初始化,方法调用,创建实例,格式输出.主要在python中随时定义变量随时用,我这道题题想好久就是我初识类,传参,不是所有参数都的加单引号.简单的东西,复杂话 ...

  6. mybatis调用存储过程,获取返回的游标

    将调用存储过程参数放入map中,由于返回的游标中包含很多参数,所以再写一个resultmap与之对应,类型为hashmap.设置返回的jdbcType=CURSOR,resultMap设置为id对应的 ...

  7. 修改create-react-app支持多入口

    使用Facebook官方脚手架create-react-app创建React应用,默认只能生成一个SPA,入口是index.html.虽然,SPA的页面切换可以使用前台路由框架方便(比如React-R ...

  8. [Leetcode 37]*数独游戏 Sudoku Solver 附解释

    [题目] 每一行.每一列.每个3*3的格子里只能出现一次1~9. [思路] 参考了思路,附加了解释. dfs遍历所有非空格子,n是已经填好的个数. 初始化条件.n=81,都填了,返回结束.对于已经填好 ...

  9. content-type的几种取值

    四种常见的 POST 提交数据方式 我们知道,HTTP 协议是以 ASCII 码传输,建立在 TCP/IP 协议之上的应用层规范.规范把 HTTP 请求分为三个部分:状态行.请求头.消息主体.类似于下 ...

  10. Unity3D Button组管理(给按钮的onclick事件“传递参数”)

    using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.UI; // ...