add readExcel for "NO fields"
This commit is contained in:
parent
a10f7e3680
commit
352c5934e3
5
pom.xml
5
pom.xml
@ -10,6 +10,11 @@
|
|||||||
<build>
|
<build>
|
||||||
<sourceDirectory>src</sourceDirectory>
|
<sourceDirectory>src</sourceDirectory>
|
||||||
<testSourceDirectory>test</testSourceDirectory>
|
<testSourceDirectory>test</testSourceDirectory>
|
||||||
|
<resources>
|
||||||
|
<resource>
|
||||||
|
<directory>res</directory>
|
||||||
|
</resource>
|
||||||
|
</resources>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
@ -232,15 +232,31 @@ public class ExcelKit {
|
|||||||
return readExcel(file, fields, -1, null);
|
return readExcel(file, fields, -1, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//read excel sheet[0] no fields
|
||||||
|
public static List<Map> readExcel(File file) throws IOException {
|
||||||
|
return readExcel(file, null, -1, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
//read excel by sheetName
|
||||||
public static List<Map> readExcel(File file, String[] fields, String sheetName) throws IOException {
|
public static List<Map> readExcel(File file, String[] fields, String sheetName) throws IOException {
|
||||||
return readExcel(file, fields, -1, sheetName);
|
return readExcel(file, fields, -1, sheetName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//read excel by sheetName no fields
|
||||||
|
public static List<Map> readExcel(File file, String sheetName) throws IOException {
|
||||||
|
return readExcel(file, null, -1, sheetName);
|
||||||
|
}
|
||||||
|
|
||||||
//read excel all sheet
|
//read excel all sheet
|
||||||
public static Map<String, List<Map>> readExcelAll(File file, String[] fields) throws IOException {
|
public static Map<String, List<Map>> readExcelAll(File file, String[] fields) throws IOException {
|
||||||
return readExcelAll(file, fields, -1);
|
return readExcelAll(file, fields, -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//read all excel no fields
|
||||||
|
public static Map<String, List<Map>> readExcelAll(File file) throws IOException {
|
||||||
|
return readExcelAll(file, null, -1);
|
||||||
|
}
|
||||||
|
|
||||||
//read excel sheet[0]
|
//read excel sheet[0]
|
||||||
private static List<Map> readExcel(File file, String[] fields, int lastRowNum, String sheetName) throws IOException {
|
private static List<Map> readExcel(File file, String[] fields, int lastRowNum, String sheetName) throws IOException {
|
||||||
Workbook wk;
|
Workbook wk;
|
||||||
@ -291,6 +307,10 @@ public class ExcelKit {
|
|||||||
* @author Lxyer 2016/8/1 10:32.
|
* @author Lxyer 2016/8/1 10:32.
|
||||||
*/
|
*/
|
||||||
private static List<Map> readExcel(Sheet sheet, String[] fields, int lastRowNum) throws OfficeXmlFileException {
|
private static List<Map> readExcel(Sheet sheet, String[] fields, int lastRowNum) throws OfficeXmlFileException {
|
||||||
|
if (fields == null || fields.length == 0){
|
||||||
|
return readExcel(sheet, lastRowNum);
|
||||||
|
}
|
||||||
|
|
||||||
List<Map> list = new ArrayList<>();
|
List<Map> list = new ArrayList<>();
|
||||||
if (lastRowNum < 0 || lastRowNum > sheet.getLastRowNum()){
|
if (lastRowNum < 0 || lastRowNum > sheet.getLastRowNum()){
|
||||||
lastRowNum = sheet.getLastRowNum();
|
lastRowNum = sheet.getLastRowNum();
|
||||||
@ -315,8 +335,7 @@ public class ExcelKit {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
int cellType = cell.getCellType();
|
if (cell.getCellTypeEnum() == CellType.NUMERIC){
|
||||||
if (cellType == 0){
|
|
||||||
map.put(fields[j], (long)cell.getNumericCellValue()+"");
|
map.put(fields[j], (long)cell.getNumericCellValue()+"");
|
||||||
}else {
|
}else {
|
||||||
map.put(fields[j], cell.getStringCellValue());
|
map.put(fields[j], cell.getStringCellValue());
|
||||||
@ -327,6 +346,48 @@ public class ExcelKit {
|
|||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Lxyer 2016/9/21 00:38.
|
||||||
|
*/
|
||||||
|
private static List<Map> readExcel(Sheet sheet, int lastRowNum) throws OfficeXmlFileException {
|
||||||
|
List<Map> list = new ArrayList<>();
|
||||||
|
if (lastRowNum < 0 || lastRowNum > sheet.getLastRowNum()){
|
||||||
|
lastRowNum = sheet.getLastRowNum();
|
||||||
|
}
|
||||||
|
|
||||||
|
int t = 0;
|
||||||
|
r:for (int i=0; i<=lastRowNum; i++){
|
||||||
|
Row row = sheet.getRow(i);
|
||||||
|
if (row == null) continue ;
|
||||||
|
short cellNum = row.getLastCellNum();
|
||||||
|
//空跳过/连续三行为空结束
|
||||||
|
if (isEmptyRow(row, 3)) {
|
||||||
|
if (t++ > 3) break;
|
||||||
|
continue ;
|
||||||
|
}
|
||||||
|
|
||||||
|
Map map = new HashMap();
|
||||||
|
for (int j=0; j < cellNum; j++){
|
||||||
|
String field = (char)((j/26 ==0? ' ' : 'a')+j/26) + "" +(char)('a'+j%26);
|
||||||
|
field = field.replace(" ","");
|
||||||
|
Cell cell = row.getCell(j);
|
||||||
|
if (cell == null){
|
||||||
|
map.put(field, "");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cell.getCellTypeEnum() == CellType.NUMERIC){
|
||||||
|
map.put(field, (long)cell.getNumericCellValue()+"");
|
||||||
|
}else {
|
||||||
|
map.put(field, cell.getStringCellValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
list.add(map);
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
//空跳过/连续三行为空结束
|
//空跳过/连续三行为空结束
|
||||||
private static boolean isEmptyRow(Row 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++) {
|
||||||
|
47
test/com/lxyer/excel/ExcelKitTest.java
Normal file
47
test/com/lxyer/excel/ExcelKitTest.java
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
package com.lxyer.excel;
|
||||||
|
|
||||||
|
import com.lxyer.excel.poi.ExcelKit;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author: liangxianyou at 2018/9/15 7:52.
|
||||||
|
*/
|
||||||
|
public class ExcelKitTest {
|
||||||
|
|
||||||
|
//read excel by fields
|
||||||
|
@Test
|
||||||
|
public void readTest() throws IOException {
|
||||||
|
/*String sql1 = "select a, b, c from Sheet1";
|
||||||
|
String sql2 = "select a, b, c from Sheet1 where a=1 order by b";
|
||||||
|
String sql3 = "select a, b, c from Sheet1 s1, Sheet2 s2 order by";
|
||||||
|
String sql4 = "select a, b, c from Sheet1 s1, Sheet2 s2 where order by";
|
||||||
|
String select = sql1.substring(0,sql1.indexOf("from")).replace("select", "").replace(" ", "");
|
||||||
|
|
||||||
|
System.out.println(sql1.substring(0,sql1.indexOf("from")).replace("select", "").replace(" ", ""));
|
||||||
|
*/
|
||||||
|
|
||||||
|
String[] fields ={"a", "b", "c"};
|
||||||
|
File file = new File("res/test.xlsx");
|
||||||
|
List<Map> list = ExcelKit.readExcel(file, fields);
|
||||||
|
System.out.println(list.size());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//read excel no fields
|
||||||
|
@Test
|
||||||
|
public void readTest2() throws IOException {
|
||||||
|
List<Map> list = ExcelKit.readExcel(new File("res/test.xlsx"));
|
||||||
|
|
||||||
|
list.forEach(x->{
|
||||||
|
x.forEach((k,v)->{
|
||||||
|
System.out.println(String.format("%s:%s", k, v));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user