This commit is contained in:
linfso 2024-07-23 08:30:45 +08:00
parent c53e3179cb
commit 2de12b6f9f
4 changed files with 200 additions and 30 deletions

View File

@ -12,16 +12,23 @@
package com.xdadan.erp.web.emis;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.MapUtils;
import com.xdadan.erp.emis.domain.EmisSettleSubBill;
import com.xdadan.erp.emis.domain.EmisSettleSubBillFeeitem;
import com.xdadan.erp.emis.domain.exception.EmisBizError;
import com.xdadan.erp.emis.domain.vo.scan.ScanResult;
import com.xdadan.erp.emis.service.EmisBaseService;
import com.xdadan.erp.emis.service.billexport.ExcelUtils;
import com.xdadan.erp.emis.service.billexport.MapObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
@ -197,15 +204,32 @@ public class EmisSettleBillController extends BaseController
public void exportBill(HttpServletResponse response, EmisSettleBill emisSettleBill)
{
EmisSettleBill bill=emisBaseService.getSettleBillBySettleBillNo(emisSettleBill.getSettleBillNo());
List<EmisSettleSubBill> list = emisBaseService.selectSubBillListBySettleBillNo(emisSettleBill.getSettleBillNo());
ExcelUtil<EmisSettleSubBill> util = new ExcelUtil<EmisSettleSubBill>(EmisSettleSubBill.class);
// util.exportExcel(response, list, "账单明细数据");
// bill.getSettleBillName()+".xls",
try {
ExcelUtils.exportToTemplate(response, "tpl/settle_bill_tpl.xls", bill.getSettleBillName()+".xls", null,
null, null,
null);
EmisSettleBill bill=emisBaseService.getSettleBillBySettleBillNo(emisSettleBill.getSettleBillNo());
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
String fileOutFileName = URLEncoder.encode(bill.getSettleBillName(), "UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + fileOutFileName + ".xls");
List<EmisSettleSubBill> list = emisBaseService.selectSubBillListBySettleBillNo(emisSettleBill.getSettleBillNo());
List<Map<String, Object>> listMap=MapObjectUtil.objListToMapList(list);
InputStream fis = new ClassPathResource("tpl/bill_tpl.xls").getInputStream();
Map<String, Object> map = MapUtils.newHashMap();
map.put("name", "张三");
map.put("number", 5.2);
EasyExcel.write(response.getOutputStream()).withTemplate(fis).sheet().doFill(map);
// ExcelUtils.exportToTemplate(response.getOutputStream(), fis, null,
// null, null,
// null);
} catch (IOException e) {
e.printStackTrace();
}
@ -213,6 +237,8 @@ public class EmisSettleBillController extends BaseController
}
/**
* 修改结算总账单
*/
@ -234,4 +260,19 @@ public class EmisSettleBillController extends BaseController
{
return toAjax(emisSettleBillService.deleteEmisSettleBillByIds(ids));
}
public static void main(String[] args) {
// List<Map<String, Object>> listMap=MapObjectUtil.objListToMapList(list);
// ExcelUtil<EmisSettleSubBill> util = new ExcelUtil<EmisSettleSubBill>(EmisSettleSubBill.class);
// util.exportExcel(response, list, "账单明细数据");
try {
// ExcelUtils.exportToTemplate(response, "tpl/settle_bill_tpl.xls", bill.getSettleBillName()+".xls", null,
// null, null,
// null);
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@ -2,6 +2,7 @@ package com.xdadan.erp.emis.service.billexport;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.util.MapUtils;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.alibaba.excel.write.metadata.fill.FillWrapper;
@ -9,8 +10,7 @@ import org.apache.commons.lang3.StringUtils;
import org.springframework.core.io.ClassPathResource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.time.ZoneId;
@ -23,27 +23,37 @@ import java.util.*;
*/
public class ExcelUtils {
/**
* 导出数据到指定Excel
*
* @param response HttpServletResponse对象
* @param excelPath Excel模板地址
* @param excelFileName 文件名称
* @param outerMap 头部内容map
* @param innerMapList 表格内容list
* @param excelFillCellMergePrevColUtils 列合并参数
* @param excelFillCellMergeStrategyUtils 行合并参数
* @throws IOException 异常错误
*/
public static void exportToTemplate(HttpServletResponse response, String excelPath, String excelFileName, Map<String, Object> outerMap, List<Map<String, Object>> innerMapList,
public static void exportTplFile(HttpServletResponse response, String excelPath, String fileName, Map<String, Object> outerMap, List<Map<String, Object>> innerMapList,
ExcelFillCellMergePrevColUtils excelFillCellMergePrevColUtils, ExcelFillCellMergeStrategyUtils excelFillCellMergeStrategyUtils) throws IOException {
OutputStream fos=response.getOutputStream();
InputStream fis = new ClassPathResource(excelPath).getInputStream();
exportToTemplate(fos, fis, outerMap, innerMapList,
excelFillCellMergePrevColUtils, excelFillCellMergeStrategyUtils);
}
public static void exportTplFileLocal(String outFile, String tplFile, Map<String, Object> outerMap, List<Map<String, Object>> innerMapList,
ExcelFillCellMergePrevColUtils excelFillCellMergePrevColUtils, ExcelFillCellMergeStrategyUtils excelFillCellMergeStrategyUtils) throws IOException {
FileOutputStream fos=new FileOutputStream(outFile);
InputStream fis = new FileInputStream(tplFile);
exportToTemplate(fos, fis,outerMap, innerMapList,
excelFillCellMergePrevColUtils, excelFillCellMergeStrategyUtils);
}
public static void exportToTemplate(OutputStream fos, InputStream fis, Map<String, Object> outerMap, List<Map<String, Object>> innerMapList,
ExcelFillCellMergePrevColUtils excelFillCellMergePrevColUtils, ExcelFillCellMergeStrategyUtils excelFillCellMergeStrategyUtils) throws IOException {
InputStream inputStream = new ClassPathResource(excelPath).getInputStream();
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
String fileName = URLEncoder.encode(excelFileName, "UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream())
.withTemplate(inputStream)
// InputStream inputStream = new ClassPathResource(excelPath).getInputStream();
// response.setContentType("application/vnd.ms-excel");
// response.setCharacterEncoding("utf-8");
// String fileOutFileName = URLEncoder.encode(fileName, "UTF-8");
// response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
ExcelWriter excelWriter = EasyExcel.write(fos)
.withTemplate(fis)
.registerWriteHandler(excelFillCellMergePrevColUtils)
.registerWriteHandler(excelFillCellMergeStrategyUtils)
.build();
@ -152,6 +162,28 @@ public class ExcelUtils {
}
public static void main(String[] args) {
// ExcelUtils.exportToTemplate(response, "excel/某某管理导入模板.xlsx", "某某管理导入模板", null, null, null, null);
String outFile="";
try {
String templateFileName = "/Users/alan/Downloads/m1.xls";
String fileName = "/Users/alan/Downloads/1.xls";
// 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
// 这里要注意 withTemplate 的模板文件会全量存储在内存里面,所以尽量不要用于追加文件,如果文件模板文件过大会OOM
// 如果要再文件中追加(无法在一个线程里面处理,可以在一个线程的建议参照多次写入的demo) 建议临时存储到数据库 或者 磁盘缓存(ehcache) 然后再一次性写入
// EasyExcel.write(fileName, Map.class).withTemplate(templateFileName).sheet().doWrite(data());
Map<String, Object> map = MapUtils.newHashMap();
map.put("name", "张三");
map.put("number", 5.2);
EasyExcel.write(fileName).withTemplate(templateFileName).sheet().doFill(map);
// ExcelUtils.exportTplFileLocal("/Users/alan/Downloads/1.xls", "/Users/alan/Downloads/settle_bill_tpl.xls", null, null, null,null);
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,97 @@
package com.xdadan.erp.emis.service.billexport;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.beanutils.BeanMap;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @Description
* @Author susu
* @Date 2024/2/19
*/
public class MapObjectUtil {
/**
* @description: 将object的list数据 转换成 map的list(如:List<Map<String, Object>>)
* @param objDataList
* @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
* @author susu
*/
public static List<Map<String, Object>> objListToMapList(List<?> objDataList){
List<Map<String, Object>> dataList = new ArrayList<>();
if (objDataList==null || objDataList.size()<1){
return null;
}
objDataList.forEach(obj->{
try {
Map<String, Object> map = MapObjectUtil.objectToMap(obj);
dataList.add(map);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
});
return dataList;
}
/**
* @description: 将object数据转换成map数据
* @param obj
* @return java.util.Map<java.lang.String,java.lang.Object>
* @author susu
*/
public static Map<String, Object> objectToMap(Object obj) throws IllegalAccessException {
Map<String, Object> map = new HashMap();
Class<?> cla = obj.getClass();
Field[] fields = cla.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
String keyName = field.getName();
Object value = field.get(obj);
if (value == null)
value = "";
map.put(keyName, value);
}
return map;
}
/**
* @description: 使用 JSONObject 将object转换成map
* @param obj
* @return java.util.Map<?,?>
* @author susu
*/
public static Map<?, ?> objectToMap2(Object obj) {
if (obj == null)
return null;
return JSONObject.parseObject(JSONObject.toJSONString(obj),Map.class);
}
/**
* @description: 使用BeanMap将object转换成map
* @param obj
* @return java.util.Map<?,?>
* @author susu
*/
public static Map<?, ?> objectToMap3(Object obj) {
if (obj == null)
return null;
return new BeanMap(obj);
}
public static void main(String[] args) {
// DogEntity2 dog = new DogEntity2();
// dog.setDogId("A-1001");
// dog.setDogAge(3);
// dog.setDogName("aaa");
//// Map map = JSONObject.parseObject(JSONObject.toJSONString(dog),Map.class);
// Map map = objectToMap2(dog);
// System.out.println(map);
}
}