package com.axhu.util.excel;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
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;
public class ExcelUtil {
/**
* 方法描述:从指定路径读出Excel的值
*
* 参数描述:即要读取文件的路径
*
*/
public static void importExcel(String filePath) {
Boolean bl = null;
if(validateExcel(filePath)){
//默认>2003版本//验证excel版本,true是2003,false是2007
bl = isExcel2003(filePath);
}else{
System.out.println("文件选择错误!!!");
}
/** 调用本类提供的根据流读取的方法 */
File file = new File(filePath);
InputStream inputStream = null;
//创建Excel对象
Workbook book = null;
try {
inputStream = new FileInputStream(file);
if (bl) {
book = new HSSFWorkbook(inputStream);
} else {
book = new XSSFWorkbook(inputStream);
}
/**
* 如果Excel表格不只一页的话,那就用
* int page = book.getActiveSheetIndex();
* 然后类似底下遍历行和列,用for循环遍历遍历即可
* 获取excel的页数,若该Excel只有一页,得到的page值为0,
* 若该Excel有两页,得到的page值为1,以此类推.
* 同时,获取的行数和每一行的列数也都是以0开始的
*/
//获取excel的页数
int pages = book.getActiveSheetIndex();
//获得第一个工作表对象.第0页
Sheet sheet = book.getSheetAt(0);
//获取该页的名称
String sheetName = sheet.getSheetName();
//String sheetName = book.getSheetName(0);//效果同上
System.out.println("该Excel文件总共有"+pages+"页(从0开始),这是第0页,该页的名称是:"+sheetName);
//excel的总行数
int rows = sheet.getPhysicalNumberOfRows();
int columns = 0;
if (rows >= 1 && sheet.getRow(0) != null) {
//excel总列数
columns = sheet.getRow(0).getPhysicalNumberOfCells();
}
//遍历每行
for (int i = 0; i < rows; i++) {
//获取每一行
Row row = sheet.getRow(i);
//单元格的值
String result = "";
//循环改(i)行每(j)列的单元格
for (int j = 0; j < columns; j++) {
//获取每一行的每一列
Cell cell = row.getCell(j);
if (null != cell) {
//以下是判断数据的类型,根据类型,选择不同的获取数据的方法
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_NUMERIC: //数字
result = cell.getNumericCellValue() + "";
break;
case HSSFCell.CELL_TYPE_STRING: //字符串
result = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_BOOLEAN: //Boolean
result = cell.getBooleanCellValue() + "";
break;
case HSSFCell.CELL_TYPE_FORMULA: //公式
result = cell.getCellFormula() + "";
break;
case HSSFCell.CELL_TYPE_BLANK: //空值
result = "";
break;
case HSSFCell.CELL_TYPE_ERROR: //故障
result = "非法字符";
break;
default:
result = "未知类型";
break;
}
System.out.print(result+"\t");
}
}
//换行
System.out.println();
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if(inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 作用: 是否为Excel2003,true是2003,false不是
* 参数: 文件的路径
*/
public static boolean isExcel2003(String filePath) {
return filePath.matches("^.+\\.(?i)(xls)$");
}
/**
* 作用: 是否为Excel2003,true是2003,false不是
* 是否为Excel2007,true是2003,false不是
*/
public static boolean isExcel2007(String filePath) {
return filePath.matches("^.+\\.(?i)(xlsx)$");
}
/**
* 作用: 检查文件是否存在
* 检查文件名是否为空或者是否是Excel格式的文件
* 参数: 文件的路径
*/
public static boolean validateExcel(String filePath) {
/* 检查文件名是否为空或者是否是Excel格式的文件 */
if (filePath == null
|| !(isExcel2003(filePath) ||isExcel2007(filePath))) {
System.out.println("不是Excel文件!!!");
return false;
}
/* 检查文件名是否为空或者是否是Excel格式的文件 */
File file = new File(filePath);
if (file == null || !file.exists()) {
System.out.println("文件不存在!!!");
return false;
}
return true;
}
}
Java Excel 导入
最新推荐文章于 2024-11-22 16:40:03 发布