poi读取excel,包含所有excel版本,兼容excel2003和2007以上版本
更新时间:2024-01-15 06:34:01 阅读量: 教育文库 文档下载
- poi 读取excel推荐度:
- 相关推荐
package com.excel;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream;
import java.text.SimpleDateFormat;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.DateUtil; 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.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.xmlbeans.impl.piccolo.io.FileFormatException; /**
* 读取EXCEL内容 * @author lxr * */
public class TestReadExcelAll { private static final String EXTENSION_XLS = \ private static final String EXTENSION_XLSX = \ /** * 判断EXCEL版本 * @param in * @param filename * @return * @throws IOException */ public static Workbook getWorkbook(InputStream in,String filename)throws IOException {
}
Workbook wb = null;
if(filename.endsWith(EXTENSION_XLS)){ wb = new HSSFWorkbook(in);//Excel 2003 } else if(filename.endsWith(EXTENSION_XLSX)){ wb = new XSSFWorkbook(in);//Excel 2007 }
return wb;
/**
* 文件校验是否是excel *
* @param filePath
* @throws FileNotFoundException * @throws FileFormatException */
private void preReadCheck(String filePath) throws FileNotFoundException, FileFormatException { // 常规检查 File file = new File(filePath); if (!file.exists()) { }
/**
* 读取EXCEL * @param filePath
* @throws FileNotFoundException * @throws FileFormatException */
public void readExcel(String filePath) throws FileNotFoundException,FileFormatException { // 检查 preReadCheck(filePath); // 获取workbook对象 Workbook workbook = null;
}
throw new FileNotFoundException(\传入的文件不存在:\
if (!(filePath.endsWith(EXTENSION_XLS) || filePath .endsWith(EXTENSION_XLSX))) { }
throw new FileFormatException(\传入的文件不是excel\
InputStream is = new FileInputStream(filePath); try { workbook = getWorkbook(is, filePath); // workbook = WorkbookFactory.create(is); // 读文件 一个sheet一个sheet地读取 for (int numSheet = 0; numSheet < workbook.getNumberOfSheets(); numSheet++) { Sheet sheet = workbook.getSheetAt(numSheet); if (sheet == null) { continue; } int sheetCount = workbook.getNumberOfSheets(); //Sheet的数量 int rowCount = sheet.getPhysicalNumberOfRows(); //获取总行数 int firstRowIndex = sheet.getFirstRowNum(); int lastRowIndex = sheet.getLastRowNum(); if (firstRowIndex != lastRowIndex && lastRowIndex != 0) { System.out.println(\+ sheet.getSheetName()+ \ // 读取数据行 for (int rowIndex = 0; rowIndex <= lastRowIndex; rowIndex++) { Row currentRow = sheet.getRow(rowIndex);// 当前行 int firstColumnIndex = currentRow.getFirstCellNum(); // 首列 int lastColumnIndex = currentRow.getLastCellNum();// 最后一列 for (int columnIndex = firstColumnIndex; columnIndex <= lastColumnIndex; columnIndex++) { Cell currentCell = currentRow.getCell(columnIndex);// 当前单元格 String currentCellValue = this.getCellValue(currentCell, true);// 当前单元格的值 System.out.print(currentCellValue + \ } System.out.println(\ } System.out.println(\ System.out.println(\
// // // // // // //
}
}
System.out.println(\
}
} catch (Exception e) { e.printStackTrace(); } finally { if (workbook != null) { try { workbook.close(); } catch (IOException e) { e.printStackTrace(); } } }
/**
* 取单元格的值 *
* @param cell
* 单元格对象 * @param treatAsStr
* 为true时,当做文本来取值 (取到的是文本,不会把“1”取成“1.0”) * @return */
private String getCellValue(Cell cell, boolean treatAsStr) { if (cell == null) { return \ }
if (treatAsStr) {
// 虽然excel中设置的都是文本,但是数字文本还被读错,如“1”取成“1.0” // 加上下面这句,临时把它当做文本来读取 cell.setCellType(Cell.CELL_TYPE_STRING); }
SimpleDateFormat fmt = new SimpleDateFormat(\String cellValue = null;
int cellType = cell.getCellType(); switch (cellType) {
case Cell.CELL_TYPE_STRING: // 文本 cellValue = cell.getStringCellValue(); break;
} /**
case Cell.CELL_TYPE_NUMERIC: // 数字、日期 if (DateUtil.isCellDateFormatted(cell)) { cellValue = fmt.format(cell.getDateCellValue()); // 日期型 } else { cell.setCellType(Cell.CELL_TYPE_STRING);
cellValue = String.valueOf(cell.getNumericCellValue()); // 数字 }
break;
case Cell.CELL_TYPE_BOOLEAN: // 布尔型 cellValue = String.valueOf(cell.getBooleanCellValue()); break;
case Cell.CELL_TYPE_BLANK: // 空白 cellValue = cell.getStringCellValue(); break; case Cell.CELL_TYPE_ERROR: // 错误 cellValue = \错误\ break; case Cell.CELL_TYPE_FORMULA: // 公式 cellValue = \错误\ break; default: cellValue = \错误\}
return cellValue;
* 测试
* @param args
* @throws FileNotFoundException * @throws FileFormatException */
public static void main(String[] args) throws FileNotFoundException, FileFormatException { TestReadExcelAll e3 = new TestReadExcelAll(); e3.readExcel(\ e3.readExcel(\}
}
正在阅读:
poi读取excel,包含所有excel版本,兼容excel2003和2007以上版本01-15
(完整版)《灯笼》教案04-18
会计信息系统上机实验资料(总账部分)09-19
公路工程检查细则12-16
对药学专业的认识05-22
五岳联考2020届河南广东等省高三普通高等学校招生全国统一考试4月联考地理试卷及答案04-28
大盘今后将何去何从-牛市已近07-28
函数的概念和性质高考真题04-05
林清玄 - 窗户和镜子01-12
换届选举个人工作总结范文03-27
- exercise2
- 铅锌矿详查地质设计 - 图文
- 厨余垃圾、餐厨垃圾堆肥系统设计方案
- 陈明珠开题报告
- 化工原理精选例题
- 政府形象宣传册营销案例
- 小学一至三年级语文阅读专项练习题
- 2014.民诉 期末考试 复习题
- 巅峰智业 - 做好顶层设计对建设城市的重要意义
- (三起)冀教版三年级英语上册Unit4 Lesson24练习题及答案
- 2017年实心轮胎现状及发展趋势分析(目录)
- 基于GIS的农用地定级技术研究定稿
- 2017-2022年中国医疗保健市场调查与市场前景预测报告(目录) - 图文
- 作业
- OFDM技术仿真(MATLAB代码) - 图文
- Android工程师笔试题及答案
- 生命密码联合密码
- 空间地上权若干法律问题探究
- 江苏学业水平测试《机械基础》模拟试题
- 选课走班实施方案
- 版本
- excel
- 兼容
- excel2003
- 读取
- 包含
- 以上
- 所有
- 2007
- poi
- 柯美282(283)扫描设置的步骤
- 安徽省地方标准DB34-T-371.1-2014水利水电工程施工质量检验与评定规范
- 日照市特色小镇投资建设研究报告(目录) - 图文
- 东南大学毕业论文参照模版--贺磊14404106(1)
- 电工实习公开课详案-导线连接
- 学案20 氮气及其氧化物
- 2015年继续医学教育华医网公共课程实用现场急救技术
- 重大应用示范工程项目资金申请报告编制要点
- 新课程高中数学教学中创新思维能力的培养-精品文档
- 肉毒素注射后出现不良反应
- 光缆应急抢修工作内容
- QOS学习笔记
- AD536A
- 行云流水 天衣无缝 浅谈数学课堂的衔接过渡
- 2013年河北省保定市中考数学二模试卷
- 关于个人现实表现
- 中考复习九 重点动词短语讲练
- 暴风雨后的选择
- 小学英语观课评课稿(共4篇)
- 城镇居民社会养老保险