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 ...
随机推荐
- Your intuition 你的直觉
If you’re thinking just like everyone else, you aren’t really thinking. Follow your intuition. Do wh ...
- gitlab多人协作开发
gitlab多人协同工作 本文为亨利向<Git权威指南>的作者蒋鑫老师的答疑邮件写成. 这里特别感谢蒋鑫老师对我询问gitlab的协同工作流程问题的详细解答. 蒋鑫老师的细致专业的解答让我 ...
- CacheHelper
public static ObjectCache Cache { get { return MemoryCache.Default; } } public static bool TryGetCac ...
- ubuntu vim8.0源码安装
安装篇 从https://github.com/vim/vim下载相应zip源码文件,利用unzip vim-master.zip 命令解压到当前用户目录,即~: 解压后进入vim的src目录,首先, ...
- Submit Text 快捷键总结
Ctrl+D : 选择单词,重复可增加选择下一个相同的单词Ctrl+F : 查找内容Ctrl+G : 跳转到指定行Ctrl+H : 替换 Ctrl+J : 合并行(已选择需要合并的多行时)Ctr ...
- 关于sql 的列转行
select * from TbaleOne unpivot(qty ])) as up go select * from TableTwo unpivot(grate for subject in ...
- Android动态方式破解apk前奏篇(Eclipse动态调试smail源码)
一.前言 今天我们开始apk破解的另外一种方式:动态代码调试破解,之前其实已经在一篇文章中说到如何破解apk了: Android中使用静态方式破解Apk 主要采用的是静态方式,步骤也很简单,首先使用 ...
- Swift基础语法学习总结(转)
Swift基础语法学习总结 1.基础 1.1) swift还是使用// 和/* */ 来注释,并且/* */允许多行注释. 1.2) swift使用print和println打印,它的传参是一个泛型 ...
- 3.mvc core 文件目录详细的解释
wwwroot 放js css image的文件夹,静态文件. favicon.ico 网站图标.上传文件的话最好在里面新建一个Upload的文件夹进行管理 Controllers 控制器, View ...
- 链表栈的C语言实现
#ifndef _CONST_H_#define _CONST_H_ #include <stdio.h>#include <stdlib.h> typedef enum { ...