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. 数据分析---SQL(删除数据或表)

    一.SQL中的语法 1.drop table 表名称                         eg: drop table  dbo.Sys_Test   2.truncate table 表 ...

  2. USM-V1.0

    ADSP-BF512 :Low Power Blackfin with Consumer Devices Connectivity The ADSP-BF512 is the low cost ent ...

  3. 我是如何通过学习拿到年薪80w

    本人做了5年Java,如今还是一个码农,天天写业务代码,直到2018年10月中旬遇到一位技术大牛,他给我一套技术思维导图让我又看到了希望!今天分享给各位想要提升.升职.加薪的你. 普通Java程序员与 ...

  4. 【推荐】Data Structure Visualizations

    University of San Francisco    David Galles 功能:可视化数据结构&算法实现过程 网站地址  https://www.cs.usfca.edu/~ga ...

  5. 可视化布局html5

    http://www.bootcss.com/p/layoutit/ http://layuiout.magicalcoder.com/magicaldrag-admin/drag

  6. 决策树(ID3、C4.5、CART)

    ID3决策树 ID3决策树分类的根据是样本集分类前后的信息增益. 假设我们有一个样本集,里面每个样本都有自己的分类结果. 而信息熵可以理解为:“样本集中分类结果的平均不确定性”,俗称信息的纯度. 即熵 ...

  7. DCOMP——分散式计算

    新型网络DCOMP 据国外媒体2017年11月7日报道,曾经发明互联网的秘密机构“美国国防部高级研究计划局(DARPA)”如今正致力于研发能够完胜互联网的新网络,并且启动了一项链接物联网.智能手机.智 ...

  8. datatable处理gridview筛选后的值

    DataTable dt = (DataTable)gridView1.GridControl.DataSource; DataRow[] drr = dt.Select(gridView1.RowF ...

  9. 原生js实现下拉菜单

    简单给两段代码: html和css部分: <style> .one li{ width:100px; height:30px; line-height:30px; background:u ...

  10. 软件测试:第二次作业(JUnit单元测试方法)

    一.JUnit是什么? JUnit是由 Erich Gamma 和 Kent Beck 编写的一个回归测试框架(regression testing framework).JUnit测试是程序员测试, ...