java 讀取excel工具記錄
阿新 • • 發佈:2018-12-09
依賴:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId >poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
ExcelUtil:
import org.apache.log4j.Logger;
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.ss.usermodel.Workbook;
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;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class ExcelUtil {
private static final Logger logger = Logger.getLogger(ExcelUtil.class);
public static List<List<String>> readExcel(File file) throws Exception {
String fileName = file.getName();
String[] format = fileName.split("\\.");
FileInputStream fis = new FileInputStream(file); //檔案流物件
logger.info("格式:" + format[1]);
Workbook wb;
//根據檔案字尾(xls/xlsx)進行判斷
if ("xls".equals(format[1])) {
return readXls(fis);
} else if ("xlsx".equals(format[1])) {
return readXlsx(fis);
}
return null;
}
private static List<List<String>> readXls(InputStream is) throws Exception {
// HSSFWorkbook 標識整個excel
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
List<List<String>> result = new ArrayList<List<String>>();
int size = hssfWorkbook.getNumberOfSheets();
// 迴圈每一頁,並處理當前迴圈頁
for (int numSheet = 0; numSheet < size; numSheet++) {
// HSSFSheet 標識某一頁
HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
if (hssfSheet == null) {
continue;
}
// 處理當前頁,迴圈讀取每一行
for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
// HSSFRow表示行
HSSFRow hssfRow = hssfSheet.getRow(rowNum);
int minColIx = hssfRow.getFirstCellNum();
int maxColIx = hssfRow.getLastCellNum();
List<String> rowList = new ArrayList<String>();
// 遍歷改行,獲取處理每個cell元素
for (int colIx = minColIx; colIx < maxColIx; colIx++) {
// HSSFCell 表示單元格
HSSFCell cell = hssfRow.getCell(colIx);
if (cell == null) {
continue;
}
rowList.add(getStringVal(cell));
}
result.add(rowList);
}
}
return result;
}
/**
* @param @param path
* @param @return
* @param @throws Exception 設定檔案
* @return List<List < String>> 返回型別
* @throws
* @Title: readXlsx
* @Description: 處理Xlsx檔案
*/
private static List<List<String>> readXlsx(InputStream is) throws Exception {
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is);
List<List<String>> result = new ArrayList<List<String>>();
// 迴圈每一頁,並處理當前迴圈頁
for (XSSFSheet xssfSheet : xssfWorkbook) {
if (xssfSheet == null) {
continue;
}
// 處理當前頁,迴圈讀取每一行
for (int rowNum = 1; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {
XSSFRow xssfRow = xssfSheet.getRow(rowNum);
int minColIx = xssfRow.getFirstCellNum();
int maxColIx = xssfRow.getLastCellNum();
List<String> rowList = new ArrayList<String>();
for (int colIx = minColIx; colIx < maxColIx; colIx++) {
XSSFCell cell = xssfRow.getCell(colIx);
if (cell == null) {
continue;
}
rowList.add(getStringVal(cell));
}
result.add(rowList);
}
}
return result;
}
private static String getStringVal(Cell cell) {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
return cell.getBooleanCellValue() ? "TRUE" : "FALSE";
case Cell.CELL_TYPE_FORMULA:
return cell.getCellFormula();
case Cell.CELL_TYPE_NUMERIC:
cell.setCellType(Cell.CELL_TYPE_STRING);
return cell.getStringCellValue();
case Cell.CELL_TYPE_STRING:
return cell.getStringCellValue();
default:
return "";
}
}
}