读取Excel工具类

需要的jar包

poi-3.13.jar

poi-ooxml-3.13.jar

poi-ooxml-schemas-3.13.jar

poi-scratchpad-3.9.jar

工具类1
java 复制代码
package com.test.poi;

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.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * 两种处理方式 xlsx/xls
 * @author zhaowanli
 *
 */
public class ExcelUtilReader {
	
	public static String[][] getExcelData(File file){
		if(file == null) return null;
		String name = file.getName();
		name = name.substring(name.lastIndexOf(".")+1);
		InputStream is = null;
		try {
			is = new FileInputStream(file);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		if("XLSX".equalsIgnoreCase(name)){
			return getExcelDateAF2007(is);
		}else if("XLS".equalsIgnoreCase(name)){
			return getExcelDateBF2007(is);
		}else{
			try {
				throw new Exception("文件格式不正确");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	//xls 2007 之前版本
	public static String[][] getExcelDateBF2007(InputStream inputStream) {
		String[][] data = null;
		HSSFWorkbook hssfwb = null;
		try {
			hssfwb = new HSSFWorkbook(inputStream);
			
			HSSFSheet hssfSheet = hssfwb.getSheetAt(0);
			data = new String[hssfSheet.getLastRowNum()+1][];
			int headerColNum = 0;
			for(int i = 0; i<=hssfSheet.getLastRowNum();i++){
				HSSFRow hssfRow = hssfSheet.getRow(i);
				if(i == 0 ){
					headerColNum = hssfRow.getLastCellNum();
				}
				data[i] = new String[headerColNum];
				for(int j = 0;j<hssfRow.getLastCellNum();j++){
					HSSFCell hssfCell = hssfRow.getCell(j);
					Object value = getHSSFCellValue(hssfCell);
					data[i][j] = value != null?String.valueOf(value): null;
				}
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				if(hssfwb != null){
					hssfwb.close();
				}
				if(inputStream != null){
					inputStream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		return data;
	}
	
	public static Object getHSSFCellValue(HSSFCell hssfCell){
		if(hssfCell == null) return null;
		Object value = null;
		
		switch(hssfCell.getCellType()){
			case Cell.CELL_TYPE_NUMERIC :  //数值 0
				value = hssfCell.getNumericCellValue();
				break;
			case Cell.CELL_TYPE_STRING :  //字符串 1
				value = hssfCell.getStringCellValue();
				break;
			case Cell.CELL_TYPE_FORMULA :  //公式 2
				value = hssfCell.getStringCellValue();
				break;
			case Cell.CELL_TYPE_BLANK :  //空值 3
				value = null;
				break;
			case Cell.CELL_TYPE_BOOLEAN :  //布尔 4
				value = hssfCell.getBooleanCellValue();
				break;
			case Cell.CELL_TYPE_ERROR :  //错误 5
				value = hssfCell.getErrorCellValue();
				break;
			default :
				value = hssfCell.getStringCellValue();
		}
		
		return value;
	}
	
	
	//xlsx 2007 之后版本
	public static String[][] getExcelDateAF2007(InputStream inputStream) {
		String[][] data = null;
		XSSFWorkbook xssfwb = null;
		try {
			xssfwb = new XSSFWorkbook(inputStream);
			
			XSSFSheet xssfSheet = xssfwb.getSheetAt(0);
			data = new String[xssfSheet.getLastRowNum()+1][];
			int headerColNum = 0;
			for(int i = 0; i<=xssfSheet.getLastRowNum();i++){
				XSSFRow xssfRow = xssfSheet.getRow(i);
				if(i == 0 ){
					headerColNum = xssfRow.getLastCellNum();
				}
				if(xssfRow == null ){
					data[i] = new String[0];
					continue;
				}
				data[i] = new String[headerColNum];
				for(int j = 0;j<xssfRow.getLastCellNum();j++){
					XSSFCell xssfCell = xssfRow.getCell(j);
					Object value = getXSSFCellValue(xssfCell);
					data[i][j] = value != null?String.valueOf(value): null;
				}
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				if(xssfwb != null){
					xssfwb.close();
				}
				if(inputStream != null){
					inputStream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		return data;
	}
	
	public static Object getXSSFCellValue(XSSFCell xssfCell){
		if(xssfCell == null) return null;
		Object value = null;
		
		switch(xssfCell.getCellType()){
			case Cell.CELL_TYPE_NUMERIC :  //数值 0
				value = xssfCell.getNumericCellValue();
				break;
			case Cell.CELL_TYPE_STRING :  //字符串 1
				value = xssfCell.getStringCellValue();
				break;
			case Cell.CELL_TYPE_FORMULA :  //公式 2
				value = xssfCell.getStringCellValue();
				break;
			case Cell.CELL_TYPE_BLANK :  //空值 3
				value = null;
				break;
			case Cell.CELL_TYPE_BOOLEAN :  //布尔 4
				value = xssfCell.getBooleanCellValue();
				break;
			case Cell.CELL_TYPE_ERROR :  //错误 5
				value = xssfCell.getErrorCellValue();
				break;
			default :
				value = xssfCell.getStringCellValue();
		}
		
		return value;
	}

}
工具类2
java 复制代码
package com.test.poi;

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.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * 加强版: xlsx/xls 合并成一种处理方式
 * @author zhaowanli
 *
 */
public class ExcelUtilReader2 {
	
	public static SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
	
	public static String[][] getExcelData(File file){
		if(file == null) return null;
		String name = file.getName();
		name = name.substring(name.lastIndexOf(".")+1);
		InputStream is = null;
		Workbook workbook = null;
		String[][] data = null;
		try {
			is = new FileInputStream(file);
		
			if("XLSX".equalsIgnoreCase(name)){
				workbook = new XSSFWorkbook(is);
				data = getExcelDate(workbook);
			}else if("XLS".equalsIgnoreCase(name)){
				workbook = new HSSFWorkbook(is);
				data = getExcelDate(workbook);
			}else{
				try {
					throw new Exception("文件格式不正确");
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				if(workbook != null){
					workbook.close();
				}
				if(is != null){
					is.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		
		}
		
		return data;
	}
	
	public static String[][] getExcelDate(Workbook workbook){
		String[][] data = null;
		
		Sheet sheet = workbook.getSheetAt(0);
		data = new String[sheet.getLastRowNum()+1][];
		int headerColNum = 0;
		for(int i = 0;i<=sheet.getLastRowNum();i++){
			Row row = sheet.getRow(i);
			if(i == 0 ){
				headerColNum = row.getLastCellNum();
			}
			if(row == null ){
				data[i] = new String[0];
				continue;
			}
			data[i] = new String[headerColNum];
			for(int j =0;j<row.getLastCellNum();j++){
				Cell cell = row.getCell(j);
				Object value = getCellValue(cell);
				data[i][j] = value!=null?String.valueOf(value):null;
			}
		}
		
		return data;
	}
	
	public static Object getCellValue(Cell cell){
		
		if(cell == null) return null;
		Object value = null;
		switch(cell.getCellType()){
		case Cell.CELL_TYPE_NUMERIC :
			value = cell.getNumericCellValue();
			break;
		case Cell.CELL_TYPE_STRING:
			value = cell.getStringCellValue();
			break;
		case Cell.CELL_TYPE_FORMULA :  //公式 2
			value = cell.getStringCellValue();
			break;
		case Cell.CELL_TYPE_BLANK :  //空值 3
			value = null;
			break;
		case Cell.CELL_TYPE_BOOLEAN :  //布尔 4
			value = cell.getBooleanCellValue();
			break;
		case Cell.CELL_TYPE_ERROR :  //错误 5
			value = cell.getErrorCellValue();
			break;
		default :
			value = cell.getStringCellValue();
		}
		return value;
	}

}
相关推荐
Haooog1 小时前
98.验证二叉搜索树(二叉树算法题)
java·数据结构·算法·leetcode·二叉树
武子康1 小时前
Java-143 深入浅出 MongoDB NoSQL:MongoDB、Redis、HBase、Neo4j应用场景与对比
java·数据库·redis·mongodb·性能优化·nosql·hbase
jackaroo20201 小时前
后端_基于注解实现的请求限流
java
道可到1 小时前
百度面试真题 Java 面试通关笔记 04 |JMM 与 Happens-Before并发正确性的基石(面试可复述版)
java·后端·面试
飞快的蜗牛2 小时前
利用linux系统自带的cron 定时备份数据库,不需要写代码了
java·docker
Love__Tay2 小时前
【数据分析与可视化】2025年一季度金融业主要行业资产、负债、权益结构与增速对比
金融·excel·pandas·matplotlib
聪明的笨猪猪2 小时前
Java Spring “IOC + DI”面试清单(含超通俗生活案例与深度理解)
java·经验分享·笔记·面试
ThisIsMirror2 小时前
CompletableFuture并行任务超时处理模板
java·windows·python
珹洺3 小时前
Java-Spring入门指南(二十一)Thymeleaf 视图解析器
java·开发语言·spring
源码集结号3 小时前
一套智慧工地云平台源码,支持监管端、项目管理端,Java+Spring Cloud +UniApp +MySql技术开发
java·mysql·spring cloud·uni-app·源码·智慧工地·成品系统