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;
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.poifs.filesystem.OfficeXmlFileException;
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.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import javax.servlet.http.HttpServletRequest;
@ -219,61 +213,91 @@ public class ExcelKit {
}
}
//读取 excel
//读取excel表头
public static Map readExcelHead(File file, String[] fields) throws IOException {
List<Map> list = null;
//======================= 读取excel ===============================
try {
list = readExcel(new FileInputStream(file), fields, 0);//2007 -
} catch (OfficeXmlFileException e) {
list = readExcelPlus(new FileInputStream(file), fields, 0);//2007 +
}
return list.size() > 0 ? list.get(0) : new HashMap();
}
//读取excel表头
public static Map readExcelHead(String fPath, String[] fields) throws IOException {
return readExcelHead(new File(fPath), fields);
/**
* read excel head
* @param file
* @param fields
* @param lastRowNum hope get row count
* @return
* @throws IOException
*/
public static List<Map> readExcelHead(File file, String[] fields, int lastRowNum) throws IOException {
return readExcel(file, fields, lastRowNum, null);
}
//read excel sheet[0]
public static List<Map> readExcel(File file, String[] fields) throws IOException {
List<Map> list = null;
try {
list = readExcel(new FileInputStream(file), fields, -1);//2007 -
} catch (OfficeXmlFileException e) {
list = readExcelPlus(new FileInputStream(file), fields, -1);//2007 +
}
//去除表头
if (list.size() > 0){
list.remove(0);
}
return list;
return readExcel(file, fields, -1, null);
}
public static List<Map> readExcel(String fPath, String[] fields) throws IOException {
return readExcel(new File(fPath), fields);
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 {
wk = new HSSFWorkbook(new FileInputStream(file));//if excel version 2007+ will throws OfficeXmlFileException
}catch (OfficeXmlFileException e){
wk = new XSSFWorkbook(new FileInputStream(file));
}
Sheet sheet = sheetName == null ? wk.getSheetAt(0) : wk.getSheet(sheetName);
return readExcel(sheet, fields, lastRowNum);
}
/**
* 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.
*/
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<>();
HSSFWorkbook wk = new HSSFWorkbook(is);
HSSFSheet sheet = wk.getSheetAt(0);
if (lastRowNum < 0 || lastRowNum > sheet.getLastRowNum()){
lastRowNum = sheet.getLastRowNum();
}
int t = 0;
r:for (int i=0; i<=lastRowNum; i++){
HSSFRow row = sheet.getRow(i);
Row row = sheet.getRow(i);
if (row == null) continue ;
short cellNum = row.getLastCellNum();
//空跳过/连续三行为空结束
@ -284,7 +308,7 @@ public class ExcelKit {
Map map = new HashMap();
for (int j=0; j<cellNum && j<fields.length; j++){
HSSFCell cell = row.getCell(j);
Cell cell = row.getCell(j);
if (cell == null){
map.put(fields[j], "");
continue;
@ -302,78 +326,16 @@ public class ExcelKit {
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++) {
XSSFCell cell = row.getCell(i);//
Cell 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;
}
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;
}