博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java读取excel(兼容03和07格式)
阅读量:7105 次
发布时间:2019-06-28

本文共 5337 字,大约阅读时间需要 17 分钟。

读取excel,首先需要下载POI的jar,可以去官网下,也可以在

一、简单说明

excel2003和excel2007区别比较大,最直观的感受就是扩展名不一样,哈哈

不过,使用POI的API都是面向接口编程的,实际使用起来区别其实不大(知道为什么要面向接口编程了吗?好处就在这里,O(∩_∩)O哈哈~)

代码最直观,直接看代码

 

二、范例

 

package com.hundsun.excel.test;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;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.apache.xmlbeans.impl.piccolo.io.FileFormatException;public class ReadExcelTest {    private static final String EXTENSION_XLS = "xls";    private static final String EXTENSION_XLSX = "xlsx";    /***     * 
     * 取得Workbook对象(xls和xlsx对象不同,不过都是Workbook的实现类)     *   xls:HSSFWorkbook     *   xlsx:XSSFWorkbook     * @param filePath     * @return     * @throws IOException     * 
*/ private Workbook getWorkbook(String filePath) throws IOException { Workbook workbook = null; InputStream is = new FileInputStream(filePath); if (filePath.endsWith(EXTENSION_XLS)) { workbook = new HSSFWorkbook(is); } else if (filePath.endsWith(EXTENSION_XLSX)) { workbook = new XSSFWorkbook(is); } return workbook; } /** * 文件检查 * @param filePath * @throws FileNotFoundException * @throws FileFormatException */ private void preReadCheck(String filePath) throws FileNotFoundException, FileFormatException { // 常规检查 File file = new File(filePath); if (!file.exists()) { throw new FileNotFoundException("传入的文件不存在:" + filePath); } if (!(filePath.endsWith(EXTENSION_XLS) || filePath.endsWith(EXTENSION_XLSX))) { throw new FileFormatException("传入的文件不是excel"); } } /** * 读取excel文件内容 * @param filePath * @throws FileNotFoundException * @throws FileFormatException */ public void readExcel(String filePath) throws FileNotFoundException, FileFormatException { // 检查 this.preReadCheck(filePath); // 获取workbook对象 Workbook workbook = null; try { workbook = this.getWorkbook(filePath); // 读文件 一个sheet一个sheet地读取 for (int numSheet = 0; numSheet < workbook.getNumberOfSheets(); numSheet++) { Sheet sheet = workbook.getSheetAt(numSheet); if (sheet == null) { continue; } System.out.println("=======================" + sheet.getSheetName() + "========================="); int firstRowIndex = sheet.getFirstRowNum(); int lastRowIndex = sheet.getLastRowNum(); // 读取首行 即,表头 Row firstRow = sheet.getRow(firstRowIndex); for (int i = firstRow.getFirstCellNum(); i <= firstRow.getLastCellNum(); i++) { Cell cell = firstRow.getCell(i); String cellValue = this.getCellValue(cell, true); System.out.print(" " + cellValue + "\t"); } System.out.println(""); // 读取数据行 for (int rowIndex = firstRowIndex + 1; 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 + "\t"); } 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); } if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) { return String.valueOf(cell.getBooleanCellValue()); } else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) { return String.valueOf(cell.getNumericCellValue()); } else { return String.valueOf(cell.getStringCellValue()); } }}

 

三、其他

本人在Excel2010上试的时候,excel中明明设置的都是文本类型,但是读取的时候,把数字“1”读成“1.0”

所以在getCellValue()方法中,把cell单元格的式样改了一下,确保数字“1”文本读出来还是“1”

 

转载于:https://www.cnblogs.com/yejg1212/p/3969822.html

你可能感兴趣的文章
类和对象、实例的关系理解
查看>>
Nginx 负载均衡
查看>>
学习日志---非递归二叉树游标遍历(前中后层序)
查看>>
数据库同步自动断开问题的处理
查看>>
错误页定义方法
查看>>
Guid.NewGuid() 和 new Guid()的区别
查看>>
我的友情链接
查看>>
vim技巧
查看>>
DotNetTextBox V3.0 所见即所得编辑器控件 For Asp.Net2.0(ver 3.1.2Beta)
查看>>
mysql-5.6.25-linux-glibc2.5-i686.tar.gz错误
查看>>
nginx 服务并发过10万的linux内核优化配置
查看>>
Oracle数据库体系架构概要
查看>>
extjs4视频学习笔记2
查看>>
【学神-RHEL7】1-28-mariadb数据库自动备份和expect的使用
查看>>
2017年要学习的三个CSS新特性
查看>>
C#的Unit Test如何根据exception来判断函数是否执行正确
查看>>
SQL优化
查看>>
RMAN 还原与恢复
查看>>
mysql 远程连接数据库的二种方法
查看>>
[你必须知道的.NET]第二十一回:认识全面的null
查看>>