diff --git a/jshERP-boot/pom.xml b/jshERP-boot/pom.xml index a28c25b4..dbc3df73 100644 --- a/jshERP-boot/pom.xml +++ b/jshERP-boot/pom.xml @@ -63,6 +63,11 @@ jxl 2.6.12 + + com.alibaba + easyexcel + 3.2.1 + org.projectlombok diff --git a/jshERP-boot/src/main/java/com/jsh/erp/controller/MaterialController.java b/jshERP-boot/src/main/java/com/jsh/erp/controller/MaterialController.java index c1ddde8f..016a3dae 100644 --- a/jshERP-boot/src/main/java/com/jsh/erp/controller/MaterialController.java +++ b/jshERP-boot/src/main/java/com/jsh/erp/controller/MaterialController.java @@ -1,11 +1,10 @@ package com.jsh.erp.controller; +import com.alibaba.excel.EasyExcel; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; -import com.jsh.erp.datasource.entities.MaterialInitialStockWithMaterial; import com.jsh.erp.datasource.entities.MaterialVo4Unit; import com.jsh.erp.datasource.entities.Unit; -import com.jsh.erp.datasource.vo.MaterialVoSearch; import com.jsh.erp.service.depot.DepotService; import com.jsh.erp.service.depotItem.DepotItemService; import com.jsh.erp.service.material.MaterialService; @@ -16,6 +15,7 @@ import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; @@ -23,6 +23,7 @@ import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.File; +import java.io.InputStream; import java.math.BigDecimal; import java.util.ArrayList; import java.util.HashMap; @@ -55,6 +56,15 @@ public class MaterialController { @Resource private RedisService redisService; + @Value("${server.tomcat.basedir}") + private String basedir; + + @Value("${template.path}") + private String templatePath; + + @Value("${export.mode}") + private Integer exportMode; + /** * 检查商品是否存在 * @param id @@ -360,35 +370,47 @@ public class MaterialController { List dataList = materialService.exportExcel(StringUtil.toNull(materialParam), StringUtil.toNull(color), StringUtil.toNull(weight), StringUtil.toNull(expiryNum), StringUtil.toNull(enabled), StringUtil.toNull(enableSerialNumber), StringUtil.toNull(enableBatchNumber), StringUtil.toNull(remark), StringUtil.toNull(categoryId)); - String[] names = {"条码", "名称", "规格", "型号", "颜色", "类别", "扩展信息", "单位", "基础重量", "保质期", "采购价", "零售价", "销售价", "最低售价", "备注", "状态", "序列号", "批号"}; - String title = "商品信息"; - List objects = new ArrayList<>(); - if (null != dataList) { - for (MaterialVo4Unit m : dataList) { - String[] objs = new String[100]; - objs[0] = m.getmBarCode(); - objs[1] = m.getName(); - objs[2] = m.getStandard(); - objs[3] = m.getModel(); - objs[4] = m.getColor(); - objs[5] = m.getCategoryName(); - objs[6] = materialService.getMaterialOtherByParam(mpArr, m); - objs[7] = m.getCommodityUnit(); - objs[8] = m.getWeight() == null? "" : m.getWeight().toString(); - objs[9] = m.getExpiryNum() == null? "" : m.getExpiryNum().toString(); - objs[10] = m.getPurchaseDecimal() == null? "" : m.getPurchaseDecimal().setScale(2,BigDecimal.ROUND_HALF_UP).toString(); - objs[11] = m.getCommodityDecimal() == null? "" : m.getCommodityDecimal().setScale(2,BigDecimal.ROUND_HALF_UP).toString(); - objs[12] = m.getWholesaleDecimal() == null? "" : m.getWholesaleDecimal().setScale(2,BigDecimal.ROUND_HALF_UP).toString(); - objs[13] = m.getLowDecimal() == null? "" : m.getLowDecimal().setScale(2,BigDecimal.ROUND_HALF_UP).toString(); - objs[14] = m.getRemark(); - objs[15] = m.getEnabled() ? "启用" : "禁用"; - objs[16] = "1".equals(m.getEnableSerialNumber()) ? "有" : "无"; - objs[17] = "1".equals(m.getEnableBatchNumber()) ? "有" : "无"; - objects.add(objs); + if(exportMode == 0) { + String[] names = {"条码", "名称", "规格", "型号", "颜色", "类别", "扩展信息", "单位", "基础重量", "保质期", "采购价", "零售价", "销售价", "最低售价", "备注", "状态", "序列号", "批号"}; + String title = "商品信息"; + List objects = new ArrayList<>(); + if (null != dataList) { + for (MaterialVo4Unit m : dataList) { + String[] objs = new String[100]; + objs[0] = m.getmBarCode(); + objs[1] = m.getName(); + objs[2] = m.getStandard(); + objs[3] = m.getModel(); + objs[4] = m.getColor(); + objs[5] = m.getCategoryName(); + objs[6] = materialService.getMaterialOtherByParam(mpArr, m); + objs[7] = m.getCommodityUnit(); + objs[8] = m.getWeight() == null ? "" : m.getWeight().toString(); + objs[9] = m.getExpiryNum() == null ? "" : m.getExpiryNum().toString(); + objs[10] = m.getPurchaseDecimal() == null ? "" : m.getPurchaseDecimal().setScale(2, BigDecimal.ROUND_HALF_UP).toString(); + objs[11] = m.getCommodityDecimal() == null ? "" : m.getCommodityDecimal().setScale(2, BigDecimal.ROUND_HALF_UP).toString(); + objs[12] = m.getWholesaleDecimal() == null ? "" : m.getWholesaleDecimal().setScale(2, BigDecimal.ROUND_HALF_UP).toString(); + objs[13] = m.getLowDecimal() == null ? "" : m.getLowDecimal().setScale(2, BigDecimal.ROUND_HALF_UP).toString(); + objs[14] = m.getRemark(); + objs[15] = m.getEnabled() ? "启用" : "禁用"; + objs[16] = "1".equals(m.getEnableSerialNumber()) ? "有" : "无"; + objs[17] = "1".equals(m.getEnableBatchNumber()) ? "有" : "无"; + objects.add(objs); + } } + File file = ExcelUtils.exportObjectsWithoutTitle(title, names, title, objects); + ExportExecUtil.showExec(file, file.getName(), response); + } else { + List> list = new ArrayList<>(); + Map map = new HashMap<>(); + list.add(map); + String fileName = "商品信息.xls"; + String pathFile = basedir + File.separator + fileName; + InputStream path = ExcelUtils.getPathByFileName(templatePath, "goods_template_export.xls"); + EasyExcel.write(pathFile).withTemplate(path).sheet(0).needHead(Boolean.FALSE).doWrite(list); + File file = new File(basedir, fileName); + ExportExecUtil.showExec(file, fileName, response); } - File file = ExcelUtils.exportObjectsWithoutTitle(title, names, title, objects); - ExportExecUtil.showExec(file, file.getName(), response); } catch (Exception e) { e.printStackTrace(); } diff --git a/jshERP-boot/src/main/java/com/jsh/erp/utils/ExcelUtils.java b/jshERP-boot/src/main/java/com/jsh/erp/utils/ExcelUtils.java index fe2cbf14..913da122 100644 --- a/jshERP-boot/src/main/java/com/jsh/erp/utils/ExcelUtils.java +++ b/jshERP-boot/src/main/java/com/jsh/erp/utils/ExcelUtils.java @@ -1,10 +1,14 @@ package com.jsh.erp.utils; import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.*; import jxl.*; +import lombok.extern.slf4j.Slf4j; import org.springframework.util.StringUtils; import jxl.format.*; import jxl.write.Label; @@ -13,10 +17,25 @@ import jxl.write.WritableFont; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; +@Slf4j public class ExcelUtils { public static WritableFont arial14font = null; + public static InputStream getPathByFileName(String template, String tmpFileName) { + File tmpFile = new File(template, tmpFileName); + InputStream path = null; + //判断文件或文件夹是否存在 + if (tmpFile.exists()) { + try { + path = new FileInputStream(tmpFile); + } catch (FileNotFoundException e) { + log.error("", e); + } + } + return path; + } + public static File exportObjects(String fileName, String[] names, String title, List objects) throws Exception { File excelFile = new File("fileName.xls"); diff --git a/jshERP-boot/src/main/java/com/jsh/erp/utils/ExportExecUtil.java b/jshERP-boot/src/main/java/com/jsh/erp/utils/ExportExecUtil.java index 69aee9e3..2661cb30 100644 --- a/jshERP-boot/src/main/java/com/jsh/erp/utils/ExportExecUtil.java +++ b/jshERP-boot/src/main/java/com/jsh/erp/utils/ExportExecUtil.java @@ -1,10 +1,13 @@ package com.jsh.erp.utils; -import javax.servlet.http.HttpServletResponse; -import java.io.File; -import java.io.FileInputStream; -import java.io.OutputStream; +import lombok.extern.slf4j.Slf4j; +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServletResponse; +import java.io.*; +import java.net.URLEncoder; + +@Slf4j public class ExportExecUtil { public static void showExec(File excelFile,String fileName,HttpServletResponse response) throws Exception{ @@ -25,4 +28,31 @@ public class ExportExecUtil { fis.close(); } + public static void downloadFile(InputStream inputStream, String fileName , HttpServletResponse response) { + try { + response.setContentType("multipart/form-data"); + response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8")); + ServletOutputStream outputStream = response.getOutputStream(); + byte[] buff = new byte[1024]; + int length = 0; + while ((length = inputStream.read(buff)) != -1) { + outputStream.write(buff, 0, length); + } + if (outputStream != null) { + outputStream.flush(); + outputStream.close(); + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + if (inputStream != null) { + try { + inputStream.close(); + } catch (IOException e) { + log.error("关闭资源出错" + e.getMessage()); + e.printStackTrace(); + } + } + } + } } diff --git a/jshERP-boot/src/main/resources/application.properties b/jshERP-boot/src/main/resources/application.properties index c8a57b74..706ea47a 100644 --- a/jshERP-boot/src/main/resources/application.properties +++ b/jshERP-boot/src/main/resources/application.properties @@ -30,4 +30,8 @@ file.path=/opt/jshERP/upload server.tomcat.basedir=/opt/tmp/tomcat #文件上传限制(byte) spring.servlet.multipart.max-file-size=10485760 -spring.servlet.multipart.max-request-size=10485760 \ No newline at end of file +spring.servlet.multipart.max-request-size=10485760 +#模板路径 +template.path=/home/jshERP/jshERP-web/public/doc +#导出方式 0-不带模板(默认) 1-带模板 +export.mode=0 \ No newline at end of file