使用poi插件解析Excel檔案
下載下傳插件poi-bin-3.8
package com;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
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;
import org.springframework.stereotype.Component;
import com.csvreader.CsvReader;
import com.elin.csp.model.shopUser.entity.ShopUser;
@Component
public class InputExcelFileService {
private int totalRows = 0;
private int totalCells = 0;
private String errorInfo;
public InputExcelFileService() {
}
public int getTotalRows() {
return totalRows;
}
public int getTotalCells() {
return totalCells;
}
public String getErrorInfo() {
return errorInfo;
}
public boolean validateExcel(String fileName) {
if (fileName == null
|| !(WDWUtil.isExcel2003(fileName) || WDWUtil
.isExcel2007(fileName))) {
errorInfo = "檔案名不是excel格式";
return false;
}
File file = new File(fileName);
if (file == null || !file.exists()) {
errorInfo = "檔案不存在";
return false;
}
return true;
}
public List> read(String fileName) {
List> dataLst = new ArrayList>();
InputStream is = null;
try {
if (!validateExcel(fileName)) {
System.out.println(errorInfo);
return null;
}
boolean isExcel2003 = true;
if (WDWUtil.isExcel2007(fileName)) {
isExcel2003 = false;
}
File file = new File(fileName);
is = new FileInputStream(file);
dataLst = read(is, isExcel2003);
is.close();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
is = null;
e.printStackTrace();
}
}
}
return dataLst;
}
public List> read(InputStream inputStream, boolean isExcel2003) {
List> dataLst = null;
try {
Workbook wb = isExcel2003 ? new HSSFWorkbook(inputStream)
: new XSSFWorkbook(inputStream);
dataLst = read(wb);
} catch (IOException e) {
e.printStackTrace();
}
return dataLst;
}
private List> read(Workbook wb) {
List> dataLst = new ArrayList>();
Sheet sheet = wb.getSheetAt(0);
this.totalRows = sheet.getPhysicalNumberOfRows();
if (this.totalRows >= 1 && sheet.getRow(0) != null) {
this.totalCells = sheet.getRow(0).getPhysicalNumberOfCells();
}
for (int r = 0; r < this.totalRows; r++) {
Row row = sheet.getRow(r);
if (row == null) {
continue;
}
List rowLst = new ArrayList();
for (short c = 0; c < this.getTotalCells(); c++) {
Cell cell = row.getCell(c);
String cellValue = "";
if (cell == null) {
rowLst.add(cellValue);
continue;
}
// cellValue = cell.getStringCellValue();
// rowLst.add(cellValue);
switch (cell.getCellType()) {
// 假如目前Cell的Type為NUMERIC
case HSSFCell.CELL_TYPE_NUMERIC: {
// 判定目前的cell是否為Date
if (HSSFDateUtil.isCellDateFormatted(cell)) {
// 假如是Date類型則,取得該Cell的Date值
Date date = cell.getDateCellValue();
// 把Date轉換本錢地格式的字元串
cellValue = cell.getDateCellValue().toLocaleString();
}
// 假如是純數字
else {
// 純數字時,一般為電話号碼或手機号,是以以字元串類型儲存
cell.setCellType(Cell.CELL_TYPE_STRING);
cellValue = cell.getStringCellValue();
// // 取得目前Cell的數值
// Integer num = new Integer((int)
// cell.getNumericCellValue());
// cellValue = String.valueOf(num);
}
break;
}
// 假如目前Cell的Type為STRIN
case HSSFCell.CELL_TYPE_STRING:
// 取得目前的Cell字元串
cellValue = cell.getStringCellValue().replaceAll("'", "''");
break;
// 預設的Cell值
default:
cellValue = " ";
}
rowLst.add(cellValue);
}
dataLst.add(rowLst);
}
return dataLst;
}
public static void main(String[] args) throws Exception {
InputExcelFileService poi = new InputExcelFileService();
// List> list = poi.read("d:/aaa.xls");
List> list = poi.read("d:/aab.xlsx");
if (list != null) {
for (int i = 0, ilen = list.size(); i < ilen; i++) {
System.out.println("第" + (i + 1) + "行");
List cellList = list.get(i);
for (int j = 0, jlen = cellList.size(); j < jlen; j++) {
System.out.print(" 第" + (j + 1) + "列值:");
System.out.println(cellList.get(j));
}
}
}
}
public List wdwExcelMethod(String path) {
InputExcelFileService poi = new InputExcelFileService();
List> list = poi.read(path);
return list;
}
}
class WDWUtil {
public static boolean isExcel2003(String fileName) {
return fileName.matches("^.+\\.(?i)(xls)$");
}
public static boolean isExcel2007(String fileName) {
return fileName.matches("^.+\\.(?i)(xlsx)$");
}
}