POI导入数据
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导入数据的更多相关文章
- Java利用POI导入导出Excel中的数据
首先谈一下今天发生的一件开心的事,本着一颗android的心我被分配到了PB组,身在曹营心在汉啊!好吧,今天要记录和分享的是Java利用POI导入导出Excel中的数据.下面POI包的下载地 ...
- Java 使用poi导入excel,结合xml文件进行数据验证的例子(增加了jar包)
ava 使用poi导入excel,结合xml文件进行数据验证的例子(增加了jar包) 假设现在要做一个通用的导入方法: 要求: 1.xml的只定义数据库表中的column字段,字段类型,是否非空等条件 ...
- java使用POI实现Excel批量导入数据
1.准备工作 1.1 创建模板表头与数据库表字段一一对应,示例如下 1.2将模板放入项目中,如下图所示: 2.前端页面 2.1 使用超链接提供模板下载地址 <html lang="zh ...
- java利用poi导出数据到excel
背景: 上一篇写到利用jtds连接数据库获取对应的数据,本篇写怎样用poi将数据到处到excel中,此程序为Application 正文: 第三方poi jar包:poi驱动包下载 代码片段: /** ...
- Java中使用poi导入、导出Excel
一.介绍 当前B/S模式已成为应用开发的主流,而在企业办公系统中,常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统.银行系统).或者是:我们已经习惯用Excel打印.这样在我们实际 ...
- Java使用POI实现数据导出excel报表
Java使用POI实现数据导出excel报表 在上篇文章中,我们简单介绍了java读取word,excel和pdf文档内容 ,但在实际开发中,我们用到最多的是把数据库中数据导出excel报表形式.不仅 ...
- Excel文件按照指定模板导入数据(用jxl.jar包)
本文中的方法只适合Excel2003,要读取Excel2007最好使用poi.jar,据说poi.jar还在更新,jxl.jar已经不更新了,处理Excel文件的读写问题最好还是学习poi.j ...
- 纳税服务系统【用户模块之使用POI导入excel、导出excel】
前言 再次回到我们的用户模块上,我们发现还有两个功能没有完成: 对于将网页中的数据导入或导出到excel文件中,我们是完全没有学习过的.但是呢,在Java中操作excel是相对常用的,因此也有组件供我 ...
- poi 导入导出的api说明(大全)
原文链接:http://www.cnblogs.com/qingruihappy/p/8443101.html poi 导入导出的api说明(大全) 一. POI简介 ApachePOI是Apache ...
随机推荐
- [Matlab] Attempt to execute SCRIPT *** as a function
Attempt to execute SCRIPT *** as a function 问题: 在运行MATLAB程序的时候,出现如题的报错. 原因: 在系统中,现有的.m文件有的与***函数重名,所 ...
- Android + eclipse +ADT安装完全教程
最近几天没事做,网上看来看去突然就想弄个android来学学... 首先,是要下载android SDK,在http://developer.android.com/sdk/index.html这个 ...
- js推断元素是否隐藏
if( document.getElementById("div").css("display")==='none') if( document.getEl ...
- windows下体验Redis
Redis 是一个高性能的key-value数据库, 使用内存作为主存储,数据访问速度非常快,当然它也提供了两种机制支持数据持久化存储.比较遗憾的是,Redis项目不直接支持Windows,Windo ...
- JavaScript总结(二) 系统分析
学习Javascript的时候.遇到了一个问题. 看了牛腩的视频,感觉讲的太简单和基础,像认识一位新朋友.但仅有一面之缘就结束了. 再看姜浩的视频.又看得迷迷糊糊,让我找到了当初看两位台湾专家讲VB. ...
- Qt使用QGraphicsView实现滑动窗体效果
QGraphicsView用来显示一个滚动视图区的QGraphicsScene内容.QGraphicsScene提供了QGraphicsItem的容器功能.通常与QGraphicsView一起使用来描 ...
- Mysql User表为空
Mysql5.6刚安装完成,未设置过密码,root账号登录提示:root@localhost mysql]# mysqlERROR 1045 (28000): Access denied for us ...
- arc engine - IName
IName对象是一个代表性对象.通过使用IName对象,可以访问它所代表的对象的一些基本属性,而不用将整个对象调入内存.我们用IWorkspace 获得一个Workspace,那可是会调入内存的,而I ...
- java 面对对象(抽象 继承 接口 多态)
什么是继承? 多个类中存在相同属性和行为时,将这些内容抽取到单独一个类中,那么多个类无需再定义这些属性和行为,只要继承那个类即可. 多个类可以称为子类,单独这个类称为父类.超类或者基类. 子类可以直接 ...
- Json部分知识(前台显示格式、Json-lib日期处理)
1,Json格式用于datagrid数据显示 easyui前台显示数据可以使用JSONObject,也可以使用JSONArray.但是如果需要在datagrid表格中进行数据显示,只能使用JSONOb ...