|
|
@@ -307,6 +307,11 @@ public class ExcelUtil<T>
|
|
|
return list;
|
|
|
}
|
|
|
|
|
|
+ public List<T> importExcel(InputStream is, int titleNum) throws Exception
|
|
|
+ {
|
|
|
+ return importExcel(StringUtils.EMPTY, is, titleNum);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 对excel表单默认第一个索引名转换成list
|
|
|
*
|
|
|
@@ -314,9 +319,85 @@ public class ExcelUtil<T>
|
|
|
* @param titleNum 标题占用行数
|
|
|
* @return 转换后集合
|
|
|
*/
|
|
|
- public List<T> importExcel(InputStream is, int titleNum) throws Exception
|
|
|
- {
|
|
|
- return importExcel(StringUtils.EMPTY, is, titleNum);
|
|
|
+ public List<Map<Integer,String >> importMapExcel(InputStream is, int titleNum) throws Exception {
|
|
|
+ List<Map<Integer,String >> list =null;
|
|
|
+ try {
|
|
|
+ list =importMapExcel(StringUtils.EMPTY, is, titleNum);
|
|
|
+ } catch (Exception e)
|
|
|
+ {
|
|
|
+ log.error("导入Excel异常{}", e.getMessage());
|
|
|
+ throw new UtilException(e.getMessage());
|
|
|
+ }
|
|
|
+ finally
|
|
|
+ {
|
|
|
+ IOUtils.closeQuietly(is);
|
|
|
+ }
|
|
|
+ return list;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public List<Map<Integer,String >> importMapExcel(String sheetName, InputStream is, int titleNum) throws Exception{
|
|
|
+ this.type = Type.IMPORT;
|
|
|
+ this.wb = WorkbookFactory.create(is);
|
|
|
+ List<Map<Integer,String >> list = new ArrayList<>();
|
|
|
+ // 如果指定sheet名,则取指定sheet中的内容 否则默认指向第1个sheet
|
|
|
+ Sheet sheet = StringUtils.isNotEmpty(sheetName) ? wb.getSheet(sheetName) : wb.getSheetAt(0);
|
|
|
+ if (sheet == null)
|
|
|
+ {
|
|
|
+ throw new IOException("文件sheet不存在");
|
|
|
+ }
|
|
|
+ boolean isXSSFWorkbook = !(wb instanceof HSSFWorkbook);
|
|
|
+ Map<String, PictureData> pictures;
|
|
|
+ if (isXSSFWorkbook)
|
|
|
+ {
|
|
|
+ pictures = getSheetPictures07((XSSFSheet) sheet, (XSSFWorkbook) wb);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ pictures = getSheetPictures03((HSSFSheet) sheet, (HSSFWorkbook) wb);
|
|
|
+ }
|
|
|
+ // 获取最后一个非空行的行下标,比如总行数为n,则返回的为n-1
|
|
|
+ int rows = sheet.getLastRowNum();
|
|
|
+ if (rows > 0)
|
|
|
+ {
|
|
|
+ // 定义一个map用于存放excel列的序号和field.
|
|
|
+ Map<Integer,String > cellMap = new HashMap<>();
|
|
|
+ // 获取表头
|
|
|
+ Row heard = sheet.getRow(titleNum);
|
|
|
+ for (int i = 0; i < heard.getPhysicalNumberOfCells(); i++)
|
|
|
+ {
|
|
|
+ Cell cell = heard.getCell(i);
|
|
|
+ if (StringUtils.isNotNull(cell))
|
|
|
+ {
|
|
|
+ String value = this.getCellValue(heard, i).toString();
|
|
|
+ cellMap.put( i,value);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ cellMap.put( i,null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ list.add(cellMap);
|
|
|
+ for (int i = titleNum + 1; i <= rows; i++){
|
|
|
+ Map<Integer,String> map = new HashMap<>();
|
|
|
+ // 从第2行开始取数据,默认第一行是表头.
|
|
|
+ Row row = sheet.getRow(i);
|
|
|
+ // 判断当前行是否是空行
|
|
|
+ if (isRowEmpty(row)){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ for (int x=0; x<=row.getLastCellNum(); x++){
|
|
|
+ String val=null;
|
|
|
+ if (row.getCell(x) !=null){
|
|
|
+ val= row.getCell(x).getStringCellValue();
|
|
|
+ }
|
|
|
+ map.put( x,val);
|
|
|
+ }
|
|
|
+ list.add(map);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return list;
|
|
|
}
|
|
|
|
|
|
/**
|