java常用組件之解析excel工具--ParseExcelUtils

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);
}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章