|
@@ -13,10 +13,7 @@ import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
-import java.io.File;
|
|
|
-import java.io.FileInputStream;
|
|
|
-import java.io.IOException;
|
|
|
-import java.io.InputStream;
|
|
|
+import java.io.*;
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
import java.lang.reflect.Method;
|
|
|
import java.text.SimpleDateFormat;
|
|
@@ -426,37 +423,15 @@ public class POIUtil {
|
|
|
* @return
|
|
|
* @throws IOException
|
|
|
*/
|
|
|
- public static Map<String, List<Map<String, Object>>> importExcel(InputStream inputStream, int startRowNum, String extName) throws IOException {
|
|
|
-
|
|
|
- Map<String, List<Map<String, Object>>> result = new HashMap<String, List<Map<String, Object>>>();
|
|
|
-
|
|
|
- Workbook workbook = null;
|
|
|
-
|
|
|
- try {
|
|
|
- if (extName.endsWith(".xlsx")) {
|
|
|
- // 支持excel2007 xlsx格式
|
|
|
- workbook = new XSSFWorkbook(inputStream);
|
|
|
- } else if (extName.endsWith(".xls")) {
|
|
|
- // 支持excel2003以前 xls格式
|
|
|
- workbook = new HSSFWorkbook(inputStream);
|
|
|
- } else {
|
|
|
- throw new UtilException("excel文件的扩展名是.xls or .xlsx!");
|
|
|
- }
|
|
|
- } catch (Exception ex) {
|
|
|
- LOGGER.error("excel open error.", ex);
|
|
|
- return result;
|
|
|
- } finally {
|
|
|
- if (inputStream != null) {
|
|
|
- inputStream.close();
|
|
|
- }
|
|
|
- }
|
|
|
+ public static Map<String, List<Map<String, Object>>> importExcel(InputStream inputStream, int startRowNum, String extName) throws Exception {
|
|
|
|
|
|
+ Map<String, List<Map<String, Object>>> result = new HashMap<>();
|
|
|
+ Workbook workbook = WorkbookFactory.create(inputStream);
|
|
|
int sheetCount = workbook.getNumberOfSheets();
|
|
|
Sheet sheet = null;
|
|
|
Row row = null;
|
|
|
Cell cell = null;
|
|
|
int currentRowNum = 0, currentCellNum = 0;
|
|
|
- // Object fieldValue = null;
|
|
|
Iterator<Row> rowIter = null;
|
|
|
Iterator<Cell> cellIter = null;
|
|
|
|
|
@@ -468,9 +443,9 @@ public class POIUtil {
|
|
|
sheet = workbook.getSheetAt(i);
|
|
|
|
|
|
Map<String, List<PictureData>> picMap = null;
|
|
|
- if (extName.endsWith(".xlsx")) {
|
|
|
+ if(sheet instanceof XSSFSheet){
|
|
|
picMap = getXlsxPictures((XSSFSheet) sheet);
|
|
|
- } else if (extName.endsWith(".xls")) {
|
|
|
+ }else if(sheet instanceof HSSFSheet){
|
|
|
picMap = getXlsPictures((HSSFSheet) sheet);
|
|
|
}
|
|
|
|
|
@@ -487,7 +462,6 @@ public class POIUtil {
|
|
|
if (currentRowNum == 1) {// 第一列表示英文名称对应表字段
|
|
|
cellIter = row.iterator();
|
|
|
// 列号清零
|
|
|
- currentCellNum = 0;
|
|
|
List<String> names = new ArrayList<String>();
|
|
|
while (cellIter.hasNext()) {
|
|
|
cell = cellIter.next();
|
|
@@ -496,15 +470,12 @@ public class POIUtil {
|
|
|
fieldsName = names.toArray(new String[names.size()]);
|
|
|
continue;
|
|
|
}
|
|
|
-
|
|
|
// 跳过指定的行
|
|
|
if (currentRowNum < startRowNum) {
|
|
|
continue;
|
|
|
}
|
|
|
-
|
|
|
// 实例化对象
|
|
|
- obj = new HashMap<String, Object>();// clazz.newInstance();
|
|
|
-
|
|
|
+ obj = new HashMap<>();
|
|
|
cellIter = row.iterator();
|
|
|
// 列号清零
|
|
|
currentCellNum = 0;
|