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;
}
}

  

POI导入数据的更多相关文章

  1. Java利用POI导入导出Excel中的数据

         首先谈一下今天发生的一件开心的事,本着一颗android的心我被分配到了PB组,身在曹营心在汉啊!好吧,今天要记录和分享的是Java利用POI导入导出Excel中的数据.下面POI包的下载地 ...

  2. Java 使用poi导入excel,结合xml文件进行数据验证的例子(增加了jar包)

    ava 使用poi导入excel,结合xml文件进行数据验证的例子(增加了jar包) 假设现在要做一个通用的导入方法: 要求: 1.xml的只定义数据库表中的column字段,字段类型,是否非空等条件 ...

  3. java使用POI实现Excel批量导入数据

    1.准备工作 1.1 创建模板表头与数据库表字段一一对应,示例如下 1.2将模板放入项目中,如下图所示: 2.前端页面 2.1 使用超链接提供模板下载地址 <html lang="zh ...

  4. java利用poi导出数据到excel

    背景: 上一篇写到利用jtds连接数据库获取对应的数据,本篇写怎样用poi将数据到处到excel中,此程序为Application 正文: 第三方poi jar包:poi驱动包下载 代码片段: /** ...

  5. Java中使用poi导入、导出Excel

    一.介绍 当前B/S模式已成为应用开发的主流,而在企业办公系统中,常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统.银行系统).或者是:我们已经习惯用Excel打印.这样在我们实际 ...

  6. Java使用POI实现数据导出excel报表

    Java使用POI实现数据导出excel报表 在上篇文章中,我们简单介绍了java读取word,excel和pdf文档内容 ,但在实际开发中,我们用到最多的是把数据库中数据导出excel报表形式.不仅 ...

  7. Excel文件按照指定模板导入数据(用jxl.jar包)

        本文中的方法只适合Excel2003,要读取Excel2007最好使用poi.jar,据说poi.jar还在更新,jxl.jar已经不更新了,处理Excel文件的读写问题最好还是学习poi.j ...

  8. 纳税服务系统【用户模块之使用POI导入excel、导出excel】

    前言 再次回到我们的用户模块上,我们发现还有两个功能没有完成: 对于将网页中的数据导入或导出到excel文件中,我们是完全没有学习过的.但是呢,在Java中操作excel是相对常用的,因此也有组件供我 ...

  9. poi 导入导出的api说明(大全)

    原文链接:http://www.cnblogs.com/qingruihappy/p/8443101.html poi 导入导出的api说明(大全) 一. POI简介 ApachePOI是Apache ...

随机推荐

  1. http接口测试浏览器插件

    http接口测试浏览器插件: Chrome: https://chrome.google.com/webstore/detail/chrome-poster/cdjfedloinmbppobahmon ...

  2. js推断元素是否隐藏

    if( document.getElementById("div").css("display")==='none')   if( document.getEl ...

  3. bit、byte、位、字节、字符串等概念

    原始文章:http://djt.qq.com/article/view/658 1.古代送信:马车,烽火,信鸽 2.1837年,世界第一条电报诞生, 美国科学家莫尔斯尝试用一些“点”和“划”来表示不同 ...

  4. HTML与CSS入门——第十四章  使用边距、填充、对齐和浮动

    知识点: 1.在元素周围添加边距的方法 2.在元素中添加填充的方法 3.对齐的方法 4.float属性的使用 这里提到了CSS禅意花园,这块有时间可以玩玩~ margin和padding:用于添加元素 ...

  5. js过滤

    datagrid:                loadFilter: function (data) {                    return loadFilter(data);   ...

  6. Android Studio tips and tricks 翻译学习

    Android Studio tips and tricks 翻译 这里是原文的链接. 正文: 如果你对Android Studio和IntelliJ不熟悉,本页提供了一些建议,让你可以从最常见的任务 ...

  7. idea导入项目出错

    在idea导如项目后,总是会报错,每个类都会报错.解决的办法是: 1. 2.添加本地jdk 3.添加项目中的lib包

  8. MySQL在Linux系统下配置文件详解

    在日常的的开发过程中接触到了SQLServer和MySQL数据库的操作性问题,可能是以前接触的都是SQL Server,才开始接触MySQL,总感觉使用MySQL没有使用SQLserver那么顺手,一 ...

  9. MySQL指令记录(Wampserve环境)

    1.MySQL在Wampserve中的默认用户名为'root',默认密码为空: 2.显示所有数据库 show databases; 3.切换数据库 use DATABASE_NAME; 4.列出所有表 ...

  10. C语言中对于结构的定义一般放在.h中还是.c中

    单文件使用的结构体放.c文件中 本模块内部使用结构体,放模块内部头文件中 供外部使用的结构体,放模块对外头文件中.