package util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List; import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ReadExcel { public static final String OFFICE_EXCEL_2003_POSTFIX = "xls";
public static final String OFFICE_EXCEL_2010_POSTFIX = "xlsx";
public static final String EMPTY = "";
public static final String POINT = ".";
public static final String NOT_EXCEL_FILE = " : Not the Excel file!";
public static final String PROCESSING = "Processing..."; public static List<ArrayList<String>> readExcel(String path,int sheelNum,int startRow,int cols){
if (path == null || EMPTY.equals(path)) {
return null;
} else {
String postfix = getPostfix(path);
if (!EMPTY.equals(postfix)) {
if (OFFICE_EXCEL_2003_POSTFIX.equals(postfix)) {
return readXls(path,sheelNum,startRow,cols);
} else if (OFFICE_EXCEL_2010_POSTFIX.equals(postfix)) {
return readXlsx(path,sheelNum,startRow,cols);
}
} else {
System.out.println(path + NOT_EXCEL_FILE);
}
}
return null;
}
/**
* Read the Excel 2003-2007
* @param path the path of the Excel
* @return
* @throws IOException
*/
public static List<ArrayList<String>> readXls(String path,int sheelNum,int startRow,int cols) {
System.out.println(PROCESSING + path);
List<ArrayList<String>> list = new ArrayList<ArrayList<String>>();
try {
InputStream is = new FileInputStream(path);
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(sheelNum);
if (hssfSheet == null) {
return list;
}
// Read the Row
for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
HSSFRow hssfRow = hssfSheet.getRow(rowNum);
if (hssfRow != null) {
ArrayList<String> rowList = new ArrayList<String>();
for (int col = 0; col < cols; col++) {
rowList.add(hssfRow.getCell(col).getStringCellValue());
}
list.add(rowList);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return list;
} /**
* Read the Excel 2010
* @param path the path of the excel file
* @return
* @throws IOException
*/
public static List<ArrayList<String>> readXlsx(String path,int sheelNum,int startRow,int cols) {
System.out.println(PROCESSING + path);
List<ArrayList<String>> list = new ArrayList<ArrayList<String>>();
InputStream is;
try {
is = new FileInputStream(path);
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is);
// Read the Sheet
XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(sheelNum);
if (xssfSheet == null) {
return list;
}
// Read the Row
for (int rowNum = startRow; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {
XSSFRow xssfRow = xssfSheet.getRow(rowNum);
if (xssfRow != null) {
ArrayList<String> rowList = new ArrayList<String>();
for (int col = 0; col < cols; col++) {
rowList.add(xssfRow.getCell(col).getStringCellValue());
}
list.add(rowList);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
return list;
}
/**
* get postfix of the path
* @param path
* @return
*/
private static String getPostfix(String path) {
if (path == null || EMPTY.equals(path.trim())) {
return EMPTY;
}
if (path.contains(POINT)) {
return path.substring(path.lastIndexOf(POINT) + 1, path.length());
}
return EMPTY;
}
}

  

最新文章

  1. (转)webHttpBinding、basicHttpBinding和wsHttpBinding区别
  2. [转载]一张图看懂开源许可协议,开源许可证GPL、BSD、MIT、Mozilla、Apache和LGPL的区别
  3. NGUI制作属于自己Button
  4. 关于由CSS2.1所提出的的BFC的理解与样例
  5. 1,SFDC 开发篇 - 类对象和SOQL查询
  6. 实现十进制无符号整数m到十六进制数的转换功能
  7. 替罪羊树—BZOJ3224: Tyvj 1728 普通平衡树
  8. java中的Integer的toBinaryString()方法
  9. php会话(session)生命周期概念介绍及设置更改和回收
  10. UICollectionViewController
  11. hdoj 2051 Bitset
  12. Sql Server Convert函数转换Datetime类型数据
  13. Android线程和handler
  14. xen虚拟机安装实践
  15. 开涛spring3(6.9) - 对JDBC的支持 之 7.1 概述
  16. JavaWeb架构发展
  17. C++内存分区:堆、栈、自由存储区、全局/静态存储区和常量存储区
  18. webshpere 节点&amp;环境分析
  19. C#-微信公众平台接口-上传临时素材
  20. Accepted Technical Research Papers and Journal First Papers 【ICSE2016】

热门文章

  1. CSS3实现三角形
  2. BoneCP学习笔记
  3. oracle 同样数据删除(仅仅留一条)
  4. 如何解决DE0-Nano的EPCS16 无法下载的问题:NO EPCS LAYOUT DATA --- LOOKING FOR SECTION [EPCS-XXXXXX]
  5. transition过渡的趣玩
  6. c++11 auto
  7. tomcat重启或关闭后,上传文件消失 .
  8. encodeURI后台乱码(解决)
  9. php,apache伪静态(1转)
  10. win7 去除桌面快捷方式小箭头