excel读取
一、jar包
二、工具类
package excel; import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; public class XLSOprUtil {
private static final Logger logger = LoggerFactory.getLogger(XLSOprUtil.class); public static void main(String[] args) throws Exception {
final String filename = "userInfo.xlsx";
new XLSOprUtil(filename).readExcel(0);
} private InputStream inp;
private String filePath; public XLSOprUtil(String filePath) throws FileNotFoundException {
this.inp = new FileInputStream(filePath);
this.filePath = filePath;
} /**
* 读取xls文件内容
*
* @param isCloseStream
* true:读取内容完成后,关闭流;false:不管比
* @return
*/
public List<RowContent> readExcel(int sheetIndex, boolean isCloseStream) {
try {
long s1 = System.nanoTime();
Workbook wb = getXLSWorkBook(this.filePath, this.inp); if (wb == null)
return null;
Sheet sheet = getXLSSheet(wb, sheetIndex); List<RowContent> rowsContent = readSheetContent(sheet); long s2 = System.nanoTime();
logger.debug("readSheetContent coast times:" + (s2 - s1) / 1000000); return rowsContent;
} catch(Exception e)
{
logger.error("readExcel 异常", e);
return null;
} finally
{
if(isCloseStream)
{
destory();
}
}
} /**
* 重载,默认读取完成后,关闭流
*
* @return
*/
public List<RowContent> readExcel(int sheetIndex) {
return readExcel(sheetIndex, true);
} /**
*
* @param sheet
* @return
*/
public List<RowContent> readSheetContent(Sheet sheet) {
if (sheet == null) {
return null;
} List<RowContent> rowsContent = new ArrayList<XLSOprUtil.RowContent>(); int itemNum = sheet.getLastRowNum();
logger.info("Sheet名称:{}, 行数:{}", sheet.getSheetName(), itemNum); for (Row row : sheet) {
int cellNum = row.getLastCellNum();
String[] cells = new String[cellNum];
int index = 0;
boolean isAllEmpty = true; // 判断某一行内容是否为空
for (Cell cell : row) {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
// System.out.print("数字型单元格内容:" + cell.getNumericCellValue()
// + " ");
cells[index++] = String.valueOf(cell.getNumericCellValue());
isAllEmpty = false;
break;
case Cell.CELL_TYPE_STRING:
// System.out.print("字符串型单元格内容:" + cell.getStringCellValue()
// + " ");
cells[index++] = String.valueOf(cell.getStringCellValue());
isAllEmpty = false;
break;
default:
index++;
break;
}
} if (!isAllEmpty) {
RowContent rowContent = new RowContent();
rowContent.setRowCells(cells);
rowsContent.add(rowContent);
}
} return rowsContent;
} /**
*
*/
public void destory() {
if (inp != null) {
try {
inp.close();
logger.debug("--- 关闭读取的文件流 成功 ----");
} catch (Exception e) {
logger.error(e.toString());
}
}
} /**
* 适配不同的excel文档版本
*
* @param filename
* @param inp
* @return
* @throws IOException
*/
public Workbook getXLSWorkBook(String filename, InputStream inp)
throws IOException {
if (filename.matches("^(.*.xls)$")) {
return new HSSFWorkbook(inp);
} else {
return new XSSFWorkbook(inp);
}
} /**
* 获取指定的工作表
*
* @param wb
* @param number
* @return
*/
public Sheet getXLSSheet(Workbook wb, int number) {
if (wb != null) {
return wb.getSheetAt(number);
}
return null;
} @SuppressWarnings("serial")
public class RowContent implements Serializable{
private String[] rowCells; /**
* @return the rowCells
*/
public String[] getRowCells() {
return rowCells;
} /**
* @param rowCells
* the rowCells to set
*/
public void setRowCells(String[] rowCells) {
this.rowCells = rowCells;
}
}
}
三、使用
excel读取的更多相关文章
- java的poi技术下载Excel模板上传Excel读取Excel中内容(SSM框架)
使用到的jar包 JSP: client.jsp <%@ page language="java" contentType="text/html; charset= ...
- excel读取 工具类
package cn.yongche.utils; import java.io.File; import java.io.FileInputStream; import java.io.IOExce ...
- NPOI 2.0 Excel读取显示
NPOI 2.0 Excel读取显示 最近接到需求,需要把excel表格里的数据原样展示到web页面,主要是满足随意跨行跨列. 之前用过一点NPOI,不过接触的不太多,趁这次机会再熟悉一下.由于操 ...
- Pandas之Dateframe 实现Excel读取与写入
目的:有时需对数据进行到出到Excel,直观的给别人参阅,或从Excel中读取数据进行操作和分析依赖库 pandas 可简单的读出和写入 1,根据Excel读取( 需安装xlrd库) import n ...
- EPPlus实战篇——Excel读取
.net core 项目 可以从excel读取任何类型(T)的数据,只要T中的field的[Display(Name = "1233")]中的name==excel column ...
- php excel 读取日期问题
在 php excel 读取 xls 格式的文件时,xls 上面显示的是正常的日期格式 但是读取出来的话,就会是一个万位整形数据,这显然不是我们想要的日期 读取出来的结果: 41807 $t = 41 ...
- 利用poi包装一个简单的Excel读取器.一(适配一个Reader并提供readLine方法)
通常,读文本我们会使用BufferedReader,它装饰或者说管理了InputStreamReader,同时提供readLine()简化了我们对文本行的读取.就像从流水线上获取产品一样,每当取完一件 ...
- C#连接Excel读取与写入数据库SQL ( 上 )
第一次写C#与sql的东西,主要任务是从Excel读取数据,再存到SQL server中. 先上读取Excel文件的code如下. public bool GetFiles(string equipN ...
- Excel 读取写入数据库
// Excel 读取写入数据库 // 3.8版本的poi 4.0 可以不用写 parseCell 这个方法,可以直接赋值 STRING 类型 import org.apache.poi.hss ...
随机推荐
- linux下使用远程图形界面
1. 用xrdp的方式(客户端就是windows下的远程桌面程序) http://jingyan.baidu.com/article/d3b74d64bdab5d1f76e60951.html 2. ...
- C语言初学者代码中的常见错误与瑕疵(23)
见:C语言初学者代码中的常见错误与瑕疵(23)
- java static静态方法的并发性
在做一个web项目的时候需要做一个通用类去处理一些问题,想到这个类很多地方都有用到,又不想每次都new一个,因此就定义了里面的方法是静态方法,然后又因为多个静态方法都用到了同一个对象,结果定义了一个类 ...
- Centos7搭建需要mysql的网站
1.在centos7上安装好http.php.php-mysql服务 php-mysql是用来链接的工具 2.在centos5上yum安装mysql 注意在搭建本地yum源时把校验关闭,不然安装不上 ...
- github在liunx上的搭建
清屏:ctrl+l 1 在linux下安装git yum -y install git 查看版本 git --version 4 设置git的用户名和邮箱地址 git config --global ...
- ROCKETMQ源码分析笔记1:tools
rocketmq源码解析笔记 大家好,先安利一下自己,本人男,35岁,已婚.目前就职于小资生活(北京),职位是开发总监. 姓名DaneBrown 好了.我保证本文绝不会太监!转载时请附上以上安利信息. ...
- MySQL显示中文
http://www.cnblogs.com/livingintruth/p/3433259.html 这两天在学习webpy,把webpy的一个blog例子扒下来学习一下,默认创建的table当存入 ...
- 关于Markdown
之前有接触过一点markdown,知道能生成好看的排版,只是太懒都不去看不去记那些标签 现在才发现它简单好用得有点伟大 一个在线的Markdown编辑器:https://stackedit.io/ed ...
- 安装composer
按照composer官网的指导,运行下列命令:curl -sS https://getcomposer.org/installer | php长时间无反应.手动安装1.下载installer# wge ...
- 谷歌input框黄色背景问题
input:-webkit-autofill,input:-webkit-autofill:hover,input:-webkit-autofill:focus,input:-webkit-autof ...