zouxuan 4 年之前
父節點
當前提交
9fe5838aaa

+ 1 - 1
mec-biz/src/main/java/com/ym/mec/biz/service/FinancialExpenditureService.java

@@ -17,5 +17,5 @@ public interface FinancialExpenditureService extends BaseService<Long, Financial
      * @param file:
      * @return java.lang.Object
      */
-    List<FinancialExpenditure> importFinancialExpenditure(MultipartFile file) throws IOException;
+    List<FinancialExpenditure> importFinancialExpenditure(MultipartFile file) throws Exception;
 }

+ 5 - 5
mec-biz/src/main/java/com/ym/mec/biz/service/impl/FinancialExpenditureServiceImpl.java

@@ -4,6 +4,7 @@ package com.ym.mec.biz.service.impl;
 import com.alibaba.fastjson.JSONObject;
 import com.ym.mec.biz.dal.dao.FinancialExpenditureDao;
 import com.ym.mec.biz.dal.entity.FinancialExpenditure;
+import com.ym.mec.biz.dal.enums.TemplateTypeEnum;
 import com.ym.mec.biz.service.FinancialExpenditureService;
 import com.ym.mec.common.dal.BaseDAO;
 import com.ym.mec.common.exception.BizException;
@@ -19,7 +20,7 @@ import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.multipart.MultipartFile;
 
-import java.io.IOException;
+import java.io.ByteArrayInputStream;
 import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.List;
@@ -40,11 +41,10 @@ public class FinancialExpenditureServiceImpl extends BaseServiceImpl<Long, Finan
 
 	@Override
 	@Transactional(rollbackFor = Exception.class)
-	public List<FinancialExpenditure> importFinancialExpenditure(MultipartFile file) throws IOException {
-		Map<String, List<Map<String, Object>>> sheetsListMap = POIUtil.importExcel(file.getInputStream(), 2, file.getOriginalFilename());
-		String fileName = file.getOriginalFilename().split("\\.")[0];
+	public List<FinancialExpenditure> importFinancialExpenditure(MultipartFile file) throws Exception {
+		Map<String, List<Map<String, Object>>> sheetsListMap = POIUtil.importExcel(new ByteArrayInputStream(file.getBytes()), 2, file.getOriginalFilename());
 		InputStream inputStream = new ClassPathResource("columnMapper.ini").getInputStream();
-		Map<String,String> columns = IniFileUtil.readIniFile(inputStream,fileName);
+		Map<String,String> columns = IniFileUtil.readIniFile(inputStream,TemplateTypeEnum.FINANCIAL_EXPENDITURE.getMsg());
 		List<FinancialExpenditure> financialExpenditures = new ArrayList<>();
 		Map<String, Integer> organMap = getMap("organization", "name_", "id_", true, String.class, Integer.class);
 		Map<String, Integer> cooperationOrganMap = getMap("cooperation_organ", "name_", "id_", true, String.class, Integer.class);

+ 3 - 3
mec-biz/src/main/java/com/ym/mec/biz/service/impl/GoodsServiceImpl.java

@@ -5,6 +5,7 @@ import com.ym.mec.biz.dal.dao.GoodsCategoryDao;
 import com.ym.mec.biz.dal.dao.GoodsDao;
 import com.ym.mec.biz.dal.entity.Goods;
 import com.ym.mec.biz.dal.enums.GoodsType;
+import com.ym.mec.biz.dal.enums.TemplateTypeEnum;
 import com.ym.mec.biz.service.GoodsService;
 import com.ym.mec.biz.service.UploadFileService;
 import com.ym.mec.common.dal.BaseDAO;
@@ -67,10 +68,9 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 	@Override
 	@Transactional(rollbackFor = Exception.class)
 	public List<Goods> importGoods(MultipartFile file) throws Exception {
-		Map<String, List<Map<String, Object>>> sheetsListMap = POIUtil.importExcel(file.getInputStream(), 2, file.getOriginalFilename());
+		Map<String, List<Map<String, Object>>> sheetsListMap = POIUtil.importExcel(new ByteArrayInputStream(file.getBytes()), 2, file.getOriginalFilename());
 		InputStream inputStream = new ClassPathResource("columnMapper.ini").getInputStream();
-		String fileName = file.getOriginalFilename().split("\\.")[0];
-		Map<String,String> columns = IniFileUtil.readIniFile(inputStream,fileName);
+		Map<String,String> columns = IniFileUtil.readIniFile(inputStream, TemplateTypeEnum.GOODS.getMsg());
 		List<Goods> goodsList = new ArrayList<>();
 //		Map<String, Integer> map = MapUtil.convertIntegerMap(goodsCategoryDao.queryCategotyMap());
 		Map<String, Integer> map = getMap("goods_category","name_","id_",true,String.class,Integer.class);

+ 7 - 36
mec-util/src/main/java/com/ym/mec/util/excel/POIUtil.java

@@ -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;

+ 4 - 14
mec-web/src/main/java/com/ym/mec/web/controller/ImportController.java

@@ -2,6 +2,7 @@ package com.ym.mec.web.controller;
 
 import com.ym.mec.biz.dal.entity.FinancialExpenditure;
 import com.ym.mec.biz.dal.entity.Goods;
+import com.ym.mec.biz.dal.enums.TemplateTypeEnum;
 import com.ym.mec.biz.service.FinancialExpenditureService;
 import com.ym.mec.biz.service.GoodsService;
 import com.ym.mec.common.controller.BaseController;
@@ -50,23 +51,12 @@ public class ImportController extends BaseController {
     @ApiOperation(value = "下载导入模板")
     @GetMapping(value = "downloadTemplate")
     @PreAuthorize("@pcs.hasPermissions('import/downloadTemplate')")
-    public void getGoodsTemplate(HttpServletResponse response,String templateType) throws IOException {
-        String templateName = null;
-        switch (templateType){
-            case "goods":
-                templateName = "商品导入模板";
-                break;
-            case "financialExpenditure":
-                templateName = "财务支出导入模板";
-                break;
-            default:
-                templateName = "商品导入模板";
-        }
-        InputStream inputStream = new ClassPathResource("excelTemplate/" + templateName + ".xls").getInputStream();
+    public void getGoodsTemplate(HttpServletResponse response, TemplateTypeEnum templateType) throws IOException {
+        InputStream inputStream = new ClassPathResource("excelTemplate/" + templateType.getMsg() + ".xls").getInputStream();
         OutputStream outputStream = response.getOutputStream();
         try {
             response.setContentType("application/octet-stream");
-            response.setHeader("Content-Disposition", "attachment;filename=" + templateName + "-" + DateUtil.getDate(new Date()) + ".xls");
+            response.setHeader("Content-Disposition", "attachment;filename=" + templateType.getMsg() + "-" + DateUtil.getDate(new Date()) + ".xls");
             response.flushBuffer();
             outputStream = response.getOutputStream();
             IOUtils.copy(inputStream, outputStream);