📄 excelread.java
字号:
package com.work.excel;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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.poifs.filesystem.POIFSFileSystem;
/**
* 用来读取excel文件。
* @author wangmj
*
*/
public class ExcelRead {
private static Log log = LogFactory.getLog(ExcelRead.class);
/**
* 读取excel的cell的值。
*
* @param cell
* HSSFCell
* @return String 要么返回空字符串,要么返回一个String类型的值。<br />
* 如果CELL的类型为CELL_TYPE_BLANK、CELL_TYPE_FORMULA、CELL_TYPE_BOOLEAN、CELL_TYPE_ERROR均返回空字符串。
*/
public static String getCellValue(HSSFCell cell) {
String result = "";
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_NUMERIC: // '\0' 读取float类型的cell
double tempd = 0.0;
try {
tempd = cell.getNumericCellValue();
} catch (NumberFormatException e) {
// 读String类型的cell会抛出异常,Blank类型的cell返回0
log.debug(e.getMessage());
tempd = 0.0;
}
result = tempd + "";
// result = result.substring(0, result.indexOf("."));
break;
case HSSFCell.CELL_TYPE_STRING: // '\001' 读取string类型的cell
result = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_BLANK: // '\003' 读取BLANK
result = "";
// fall through
case HSSFCell.CELL_TYPE_FORMULA: // '\002'
case HSSFCell.CELL_TYPE_BOOLEAN: // '\004'
case HSSFCell.CELL_TYPE_ERROR: // '\005'
default:
result = "";
break;
}
return result.trim();
}
/**
* 用来读取excel文件的第一个sheet的第一行。
*
* @param fileName
* 文件的绝对路径。例如:d:/logs/des.xls
* @return 返回一个字符串数组对象,长度为第一行的列数。
* @throws IOException
* 读取文件的时候出错。
* @throws FileNotFoundException
* 找不到读取的excel
*/
public static String[] readHead(String fileName) throws FileNotFoundException,
IOException {
if (fileName == null || fileName.equals("")) {
throw new NullPointerException("要读取的excel文件不存在!");
}
File f = new File(fileName);
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(f));
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
// 得到标题头,根据头填写实际字段
HSSFRow row = sheet.getRow(0); // 第一行为标题
HSSFCell cell = null;
int cellLEN = row.getLastCellNum();
String[] chHead = new String[cellLEN];
for (short b = 0; b < cellLEN; b++) {
cell = row.getCell(b);
if (cell != null) {
chHead[b] = getCellValue(cell);
} else {
chHead[b] = "";
}
}
return chHead;
}
/**
* 用来读取excel文件的第一个sheet的第一行,因为第一行一般为列的名字。
*
* @param fileName
* @return 返回一个字符串数组对象,内容包含标题列。
* @throws IOException
* 读取文件的时候出错。
* @throws FileNotFoundException
* 找不到读取的excel
*/
public static String[] readHead(File fileName) throws FileNotFoundException,
IOException {
if (fileName == null || !fileName.exists()) {
throw new NullPointerException("要读取的excel文件不存在!");
}
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(fileName));
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
// 得到标题头,根据头填写实际字段
HSSFRow row = sheet.getRow(0); // 第一行为标题
HSSFCell cell = null;
int cellLEN = row.getLastCellNum();
String[] chHead = new String[cellLEN];
for (short b = 0; b < cellLEN; b++) {
cell = row.getCell(b);
if (cell != null) {
chHead[b] = getCellValue(cell);
} else {
chHead[b] = "";
}
}
return chHead;
}
/**
* 读取excel的第一个sheet的文件的内容,从第二行开始,但是不读取最后一行。
*
* @param fileName
* 文件对象。
* @return 返回一个List对象,包含的是String[]对象。
* @throws IOException
* 读取文件的时候出错。
* @throws FileNotFoundException
* 找不到读取的excel
*/
public static List readOtherConent(File fileName) throws FileNotFoundException,
IOException {
if (fileName == null || !fileName.exists()) {
throw new NullPointerException("要读取的excel文件不存在!");
}
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(fileName));
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
int rowNum = sheet.getLastRowNum(); // 获取总共有多少行,然后从第一行开始读取内容
HSSFRow row = sheet.getRow(0);
int colNum = row.getLastCellNum();// 获取总共有多少列
HSSFCell cell = null;
String[] tempRow = null;
List l = new ArrayList(rowNum); // 用来保存最后的结果。
for (short i = 1; i < rowNum; i++) { // 注意目前没有读取最后一行。
row = sheet.getRow(i);
tempRow = new String[colNum];
for (short j = 0; j < colNum; j++) {
cell = row.getCell(j);
if (cell != null)
tempRow[j] = getCellValue(cell);
else
tempRow[j] = "";
}
l.add(tempRow);
}
return l;
}
/**
* 读取excel的第一个sheet的文件的内容,从第二行开始,但是不读取最后一行。
*
* @param fileName 文件的绝对路径,例如: d:/log/dest.xls
* @return 返回一个List对象,包含的是String[]对象。
* @throws IOException
* 读取文件的时候出错。
* @throws FileNotFoundException
* 找不到读取的excel
*/
public static List readOtherConent(String fileName) throws FileNotFoundException,
IOException {
if (fileName == null || fileName.equals("")) {
throw new NullPointerException("要读取的excel文件不存在!");
}
File f = new File(fileName);
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(f));
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
//wb.getNumberOfSheets();//获取有多少个sheet
int rowNum = sheet.getLastRowNum(); // 获取总共有多少行,然后从第一行开始读取内容
HSSFRow row = sheet.getRow(0);
int colNum = row.getLastCellNum();// 获取总共有多少列
HSSFCell cell = null;
String[] tempRow = null;
List l = new ArrayList(rowNum); // 用来保存最后的结果。
// for (short i = 1; i <= rowNum; i++) { 读取所有的行。
for (short i = 1; i < rowNum; i++) {
row = sheet.getRow(i);
tempRow = new String[colNum];
for (short j = 0; j < colNum; j++) {
cell = row.getCell(j);
if (cell != null)
tempRow[j] = getCellValue(cell);
else
tempRow[j] = "";
}
l.add(tempRow);
}
return l;
}
/**
* 读取excel的第一个sheet的文件的所有内容.
*
* @param fileName 文件的绝对路径,例如: d:/log/dest.xls
* @return 返回一个List对象,包含的是String[]对象。
* @throws IOException
* 读取文件的时候出错。
* @throws FileNotFoundException
* 找不到读取的excel
*/
public static List readAllConent(String fileName) throws FileNotFoundException,
IOException {
if (fileName == null || fileName.equals("")) {
throw new NullPointerException("要读取的excel文件不存在!");
}
File f = new File(fileName);
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(f));
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
int rowNum = sheet.getLastRowNum(); // 获取总共有多少行,然后从第一行开始读取内容
HSSFRow row = sheet.getRow(0);
int colNum = row.getLastCellNum();// 获取总共有多少列
HSSFCell cell = null;
String[] tempRow = null;
List l = new ArrayList(rowNum); // 用来保存最后的结果。
// for (short i = 1; i <= rowNum; i++) { 读取所有的行。
for (short i = 0; i <= rowNum; i++) {
row = sheet.getRow(i);
tempRow = new String[colNum];
for (short j = 0; j < colNum; j++) {
cell = row.getCell(j);
if (cell != null)
tempRow[j] = getCellValue(cell);
else
tempRow[j] = "";
}
l.add(tempRow);
}
return l;
}
/**
* 读取excel的第一个sheet的文件的内容。
*
* @param fileName
* 文件对象。
* @return 返回一个List对象,包含的是String[]对象。
* @throws IOException
* 读取文件的时候出错。
* @throws FileNotFoundException
* 找不到读取的excel
*/
public static List readAllConent(File fileName) throws FileNotFoundException,
IOException {
if (fileName == null || !fileName.exists()) {
throw new NullPointerException("要读取的excel文件不存在!");
}
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(fileName));
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
int rowNum = sheet.getLastRowNum(); // 获取总共有多少行,然后从第一行开始读取内容
HSSFRow row = sheet.getRow(0);
int colNum = row.getLastCellNum();// 获取总共有多少列
HSSFCell cell = null;
String[] tempRow = null;
List l = new ArrayList(rowNum); // 用来保存最后的结果。
for (short i = 0; i <= rowNum; i++) {
row = sheet.getRow(i);
tempRow = new String[colNum];
for (short j = 0; j < colNum; j++) {
cell = row.getCell(j);
if (cell != null)
tempRow[j] = getCellValue(cell);
else
tempRow[j] = "";
}
l.add(tempRow);
}
return l;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -