给商品导出初步增加带模板导出的逻辑

This commit is contained in:
季圣华 2023-04-17 23:40:42 +08:00
parent b9cb19fab6
commit ab2ff3ab41
5 changed files with 114 additions and 34 deletions

View File

@ -63,6 +63,11 @@
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.2.1</version>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>

View File

@ -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<MaterialVo4Unit> 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<String[]> 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<String[]> 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<Map<Integer, String>> list = new ArrayList<>();
Map<Integer, String> 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();
}

View File

@ -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<String[]> objects) throws Exception {
File excelFile = new File("fileName.xls");

View File

@ -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();
}
}
}
}
}

View File

@ -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
spring.servlet.multipart.max-request-size=10485760
#模板路径
template.path=/home/jshERP/jshERP-web/public/doc
#导出方式 0-不带模板(默认) 1-带模板
export.mode=0