add get excel data by sheetName

This commit is contained in:
lxyer 2018-08-18 22:24:30 +08:00
parent e719a6e055
commit 63aa3303c9

View File

@ -1,17 +1,11 @@
package excel.poi; package excel.poi;
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.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.OfficeXmlFileException; import org.apache.poi.poifs.filesystem.OfficeXmlFileException;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook; 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 org.apache.poi.xssf.usermodel.XSSFWorkbook;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
@ -219,61 +213,91 @@ public class ExcelKit {
} }
} }
//读取 excel //======================= 读取excel ===============================
//读取excel表头
public static Map readExcelHead(File file, String[] fields) throws IOException {
List<Map> list = null;
try { /**
list = readExcel(new FileInputStream(file), fields, 0);//2007 - * read excel head
} catch (OfficeXmlFileException e) { * @param file
list = readExcelPlus(new FileInputStream(file), fields, 0);//2007 + * @param fields
} * @param lastRowNum hope get row count
return list.size() > 0 ? list.get(0) : new HashMap(); * @return
} * @throws IOException
*/
//读取excel表头 public static List<Map> readExcelHead(File file, String[] fields, int lastRowNum) throws IOException {
public static Map readExcelHead(String fPath, String[] fields) throws IOException { return readExcel(file, fields, lastRowNum, null);
return readExcelHead(new File(fPath), fields);
} }
//read excel sheet[0]
public static List<Map> readExcel(File file, String[] fields) throws IOException { public static List<Map> readExcel(File file, String[] fields) throws IOException {
List<Map> list = null; return readExcel(file, fields, -1, null);
}
public static List<Map> readExcel(File file, String[] fields, String sheetName) throws IOException {
return readExcel(file, fields, -1, sheetName);
}
//read excel all sheet
public static Map<String, List<Map>> readExcelAll(File file, String[] fields) throws IOException {
return readExcelAll(file, fields, -1);
}
//read excel sheet[0]
private static List<Map> readExcel(File file, String[] fields, int lastRowNum, String sheetName) throws IOException {
Workbook wk;
try { try {
list = readExcel(new FileInputStream(file), fields, -1);//2007 - wk = new HSSFWorkbook(new FileInputStream(file));//if excel version 2007+ will throws OfficeXmlFileException
}catch (OfficeXmlFileException e){ }catch (OfficeXmlFileException e){
list = readExcelPlus(new FileInputStream(file), fields, -1);//2007 + wk = new XSSFWorkbook(new FileInputStream(file));
} }
Sheet sheet = sheetName == null ? wk.getSheetAt(0) : wk.getSheet(sheetName);
//去除表头 return readExcel(sheet, fields, lastRowNum);
if (list.size() > 0){
list.remove(0);
}
return list;
}
public static List<Map> readExcel(String fPath, String[] fields) throws IOException {
return readExcel(new File(fPath), fields);
} }
/** /**
* excel读取2007- * read excel all sheet,
* can read excel 2007+ and 2007-
* @param file
* @param fields
* @param lastRowNum
* @return
* @throws IOException
*/
private static Map<String, List<Map>> readExcelAll(File file, String[] fields, int lastRowNum) throws IOException {
Workbook wk;
try {
wk = new HSSFWorkbook(new FileInputStream(file));//if excel version 2007+ will throws OfficeXmlFileException
}catch (OfficeXmlFileException e){
wk = new XSSFWorkbook(new FileInputStream(file));
}
Map<String, List<Map>> data = new LinkedHashMap<>();
//reading all sheet
for (int i = 0; i < wk.getNumberOfSheets(); i++) {
Sheet sheet = wk.getSheetAt(i);
List<Map> maps = readExcel(sheet, fields, lastRowNum);
//move head
if (lastRowNum < 0 && maps.size() > 0){
maps.remove(0);
}
data.put(sheet.getSheetName(), maps);
}
return data;
}
/**
* read excel
* @author Lxyer 2016/8/1 10:32. * @author Lxyer 2016/8/1 10:32.
*/ */
private static List<Map> readExcel(InputStream is, String[] fields, int lastRowNum) throws IOException,OfficeXmlFileException { private static List<Map> readExcel(Sheet sheet, String[] fields, int lastRowNum) throws OfficeXmlFileException {
List<Map> list = new ArrayList<>(); List<Map> list = new ArrayList<>();
HSSFWorkbook wk = new HSSFWorkbook(is);
HSSFSheet sheet = wk.getSheetAt(0);
if (lastRowNum < 0 || lastRowNum > sheet.getLastRowNum()){ if (lastRowNum < 0 || lastRowNum > sheet.getLastRowNum()){
lastRowNum = sheet.getLastRowNum(); lastRowNum = sheet.getLastRowNum();
} }
int t = 0; int t = 0;
r:for (int i=0; i<=lastRowNum; i++){ r:for (int i=0; i<=lastRowNum; i++){
HSSFRow row = sheet.getRow(i); Row row = sheet.getRow(i);
if (row == null) continue ; if (row == null) continue ;
short cellNum = row.getLastCellNum(); short cellNum = row.getLastCellNum();
//空跳过/连续三行为空结束 //空跳过/连续三行为空结束
@ -284,7 +308,7 @@ public class ExcelKit {
Map map = new HashMap(); Map map = new HashMap();
for (int j=0; j<cellNum && j<fields.length; j++){ for (int j=0; j<cellNum && j<fields.length; j++){
HSSFCell cell = row.getCell(j); Cell cell = row.getCell(j);
if (cell == null){ if (cell == null){
map.put(fields[j], ""); map.put(fields[j], "");
continue; continue;
@ -302,78 +326,16 @@ public class ExcelKit {
return list; return list;
} }
/**
* excel读取 2007+
* @author Lxyer 2016/8/1 10:32.
*/
private static List<Map> readExcelPlus(InputStream is, String[] fields, int lastRowNum) throws IOException {
List<Map> list = new ArrayList<>();
XSSFWorkbook wk = new XSSFWorkbook(is);
XSSFSheet sheet = wk.getSheetAt(0);
if (lastRowNum < 0 || lastRowNum > sheet.getLastRowNum()){
lastRowNum = sheet.getLastRowNum();
}
int t=0;
r:for (int i=0; i<=lastRowNum; i++){
XSSFRow row = sheet.getRow(i);
if (row == null) continue ;
short cellNum = row.getLastCellNum();
if (isEmptyRow(row, fields.length)) {
if (t++ > 3) break;
continue ;
}
Map map = new HashMap();
//读取数据
for (int j=0; j<cellNum && j<fields.length; j++){
XSSFCell cell = row.getCell(j);
if (cell == null){
map.put(fields[j], "");
continue;
}
int cellType = cell.getCellType();
if (cellType == 0){
map.put(fields[j], (long)cell.getNumericCellValue()+"");
}else {
map.put(fields[j], cell.getStringCellValue());
}
}
list.add(map);
}
return list;
}
//空跳过/连续三行为空结束 //空跳过/连续三行为空结束
private static boolean isEmptyRow(XSSFRow row, int len){ private static boolean isEmptyRow(Row row, int len){
for (int i = 0; i< row.getLastCellNum() && i < len; i++) { for (int i = 0; i< row.getLastCellNum() && i < len; i++) {
XSSFCell cell = row.getCell(i);// Cell cell = row.getCell(i);//
if (cell != null && cell.getCellType() != 0 if (cell != null && cell.getCellType() != 0
&& cell.getStringCellValue() != null && cell.getStringCellValue() != null
&& !cell.getStringCellValue().isEmpty()){ && !cell.getStringCellValue().isEmpty()){
//System.out.println("---F");
return false; return false;
} }
} }
//System.out.println("---T");
return true;
}
private static boolean isEmptyRow(HSSFRow row, int len){
for (int i = 0; i< row.getLastCellNum() && i < len; i++) {
HSSFCell cell = row.getCell(i);//
if (cell != null && cell.getCellType() != 0
&& cell.getStringCellValue() != null
&& !cell.getStringCellValue().isEmpty()){
//System.out.println("---F");
return false;
}
}
//System.out.println("---T");
return true; return true;
} }