优化商品导出功能,按模板的格式排版

This commit is contained in:
季圣华 2023-04-20 00:49:21 +08:00
parent 645abd795a
commit 18140dea12
7 changed files with 68 additions and 100 deletions

View File

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

View File

@ -1,6 +1,5 @@
package com.jsh.erp.controller; package com.jsh.erp.controller;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.jsh.erp.datasource.entities.MaterialVo4Unit; import com.jsh.erp.datasource.entities.MaterialVo4Unit;
@ -8,25 +7,20 @@ import com.jsh.erp.datasource.entities.Unit;
import com.jsh.erp.service.depot.DepotService; import com.jsh.erp.service.depot.DepotService;
import com.jsh.erp.service.depotItem.DepotItemService; import com.jsh.erp.service.depotItem.DepotItemService;
import com.jsh.erp.service.material.MaterialService; import com.jsh.erp.service.material.MaterialService;
import com.jsh.erp.service.redis.RedisService;
import com.jsh.erp.service.unit.UnitService; import com.jsh.erp.service.unit.UnitService;
import com.jsh.erp.utils.BaseResponseInfo; import com.jsh.erp.utils.BaseResponseInfo;
import com.jsh.erp.utils.ErpInfo; import com.jsh.erp.utils.ErpInfo;
import com.jsh.erp.utils.ExcelUtils;
import com.jsh.erp.utils.StringUtil; import com.jsh.erp.utils.StringUtil;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.InputStream;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;

View File

@ -11,4 +11,6 @@ public interface MaterialInitialStockMapperEx {
int batchInsert(List<MaterialInitialStock> list); int batchInsert(List<MaterialInitialStock> list);
List<MaterialInitialStock> getListExceptZero();
} }

View File

@ -1,6 +1,5 @@
package com.jsh.erp.service.material; package com.jsh.erp.service.material;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
@ -36,7 +35,6 @@ import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.File; import java.io.File;
import java.io.InputStream;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.*; import java.util.*;
@ -79,15 +77,6 @@ public class MaterialService {
@Resource @Resource
private MaterialExtendService materialExtendService; private MaterialExtendService materialExtendService;
@Value("${server.tomcat.basedir}")
private String basedir;
@Value("${template.path}")
private String templatePath;
@Value("${export.mode}")
private Integer exportMode;
public Material getMaterial(long id)throws Exception { public Material getMaterial(long id)throws Exception {
Material result=null; Material result=null;
try{ try{
@ -475,10 +464,6 @@ public class MaterialService {
public void exportExcel(String categoryId, String materialParam, String color, String weight, public void exportExcel(String categoryId, String materialParam, String color, String weight,
String expiryNum, String enabled, String enableSerialNumber, String enableBatchNumber, String expiryNum, String enabled, String enableSerialNumber, String enableBatchNumber,
String remark, String mpList, HttpServletResponse response)throws Exception { String remark, String mpList, HttpServletResponse response)throws Exception {
String[] mpArr = new String[]{};
if(StringUtil.isNotEmpty(mpList)){
mpArr= mpList.split(",");
}
List<Long> idList = new ArrayList<>(); List<Long> idList = new ArrayList<>();
if(StringUtil.isNotEmpty(categoryId)){ if(StringUtil.isNotEmpty(categoryId)){
idList = getListByParentId(Long.parseLong(categoryId)); idList = getListByParentId(Long.parseLong(categoryId));
@ -492,74 +477,61 @@ public class MaterialService {
for(MaterialExtend me: otherDataList) { for(MaterialExtend me: otherDataList) {
otherMaterialMap.put(me.getMaterialId(), me); otherMaterialMap.put(me.getMaterialId(), me);
} }
if(exportMode == 0) { String nameStr = "名称*,规格,型号,颜色,类别,基础重量(kg),保质期(天),基本单位*,副单位,基本条码*,副条码,比例,采购价,零售价,销售价,最低售价,状态*,序列号,批号,备注";
String[] names = {"名称*", "规格", "型号", "颜色", "类别", "基础重量(kg)", "保质期(天)", "基本单位*", "副单位", "基本条码*", List<String> nameList = StringUtil.strToStringList(nameStr);
"副条码", "比例", "采购价", "零售价", "销售价", "最低售价", "状态*", "序列号", "批号", "备注"}; //仓库列表
String title = "商品信息"; List<Depot> depotList = depotService.getAllList();
List<String[]> objects = new ArrayList<>(); if (nameList != null) {
if (null != dataList) { for(Depot depot: depotList) {
for (MaterialVo4Unit m : dataList) { nameList.add(depot.getName());
String[] objs = new String[100];
objs[0] = m.getName();
objs[1] = m.getStandard();
objs[2] = m.getModel();
objs[3] = m.getColor();
objs[4] = m.getCategoryName();
objs[5] = m.getWeight() == null ? "" : m.getWeight().setScale(3, BigDecimal.ROUND_HALF_UP).toString();
objs[6] = m.getExpiryNum() == null ? "" : m.getExpiryNum().toString();
objs[7] = m.getCommodityUnit();
objs[8] = otherMaterialMap.get(m.getId()) == null ? "": otherMaterialMap.get(m.getId()).getCommodityUnit();
objs[9] = m.getmBarCode();
objs[10] = otherMaterialMap.get(m.getId()) == null ? "": otherMaterialMap.get(m.getId()).getBarCode();
objs[11] = m.getRatio() == null ? "" : m.getRatio().toString();
objs[12] = m.getPurchaseDecimal() == null ? "" : m.getPurchaseDecimal().setScale(2, BigDecimal.ROUND_HALF_UP).toString();
objs[13] = m.getCommodityDecimal() == null ? "" : m.getCommodityDecimal().setScale(2, BigDecimal.ROUND_HALF_UP).toString();
objs[14] = m.getWholesaleDecimal() == null ? "" : m.getWholesaleDecimal().setScale(2, BigDecimal.ROUND_HALF_UP).toString();
objs[15] = m.getLowDecimal() == null ? "" : m.getLowDecimal().setScale(2, BigDecimal.ROUND_HALF_UP).toString();
objs[16] = m.getEnabled() ? "1" : "0";
objs[17] = m.getEnableSerialNumber();
objs[18] = m.getEnableBatchNumber();
objs[19] = m.getRemark();
objects.add(objs);
}
} }
File file = ExcelUtils.exportObjectsWithoutTitle(title, "导入时本行内容请勿删除,切记!", names, title, objects);
ExcelUtils.downloadExcel(file, file.getName(), response);
} else {
List<Map<Integer, String>> objects = new ArrayList<>();
if (null != dataList) {
for (MaterialVo4Unit m : dataList) {
Map<Integer, String> map = new HashMap<>();
map.put(0, m.getName());
map.put(1, m.getStandard());
map.put(2, m.getModel());
map.put(3, m.getColor());
map.put(4, m.getCategoryName());
map.put(5, m.getWeight() == null ? "" : m.getWeight().setScale(3, BigDecimal.ROUND_HALF_UP).toString());
map.put(6, m.getExpiryNum() == null ? "" : m.getExpiryNum().toString());
map.put(7, m.getCommodityUnit());
map.put(8, otherMaterialMap.get(m.getId()) == null ? "": otherMaterialMap.get(m.getId()).getCommodityUnit());
map.put(9, m.getmBarCode());
map.put(10, otherMaterialMap.get(m.getId()) == null ? "": otherMaterialMap.get(m.getId()).getBarCode());
map.put(11, m.getRatio() == null ? "" : m.getRatio().toString());
map.put(12, m.getPurchaseDecimal() == null ? "" : m.getPurchaseDecimal().setScale(2, BigDecimal.ROUND_HALF_UP).toString());
map.put(13, m.getCommodityDecimal() == null ? "" : m.getCommodityDecimal().setScale(2, BigDecimal.ROUND_HALF_UP).toString());
map.put(14, m.getWholesaleDecimal() == null ? "" : m.getWholesaleDecimal().setScale(2, BigDecimal.ROUND_HALF_UP).toString());
map.put(15, m.getLowDecimal() == null ? "" : m.getLowDecimal().setScale(2, BigDecimal.ROUND_HALF_UP).toString());
map.put(16, m.getEnabled() ? "1" : "0");
map.put(17, m.getEnableSerialNumber());
map.put(18, m.getEnableBatchNumber());
map.put(19, m.getRemark());
objects.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(objects);
File file = new File(basedir, fileName);
ExcelUtils.downloadExcel(file, fileName, response);
} }
//期初库存缓存
List<MaterialInitialStock> misList = materialInitialStockMapperEx.getListExceptZero();
Map<String, BigDecimal> misMap = new HashMap<>();
if (misList != null) {
for (MaterialInitialStock mis : misList) {
misMap.put(mis.getMaterialId() + "_" + mis.getDepotId(), mis.getNumber());
}
}
String[] names = StringUtil.listToStringArray(nameList);
String title = "商品信息";
List<String[]> objects = new ArrayList<>();
if (null != dataList) {
for (MaterialVo4Unit m : dataList) {
String[] objs = new String[100];
objs[0] = m.getName();
objs[1] = m.getStandard();
objs[2] = m.getModel();
objs[3] = m.getColor();
objs[4] = m.getCategoryName();
objs[5] = m.getWeight() == null ? "" : m.getWeight().setScale(3, BigDecimal.ROUND_HALF_UP).toString();
objs[6] = m.getExpiryNum() == null ? "" : m.getExpiryNum().toString();
objs[7] = m.getCommodityUnit();
objs[8] = otherMaterialMap.get(m.getId()) == null ? "" : otherMaterialMap.get(m.getId()).getCommodityUnit();
objs[9] = m.getmBarCode();
objs[10] = otherMaterialMap.get(m.getId()) == null ? "" : otherMaterialMap.get(m.getId()).getBarCode();
objs[11] = m.getRatio() == null ? "" : m.getRatio().toString();
objs[12] = m.getPurchaseDecimal() == null ? "" : m.getPurchaseDecimal().setScale(2, BigDecimal.ROUND_HALF_UP).toString();
objs[13] = m.getCommodityDecimal() == null ? "" : m.getCommodityDecimal().setScale(2, BigDecimal.ROUND_HALF_UP).toString();
objs[14] = m.getWholesaleDecimal() == null ? "" : m.getWholesaleDecimal().setScale(2, BigDecimal.ROUND_HALF_UP).toString();
objs[15] = m.getLowDecimal() == null ? "" : m.getLowDecimal().setScale(2, BigDecimal.ROUND_HALF_UP).toString();
objs[16] = m.getEnabled() ? "1" : "0";
objs[17] = m.getEnableSerialNumber();
objs[18] = m.getEnableBatchNumber();
objs[19] = m.getRemark();
//仓库期初库存
int i = 20;
for(Depot depot: depotList) {
BigDecimal number = misMap.get(m.getId() + "_" + depot.getId());
objs[i] = number == null ? "0" : number.setScale(2, BigDecimal.ROUND_HALF_UP).toString();
i++;
}
objects.add(objs);
}
}
File file = ExcelUtils.exportObjectsWithoutTitle(title, "导入时本行内容请勿删除,切记!", names, title, objects);
ExcelUtils.downloadExcel(file, file.getName(), response);
} }
@Transactional(value = "transactionManager", rollbackFor = Exception.class) @Transactional(value = "transactionManager", rollbackFor = Exception.class)

View File

@ -49,23 +49,27 @@ public class ExcelUtils {
File excelFile = new File(fileName); File excelFile = new File(fileName);
WritableWorkbook wtwb = Workbook.createWorkbook(excelFile); WritableWorkbook wtwb = Workbook.createWorkbook(excelFile);
WritableSheet sheet = wtwb.createSheet(title, 0); WritableSheet sheet = wtwb.createSheet(title, 0);
sheet.getSettings().setDefaultColumnWidth(20); sheet.getSettings().setDefaultColumnWidth(12);
// 标题的格式 // 标题的格式
WritableFont wfc = new WritableFont(WritableFont.ARIAL, 15, WritableFont wfc = new WritableFont(WritableFont.ARIAL, 12,
WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
jxl.format.Colour.BLACK); jxl.format.Colour.BLACK);
WritableCellFormat wcfFC = new WritableCellFormat(wfc); WritableCellFormat wcfFC = new WritableCellFormat(wfc);
wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE); wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE);
//设置边框
wcfFC.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
// 设置字体以及单元格格式 // 设置字体以及单元格格式
WritableFont wfont = new WritableFont(WritableFont.createFont("楷书"), 15); WritableFont wfont = new WritableFont(WritableFont.createFont("楷书"), 12);
WritableCellFormat format = new WritableCellFormat(wfont); WritableCellFormat format = new WritableCellFormat(wfont);
format.setAlignment(Alignment.LEFT); format.setAlignment(Alignment.LEFT);
format.setVerticalAlignment(VerticalAlignment.TOP); format.setVerticalAlignment(VerticalAlignment.TOP);
// 第一行写入提示 // 第一行写入提示
sheet.addCell(new Label(0, 0, tip, wcfFC)); sheet.addCell(new Label(0, 0, tip, wcfFC));
// 第二行写入标题 // 第二行写入标题
for (int i = 0; i < names.length; i++) { for (int i = 0; i < names.length; i++) {
sheet.addCell(new Label(i, 1, names[i], wcfFC)); sheet.addCell(new Label(i, 1, names[i], wcfFC));

View File

@ -30,8 +30,4 @@ file.path=/opt/jshERP/upload
server.tomcat.basedir=/opt/tmp/tomcat server.tomcat.basedir=/opt/tmp/tomcat
#文件上传限制(byte) #文件上传限制(byte)
spring.servlet.multipart.max-file-size=10485760 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

View File

@ -10,4 +10,9 @@
</foreach > </foreach >
</insert> </insert>
<select id="getListExceptZero" resultType="com.jsh.erp.datasource.entities.MaterialInitialStock">
select * from jsh_material_initial_stock where number!=0
and ifnull(delete_flag,'0') !='1'
</select>
</mapper> </mapper>