|
@@ -4,6 +4,8 @@ import cn.hutool.core.collection.CollUtil;
|
|
import cn.hutool.core.io.FileUtil;
|
|
import cn.hutool.core.io.FileUtil;
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
import cn.hutool.core.util.StrUtil;
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
|
+import cn.hutool.poi.excel.ExcelUtil;
|
|
|
|
+import cn.hutool.poi.excel.ExcelWriter;
|
|
import com.alibaba.excel.EasyExcel;
|
|
import com.alibaba.excel.EasyExcel;
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
@@ -34,13 +36,12 @@ import org.springframework.stereotype.Service;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import po.OrderDeliveryPO;
|
|
import po.OrderDeliveryPO;
|
|
-import po.StoreProductPO;
|
|
|
|
import tk.mybatis.mapper.entity.Condition;
|
|
import tk.mybatis.mapper.entity.Condition;
|
|
import tk.mybatis.mapper.entity.Example;
|
|
import tk.mybatis.mapper.entity.Example;
|
|
import vo.*;
|
|
import vo.*;
|
|
|
|
|
|
|
|
+import javax.servlet.ServletOutputStream;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
-import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
import java.io.OutputStream;
|
|
import java.io.OutputStream;
|
|
import java.math.BigDecimal;
|
|
import java.math.BigDecimal;
|
|
@@ -243,40 +244,51 @@ public class StoreOrderServiceImpl extends AbstractService<StoreOrder> implement
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public void exportFile(StoreOrderDTO dto, HttpServletResponse response) {
|
|
public void exportFile(StoreOrderDTO dto, HttpServletResponse response) {
|
|
- OutputStream outputStream = null;
|
|
|
|
|
|
+ List<StoreOrderVO> list = orderList(dto);
|
|
|
|
+ List<String> rowHead = CollUtil.newArrayList("编号");
|
|
|
|
+ ExcelWriter writer = ExcelUtil.getWriter();
|
|
try {
|
|
try {
|
|
- response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
|
|
|
- response.setCharacterEncoding("utf-8");
|
|
|
|
- String fileName = URLEncoder.encode("导出订单", "UTF-8").replaceAll("\\+", "%20");
|
|
|
|
- response.setHeader(
|
|
|
|
- "Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
|
|
|
|
-
|
|
|
|
- List<StoreOrderVO> voList = orderList(dto);
|
|
|
|
- List<StoreProductPO> list = voList.stream().map(vo -> {
|
|
|
|
- StoreProductPO po = new StoreProductPO();
|
|
|
|
- BeanUtils.copyProperties(vo, po);
|
|
|
|
- return po;
|
|
|
|
- }).collect(Collectors.toList());
|
|
|
|
- ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
|
|
|
- if (CollectionUtils.isEmpty(list)) {
|
|
|
|
- list.add(new StoreProductPO());
|
|
|
|
|
|
+ writer.writeHeadRow(rowHead);
|
|
|
|
+ List<List<Object>> rows = new LinkedList<>();
|
|
|
|
+ if (CollUtil.isNotEmpty(list)) {
|
|
|
|
+ for (StoreOrderVO vo : list) {
|
|
|
|
+ List<Object> rowA = CollUtil.newArrayList(
|
|
|
|
+ vo.getId()
|
|
|
|
+// vo.getOrderNo(),
|
|
|
|
+// vo.getAmount() == null ? "0" : vo.getAmount().toPlainString(),
|
|
|
|
+// vo.getDiscount() == null ? "0" : vo.getDiscount().toPlainString(),
|
|
|
|
+// vo.getStatus() + "",
|
|
|
|
+// vo.getChannel(),
|
|
|
|
+// vo.getCurrency(),
|
|
|
|
+// vo.getUserName(),
|
|
|
|
+// vo.getUserPhone(),
|
|
|
|
+// vo.getTransTime() == null ? "" : DateUtil.format(vo.getTransTime(), "yyyy-MM-dd HH:mm:ss"),
|
|
|
|
+// vo.getSuccessTime() == null ? "" : DateUtil.format(vo.getSuccessTime(), "yyyy-MM-dd HH:mm:ss"),
|
|
|
|
+// vo.getBank(),
|
|
|
|
+// vo.getBankAccount(),
|
|
|
|
+// vo.getBankAccountName()
|
|
|
|
+ );
|
|
|
|
+ rows.add(rowA);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
- EasyExcel.write(byteArrayOutputStream, StoreProductPO.class).sheet("导出订单").doWrite(list);
|
|
|
|
- // 将数据写入响应输出流
|
|
|
|
- outputStream = response.getOutputStream();
|
|
|
|
- byteArrayOutputStream.writeTo(outputStream);
|
|
|
|
- outputStream.flush();
|
|
|
|
|
|
+ writer.write(rows);
|
|
|
|
+ //设置宽度自适应
|
|
|
|
+ writer.setColumnWidth(-1, 22);
|
|
|
|
+ //response为HttpServletResponse对象
|
|
|
|
+ response.setContentType("application/vnd.ms-excel;charset=utf-8");
|
|
|
|
+ //test.xls是弹出下载对话框的文件名,不能为中文,中文请自行编码
|
|
|
|
+ response.setHeader("Content-Disposition", "attachment;filename=" + new String(("订单导出").getBytes("UTF-8"), "ISO-8859-1") + ".xls");
|
|
|
|
+ ServletOutputStream out = response.getOutputStream();
|
|
|
|
+ //out为OutputStream,需要写出到的目标流
|
|
|
|
+ writer.flush(out);
|
|
|
|
+ log.info("导出结束");
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
- log.error("导出订单失败: ", e);
|
|
|
|
|
|
+ log.error("导出异常", e);
|
|
|
|
+ e.printStackTrace();
|
|
} finally {
|
|
} finally {
|
|
- // 正确关闭流资源
|
|
|
|
- if (outputStream != null) {
|
|
|
|
- try {
|
|
|
|
- outputStream.close();
|
|
|
|
- } catch (IOException e) {
|
|
|
|
- log.error("关闭outputStream失败: ", e);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ // 关闭writer,释放内存
|
|
|
|
+ writer.close();
|
|
|
|
+ log.info("导出结束");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -1351,35 +1363,16 @@ public class StoreOrderServiceImpl extends AbstractService<StoreOrder> implement
|
|
public void exportDelivery(HttpServletResponse response) {
|
|
public void exportDelivery(HttpServletResponse response) {
|
|
OutputStream outputStream = null;
|
|
OutputStream outputStream = null;
|
|
try {
|
|
try {
|
|
|
|
+ outputStream = response.getOutputStream();
|
|
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
|
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
|
response.setCharacterEncoding("utf-8");
|
|
response.setCharacterEncoding("utf-8");
|
|
String fileName = URLEncoder.encode("导出发货模版", "UTF-8").replaceAll("\\+", "%20");
|
|
String fileName = URLEncoder.encode("导出发货模版", "UTF-8").replaceAll("\\+", "%20");
|
|
response.setHeader(
|
|
response.setHeader(
|
|
"Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
|
|
"Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
|
|
-
|
|
|
|
- // 使用字节数组输出流作为中间缓冲
|
|
|
|
- ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
|
|
|
-
|
|
|
|
- // 创建一个空列表,但确保Excel文件包含表头
|
|
|
|
- EasyExcel.write(byteArrayOutputStream, OrderDeliveryPO.class)
|
|
|
|
- .sheet("导出发货模版")
|
|
|
|
- .doWrite(new ArrayList<OrderDeliveryPO>());
|
|
|
|
-
|
|
|
|
- // 将数据写入响应输出流
|
|
|
|
- outputStream = response.getOutputStream();
|
|
|
|
- byteArrayOutputStream.writeTo(outputStream);
|
|
|
|
- outputStream.flush();
|
|
|
|
|
|
+ List<OrderDeliveryPO> list = new ArrayList<>();
|
|
|
|
+ EasyExcel.write(outputStream, OrderDeliveryPO.class).sheet("导出发货模版").doWrite(list);
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
- log.error("导出配送模板失败: ", e);
|
|
|
|
- } finally {
|
|
|
|
- // 正确关闭流资源
|
|
|
|
- if (outputStream != null) {
|
|
|
|
- try {
|
|
|
|
- outputStream.close();
|
|
|
|
- } catch (IOException e) {
|
|
|
|
- log.error("关闭outputStream失败: ", e);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ throw new ServiceException("模板下载失败, 请联系管理员");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|