|
@@ -0,0 +1,213 @@
|
|
|
+package com.hb.proj.excel.imp;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.text.DecimalFormat;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.apache.poi.ss.usermodel.Cell;
|
|
|
+import org.apache.poi.ss.usermodel.DateUtil;
|
|
|
+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.ss.usermodel.WorkbookFactory;
|
|
|
+
|
|
|
+import com.hb.proj.allconfig.BusinessException;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+public class ExcelDataExtractor {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 只支持单文件上传,需要传入template参数
|
|
|
+ * @param request
|
|
|
+ * @param response
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+
|
|
|
+ /*
|
|
|
+ public static ExcelResult extractExcelData(HttpServletRequest request,MultipartFile file) throws Exception{
|
|
|
+ return extractExcelData(request,file,null);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static ExcelResult extractExcelData(HttpServletRequest request,MultipartFile file,Map<String,String> otherParams) throws Exception{
|
|
|
+
|
|
|
+ request.setCharacterEncoding("UTF-8");
|
|
|
+ DiskFileItemFactory factory = new DiskFileItemFactory();
|
|
|
+ factory.setSizeThreshold(10*1024*1024);
|
|
|
+ ServletFileUpload sfu = new ServletFileUpload(factory);
|
|
|
+ sfu.setSizeMax(10*1024 * 1024); // img size 10m
|
|
|
+ List<FileItem> items =null;
|
|
|
+
|
|
|
+ Map<String,Object> extParams=new HashMap<String,Object>();
|
|
|
+ FileItem uploadFile=sfu.parseRequest(request2);
|
|
|
+ for(FileItem itm : items){
|
|
|
+ if(itm.isFormField()){
|
|
|
+ extParams.put(itm.getFieldName(), itm.getString("UTF-8"));
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ uploadFile=itm;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ if(StringUtils.isEmpty((String)extParams.get("config"))){ //excel配置
|
|
|
+ ExcelResult excelRst=new ExcelResult();
|
|
|
+ excelRst.setException("缺失导入配置,取消操作");
|
|
|
+ return excelRst;
|
|
|
+ }
|
|
|
+
|
|
|
+ ExcelConfig config=JacksonUtils.get((String)extParams.get("config"), ExcelConfig.class);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ ExcelResult er= getDatas(uploadFile, config,request);
|
|
|
+ if(er!=null){
|
|
|
+ er.setReqParams(extParams);
|
|
|
+ }
|
|
|
+ return er;
|
|
|
+ }
|
|
|
+
|
|
|
+ */
|
|
|
+
|
|
|
+ public static ExcelResult getDatas(InputStream is,ExcelConfig config){
|
|
|
+ try {
|
|
|
+ ExcelResult excelRst=new ExcelResult();
|
|
|
+ Workbook wb=WorkbookFactory.create(is);
|
|
|
+ Sheet sheet = wb.getSheetAt(config.getSheet());
|
|
|
+ List<CellConfig> cellConfigs=config.getCells();
|
|
|
+ Object[] rsts=null;
|
|
|
+ if(cellConfigs!=null&&cellConfigs.size()>0){
|
|
|
+ for(CellConfig cc : cellConfigs){
|
|
|
+ rsts=extractCell(sheet,cc);
|
|
|
+
|
|
|
+ if(StringUtils.isNotEmpty((String)rsts[0])){
|
|
|
+ excelRst.addError((String)rsts[0]);
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ excelRst.addOtherVal(cc.getName(), rsts[1]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ cellConfigs=config.getBlock();
|
|
|
+ if(cellConfigs!=null&&cellConfigs.size()>0){
|
|
|
+ extractBlock(sheet,config.getStart(),config.getEnd(),cellConfigs,excelRst);
|
|
|
+ }
|
|
|
+ return excelRst;
|
|
|
+ }
|
|
|
+ catch(Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new BusinessException("解析exel文件出错");
|
|
|
+ }
|
|
|
+ finally {
|
|
|
+ if(is!=null) {
|
|
|
+ try {
|
|
|
+ is.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Cell getCell(Sheet sheet,CellConfig cellConfig){
|
|
|
+ int[] indexs=ExcelTool.getIndex(cellConfig.getId()); //基于excel的索引
|
|
|
+
|
|
|
+ return sheet.getRow(indexs[1]-1).getCell(indexs[0]-1); //从0开始的索引
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Object[] extractCell(Sheet sheet,CellConfig cellConfig){
|
|
|
+ Cell cell=getCell(sheet,cellConfig);
|
|
|
+ String val=getCellStringValue(cell,cellConfig.getFmt());
|
|
|
+ return ExcelImportValidator.validate(val, cellConfig.getId(), cellConfig); //数据验证,通过后进行数据类型转换 //rst[0]:验证结果;rst[1]:转换后的值
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void extractBlock(Sheet sheet,String start,String end,List<CellConfig> headers,ExcelResult excelHolder){
|
|
|
+ int[] startIndexs=ExcelTool.getIndex(start);
|
|
|
+ int[] endIndexs=ExcelTool.getIndex(end);
|
|
|
+ int starRow=startIndexs[1]; //从1开始计算的
|
|
|
+ int endRow=endIndexs[1];
|
|
|
+ Object[] rst=null;
|
|
|
+ Map<String,Object> rowData=null;
|
|
|
+ while(starRow<=endRow&&!isNullRow(sheet.getRow(starRow-1))){
|
|
|
+ rowData=new HashMap<String,Object>();
|
|
|
+ for(CellConfig cc : headers){
|
|
|
+ cc.setId(cc.getId().replaceAll("\\d+", String.valueOf(starRow))); //更新行号(headers中使用的是首行的行号,需要迭代更换为数据行的行号)
|
|
|
+ rst=extractCell(sheet,cc);
|
|
|
+
|
|
|
+ if(StringUtils.isNotEmpty((String)rst[0])){
|
|
|
+ excelHolder.addError((String)rst[0]);
|
|
|
+ rowData.clear();
|
|
|
+ break; //只要有一个字段不符合格式要求就不入库
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ rowData.put(cc.getName(), rst[1]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(rowData.size()>0){
|
|
|
+ rowData.put("xlsRowNum", starRow); //add 2024.8.2
|
|
|
+ excelHolder.addRowVal(rowData);
|
|
|
+
|
|
|
+ }
|
|
|
+ starRow++;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private static String getCellStringValue(Cell cell,String fmt){
|
|
|
+ if(cell==null){
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ String cellValue="";
|
|
|
+ switch (cell.getCellType()) {
|
|
|
+ case NUMERIC:
|
|
|
+ case FORMULA: {
|
|
|
+ if (DateUtil.isCellDateFormatted(cell)) {
|
|
|
+ Date date = cell.getDateCellValue();
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ if(fmt!=null && fmt.length() >0) {
|
|
|
+ sdf.applyPattern(fmt);
|
|
|
+ }
|
|
|
+ cellValue = sdf.format(date);
|
|
|
+ } else {
|
|
|
+ DecimalFormat numdf=new DecimalFormat("#0.######");
|
|
|
+ cellValue=numdf.format(cell.getNumericCellValue());
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case STRING:
|
|
|
+ cellValue = cell.getRichStringCellValue().toString().trim();
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ cellValue="";
|
|
|
+ }
|
|
|
+
|
|
|
+ return cellValue;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private static boolean isNullRow(Row row){
|
|
|
+ if(row==null){
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ String val1=getCellStringValue(row.getCell(0),null);
|
|
|
+ String val2=getCellStringValue(row.getCell(1),null);
|
|
|
+ String val3=getCellStringValue(row.getCell(2),null);
|
|
|
+ if("".equals(val1.trim())&&"".equals(val2.trim())&&"".equals(val3.trim())){
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+}
|