package cn.ccb.jstsccf.common.utils;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/**
* 解析EXCEL工具类
*
* @author ghl
*
*/
public class ParseExcel {
/**
* 初始化读取EXCEL表数据方法
*
* @param file
* `需要读取的文件对象输入流
* @param sheetIndex
* 需要读取第几个表的数据,索引从0开始
* @param startRow
* 从第几行开始读取数据,索引从0开始
* @param cellInRow
* 数据表的有效数据列数
* @return List 返回包含行和列数据的一个集合,其中所有的数据均为字符串
*/
public static List parseExcel(InputStream file, int sheetIndex,
int startRow, int cellInRow) throws Exception {
// 创建EXCEL文件对像
HSSFWorkbook workBook = null;
// 创建一个表对象
HSSFSheet sheet = null;
try {
workBook = new HSSFWorkbook(file,false);
} catch (FileNotFoundException e) {
throw e;
} catch (IOException e) {
throw e;
}
List rowList = new ArrayList();
List cellList = null;
sheet = workBook.getSheetAt(sheetIndex);
try {
// 循环读取行数据
for (int i = startRow; i < sheet.getPhysicalNumberOfRows(); i++) {
// HSSFRow为EXCEL行对象
HSSFRow row = sheet.getRow(i);
//int numberOfCells = row.getPhysicalNumberOfCells();
/*
* // 如果读取到某行的数据中的单元格的列数少于正常的数据列数,则这列被视为非法数据,不导入到数据库 if
* (numberOfCells < cellInRow) { continue; }
*/
cellList = new ArrayList();
// 循环读取列数据
for (int j = 0; j < cellInRow; j++) {
// HSSFCell为EXCEL列对象
HSSFCell cell = row.getCell(j);
if (null == cell) {
cellList.add("");
continue;
}
String cellStr = getCell(cell);
// 如果此数据行为合计数据行,则不导入到数据库(记得一定要TRIM)
if ("合计".equals(cellStr)) {
break;
}
cellList.add(cellStr);
}
// 如果不是空白行才读出
if (!isblankRow(cellList)) {
rowList.add(cellList);
}
}
} catch (Exception e) {
throw e;
}
return rowList;
}
/**
* 获取某行某列的字符内容
*
* @param cell
* @return
*/
private static String getCell(HSSFCell cell) {
if (!(null == cell || cell.equals(new String("")))) {
// 判断单元格是否为字符串值
if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
return cell.getRichStringCellValue().getString();
}
// 判断单元格是否为日期值
else if (HSSFDateUtil.isCellDateFormatted(cell)) {
return cell.getDateCellValue().toLocaleString();
}
// 判断单元格是否为数字值
else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
DecimalFormat df = new DecimalFormat(
"################.##");
String numericeValue = df.format(cell
.getNumericCellValue());
return numericeValue;
} else {
HSSFRichTextString stringCellValue = cell
.getRichStringCellValue();
return stringCellValue.getString();
}
}
return "";
}
/**
* 判断行是不是空白行
*
* @param cellList
* @return
*/
private static boolean isblankRow(List cellList) {
if (cellList == null || cellList.size() == 0) {
return true;
}
for (Iterator iter = cellList.iterator(); iter.hasNext();) {
String element = (String) iter.next();
if (StringUtils.isNotBlank(element)) {
return false;
}
}
return true;
}
/**
* 初始化读取EXCEL表数据方法
*
* @param file
* `需要读取的文件对象输入流
* @param sheetIndex
* 需要读取第几个表的数据,索引从0开始
* @param rowIndex
* 读取第几行数据,索引从0开始
* @param cellInRow
* 读取第几列的数据
* @return String 返回某一行和某一列数据的字符串
*/
public static String getCell(InputStream file, int sheetIndex,
int rowIndex, int cellInRow) throws Exception {
// 创建EXCEL文件对像
HSSFWorkbook workBook = null;
// 创建一个表对象
HSSFSheet sheet = null;
try {
workBook = new HSSFWorkbook(file, false);
} catch (FileNotFoundException e) {
throw e;
} catch (IOException e) {
throw e;
}
sheet = workBook.getSheetAt(sheetIndex);
// HSSFRow为EXCEL行对象
HSSFRow row = sheet.getRow(rowIndex);
// HSSFCell为EXCEL列对象
HSSFCell cell = row.getCell(cellInRow);
return getCell(cell);
}
public static void main(String[] args) throws FileNotFoundException, Exception {
// 创建EXCEL文件对像
HSSFWorkbook workBook = null;
// 创建一个表对象
HSSFSheet sheet = null;
try {
workBook = new HSSFWorkbook(new FileInputStream("D:\\skyon\\信用卡辅助管理系统ODSB迁移\\逾期及还款信息文档\\江苏100527.xls"));
} catch (FileNotFoundException e) {
throw e;
} catch (IOException e) {
throw e;
}
sheet = workBook.getSheetAt(0);
System.out.println(sheet.getPhysicalNumberOfRows());
// String ss = ParseExcel.getCell(new FileInputStream("D:\\skyon\\信用卡辅助管理系统ODSB迁移\\逾期及还款信息文档\\江苏100527.xls"), 0, 1, 0);
// System.out.println(ss);
}
}