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的更多相关文章
随机推荐
- idea 中add configuration
记住:点击“+”号配置tomcat 第一步当然先得建一个web项目 1.file -> new -> project -Next -> Finish -项目建好了接下来就是配置了 - ...
- 苹果手机的SB系列(3)超级烦人的账户解锁?
不知道大家有没有这种体验,Iphone 每隔一段时间后账户就被锁定了,也不告诉你原因,就是要解锁? 我怎么感觉比做的比支付宝差多了,我注册支付宝十几年,也没有动不动就告诉了有安全原因,要解锁,要重置密 ...
- JS数组映射保存数据-场景
开发遇到,写个随笔,以防我的金鱼记忆 场景:一个页面从后台拿到20条数据,把他们展现在页面上,当点击某一个item时,需要展示这个item的详情,通常不会去把页面调走,就在本页面通过 display: ...
- 关于SASS
SASS:(是一款辅助编写css的工具 安装之后可以通过同时按window键+“R”键 输入“powershell”进入CMD命令页面: 输入“sass -v”可以查看当前的sass版本 输入“cd ...
- Document.write和 InnerHTML
Document.write 定义: Document.write()这种方法是将文本字符串写入document.open()打开的文档流. document.write()方法可以用在两个方面:页面 ...
- Touch事件分发源码解析
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 以下源码基于Gingerbread 2.3.7 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1.先看ViewGroup的di ...
- .net webService保存全局变量中
遇到坑,然后找到解决方案,我就习惯做个记录. 情景:通过webservice调用一个第三方的库,在初始化第三方库的时候需要花费较长时间 目的:希望通过初始化一次,缓存下来,下次直接调用,而不必每次初始 ...
- 零基础学习JavaSE(二)——基础语法
二.Java 基础语法 2.1 Java 基础语法 java是一个面向对象的程序语言,及可把一切事物当做对象处理,而java的事物中最小的就是class (类),类中有方法,类可以创建对象,并且有一些 ...
- java富文本编辑器KindEditor
在页面写一个编辑框: <textarea name="content" class="form-control" id="content&quo ...
- .NET复习笔记-泛型
1.yield关键字用于返回迭代器具体的值,如下框代码所示 /// 返回0~9整数集合 public static IEnumerable<int> yieldSampleMethod() ...