初始化readExcel对象 26为excel列数
String file_id = ra.getParameter("file_id");
String filePath = ra.getFileWritePath() + "files/" + file_id;
File file = new File(filePath);
ReadExcel readExcel = new ReadExcel(26);
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
try {
list = readExcel.getExcelInfo(file, filePath, 0);
file.delete();
} catch (Exception e) {
e.printStackTrace();
msg = "数据解析错误,请检查模板";
System.err.println("数据解析错误,请检查模板");
}
if (list.size() > 0) {
try {
for (int i = 0; i < list.size(); i++) {
boolean kill = true;
Map<String, Object> map = list.get(i);
map.get("cell0").toString().trim()
}catch (Exception e) {
log().error(e.getMessage());
msg = e.getMessage();
}
}
}else {
success = false;
msg = "请检查文件";
}
工具类
package com.originsys.dalianjinpu.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.hssf.usermodel.HSSFCell;
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;
public class ReadExcel {
private int totalRows = 0;
private int totalCells = 0;
private int x = 0;
private String errorMsg;
public ReadExcel() {
}
public int getTotalRows() {
return totalRows;
}
public int getTotalCells() {
return totalCells;
}
public String getErrorInfo() {
return errorMsg;
}
public ReadExcel(int a) {
this.x=a;
}
public List<Map<String, Object>> getExcelInfo(File File,String path,int sheetnub) {
String fileName = File.getName();
try {
if (!validateExcel(fileName)) {
return null;
}
boolean isExcel2003 = true;
if (isExcel2007(fileName)) {
isExcel2003 = false;
}
FileInputStream in = new FileInputStream(path);
System.out.println("流:"+in);
return createExcel(in, isExcel2003,sheetnub);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public List<Map<String, Object>> createExcel(FileInputStream is, boolean isExcel2003,int sheetnub) {
try {
Workbook wb = null;
if (isExcel2003) {
wb = new HSSFWorkbook(is);
} else {
wb = new XSSFWorkbook(is);
}
return readExcelValue(wb,sheetnub);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private List<Map<String, Object>> readExcelValue(Workbook wb,int sheetnub) {
Sheet sheet = wb.getSheetAt(sheetnub);
this.totalRows = sheet.getPhysicalNumberOfRows();
if (totalRows > 1 && sheet.getRow(0) != null) {
this.totalCells = sheet.getRow(0).getPhysicalNumberOfCells();
}
List<Map<String, Object>> userList = new ArrayList<Map<String, Object>>();
for (int r = 2; r < totalRows; r++) {
Row row = sheet.getRow(r);
if (row == null) {
continue;
}
Map<String, Object> map = new HashMap<String, Object>();
for (int c = 0; c < x; c++) {
Cell cell = row.getCell(c);
if (null != cell) {
if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
String name = String.valueOf(cell.getNumericCellValue());
name = name == null ? name : name.trim().replaceAll("\r|\t|\n","");
map.put("cell"+c, name);
} else {
map.put("cell"+c, cell.getStringCellValue() == null? null :cell.getStringCellValue().trim().replaceAll("\r|\t|\n",""));
}
}
}
userList.add(map);
}
return userList;
}
public boolean validateExcel(String filePath) {
if (filePath == null || !(isExcel2003(filePath) || isExcel2007(filePath))) {
errorMsg = "文件名不是excel格式";
return false;
}
return true;
}
public static boolean isExcel2003(String filePath) {
return filePath.matches("^.+\\.(?i)(xls)$");
}
public static boolean isExcel2007(String filePath) {
return filePath.matches("^.+\\.(?i)(xlsx)$");
}
}