提供读取excel 的方法
/**
* 对外提供读取excel 的方法
* */
public static List<List<Object>> readExcel(String path) throws IOException {
File file = new File(path);
String fileName = file.getName();
String extension = fileName.lastIndexOf(".") == -1 ? "" : fileName
.substring(fileName.lastIndexOf(".") + 1);
if ("xls".equals(extension)) {
return read2003Excel(file);
} else if ("xlsx".equals(extension)) {
return read2007Excel(file);
} else {
throw new IOException("不支持的文件类型");
}
} /**
* 读取 office 2003 excel
*
* @throws IOException
* @throws FileNotFoundException
*/
private static List<List<Object>> read2003Excel(File file)
throws IOException {
List<List<Object>> list = new LinkedList<List<Object>>();
HSSFWorkbook hwb = new HSSFWorkbook(new FileInputStream(file));
HSSFSheet sheet = hwb.getSheetAt(0);
Object value = null;
HSSFRow row = null;
HSSFCell cell = null;
int counter = 0;
for (int i = sheet.getFirstRowNum(); counter < sheet
.getPhysicalNumberOfRows(); i++) {
row = sheet.getRow(i);
if (row == null) {
continue;
} else {
counter++;
}
List<Object> linked = new LinkedList<Object>();
for (int j = row.getFirstCellNum(); j <= row.getLastCellNum(); j++) {
cell = row.getCell(j);
if (cell == null) {
continue;
}
DecimalFormat df = new DecimalFormat("0");// 格式化 number String
// 字符
SimpleDateFormat sdf = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");// 格式化日期字符串
DecimalFormat nf = new DecimalFormat("0.00");// 格式化数字
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
//System.out.println(i + "行" + j + " 列 is String type");
value = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_NUMERIC:
//System.out.println(i + "行" + j+ " 列 is Number type ; DateFormt:"+ cell.getCellStyle().getDataFormatString());
if ("@".equals(cell.getCellStyle().getDataFormatString())) {
value = df.format(cell.getNumericCellValue());
} else if ("General".equals(cell.getCellStyle()
.getDataFormatString())) {
value = nf.format(cell.getNumericCellValue());
} else {
value = sdf.format(DateUtil.getJavaDate(cell
.getNumericCellValue()));
}
break;
case Cell.CELL_TYPE_BOOLEAN:
//System.out.println(i + "行" + j + " 列 is Boolean type");
value = cell.getBooleanCellValue();
break;
case Cell.CELL_TYPE_BLANK:
//System.out.println(i + "行" + j + " 列 is Blank type");
value = "";
break;
default:
//System.out.println(i + "行" + j + " 列 is default type");
value = cell.toString();
}
if (value == null || "".equals(value)) {
value = "";
//continue;
}
linked.add(value);
}
list.add(linked);
}
return list;
} /**
* 读取Office 2007 excel
* */
private static List<List<Object>> read2007Excel(File file)
throws IOException {
List<List<Object>> list = new LinkedList<List<Object>>();
// 构造 XSSFWorkbook 对象,strPath 传入文件路径
XSSFWorkbook xwb = new XSSFWorkbook(new FileInputStream(file));
// 读取第一章表格内容
XSSFSheet sheet = xwb.getSheetAt(0);
Object value = null;
XSSFRow row = null;
XSSFCell cell = null;
int counter = 0;
for (int i = sheet.getFirstRowNum(); counter < sheet
.getPhysicalNumberOfRows(); i++) {
row = sheet.getRow(i);
if (row == null) {
continue;
} else {
counter++;
}
List<Object> linked = new LinkedList<Object>();
for (int j = row.getFirstCellNum(); j <= row.getLastCellNum(); j++) {
cell = row.getCell(j);
if (cell == null) {
continue;
}
DecimalFormat df = new DecimalFormat("0");// 格式化 number String
// 字符
SimpleDateFormat sdf = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");// 格式化日期字符串
DecimalFormat nf = new DecimalFormat("0.00");// 格式化数字
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
//System.out.println(i + "行" + j + " 列 is String type");
value = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_NUMERIC:
//System.out.println(i + "行" + j+ " 列 is Number type ; DateFormt:"+ cell.getCellStyle().getDataFormatString());
if ("@".equals(cell.getCellStyle().getDataFormatString())) {
value = df.format(cell.getNumericCellValue());
} else if ("General".equals(cell.getCellStyle()
.getDataFormatString())) {
value = nf.format(cell.getNumericCellValue());
} else {
value = sdf.format(DateUtil.getJavaDate(cell
.getNumericCellValue()));
}
break;
case Cell.CELL_TYPE_BOOLEAN:
//System.out.println(i + "行" + j + " 列 is Boolean type");
value = cell.getBooleanCellValue();
break;
case Cell.CELL_TYPE_BLANK:
//System.out.println(i + "行" + j + " 列 is Blank type");
value = "";
break;
default:
//System.out.println(i + "行" + j + " 列 is default type");
value = cell.toString();
}
if (value == null || "".equals(value)) {
value = "";
//continue;
}
linked.add(value);
}
list.add(linked);
}
return list;
}
提供读取excel 的方法的更多相关文章
- 通过第三方组件NPOI读取Excel的方法
public class ExcelHelper { public class x2003 { #region Excel2003 /// <summary> /// 将Excel文件中的 ...
- R 读取excel的方法
1.加载 readxl 包,利用 reade_excel() 函数 install.packages("readxl") library(readxl) data = read_e ...
- 读取excel的方法(可用于批量导入)
FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read); //1. Reading from a binary ...
- java读取excel文件数据导入mysql数据库
这是我来公司的第二周的一个小学习任务,下面是实现过程: 1.建立maven工程(方便管理jar包) 在pom.xml导入 jxl,mysql-connector 依赖 可以在maven仓库搜索 2.建 ...
- C# 读取EXCEL文件的三种经典方法
1.方法一:采用OleDB读取EXCEL文件: 把EXCEL文件当做一个数据源来进行数据的读取操作,实例如下: public DataSet ExcelToDS(string Path) { stri ...
- ASP.NET读取EXCEL文件的三种经典方法(转)
1.方法一:采用OleDB读取EXCEL文件: 把EXCEL文件当做一个数据源来进行数据的读取操作,实例如下:public DataSet ExcelToDS(string Path) { str ...
- ASP.NET读取EXCEL文件的三种经典方法
1.方法一:采用OleDB读取EXCEL文件: 把EXCEL文件当做一个数据源来进行数据的读取操作,实例如下: public DataSet ExcelToDS(string Path) { ...
- asp.net读取excel文件多种方法
asp.net读取excel文件的三种方法示例,包括采用OleDB读取Excel文件.引用的com组件读取Excel文件.用文件流读取. 方法一:采用OleDB读取Excel文件 把Excel文件 ...
- (转)C# 读取EXCEL文件的三种经典方法
原文地址http://www.open-open.com/code/view/1420029490093 1.方法一:采用OleDB读取EXCEL文件: 把EXCEL文件当做一个数据源来进行数据的读取 ...
随机推荐
- IDEA配置JRebel实现热部署
IDEA配置JRebel实现热部署 1.下载JRebel and XRebel for Intellij插件 2. 激活 请查看这个文章http://www.cicoding.cn/other/jre ...
- “==”和equals的区别
区别: (1)比较基本数据类型时 只能采用"==",比较的是数值; (2)当比较引用数据类型时 "==" 比较的是引用对象的内存地址; 而equals分两种情况 ...
- IndexedDB详解
目录 简介 IndexedDB简介 IndexedDB的使用 IndexedDB的浏览器支持 创建IndexedDB indexdb中的CURD 使用游标cursor 简介 IndexedDB是一种在 ...
- Kubernetes官方java客户端之八:fluent style
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- LeetCode703 流中第k大的元素
前言: 我们已经介绍了二叉搜索树的相关特性,以及如何在二叉搜索树中实现一些基本操作,比如搜索.插入和删除.熟悉了这些基本概念之后,相信你已经能够成功运用它们来解决二叉搜索树问题. 二叉搜索树的有优点是 ...
- N叉树的最大深度-DFS
再看这道题之前,先来一道类似的简单题. 题目:求二叉树的最大深度 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示 ...
- Docker学习笔记之Dockerfile
Dockerfile的编写格式为<命令><形式参数>,命令不区分大小写,但一般使用大写字母.Docker会依据Dockerfile文件中编写的命令顺序依次执行命令.Docker ...
- spring cloud gateway 自定义GatewayFilterFactory
spring cloud gateway提供了很多内置的过滤器,那么因为需求的关系,需要自定义实现,并且要可配置,在一番折腾之后,总算是解决了,那么久记录下来 对于自定义的factory,我们可以选择 ...
- ctfhub技能树—web前置技能—http协议—请求方式
打开靶机环境(每次打开都要30金币,好心疼啊) 题目描述为"请求方式" HTTP的请求方式共有八种 1.OPTIONS 返回服务器针对特定资源所支持的HTTP请求方法,也可以利用向 ...
- vxfs(Veritas File System)扩充目录大小
1.新增加一个磁盘并初始化 # vxdisk list # vxdisksetup -i 3pardata0_22 2.将新增加的磁盘合并到磁盘组中 # vxdg -g testdg01 adddis ...