|
|
@ -1,10 +1,10 @@
|
|
|
|
package com.ruoyi.jjh.declaration.service.impl;
|
|
|
|
package com.ruoyi.jjh.declaration.service.impl;
|
|
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
|
import com.alibaba.excel.EasyExcel;
|
|
|
|
|
|
|
|
import com.alibaba.fastjson2.JSONArray;
|
|
|
|
|
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
import com.ruoyi.common.exception.ServiceException;
|
|
|
|
|
|
|
|
import com.ruoyi.common.utils.file.FileUtils;
|
|
|
|
|
|
|
|
import com.ruoyi.jjh.declaration.entity.BmsTemplateInfo;
|
|
|
|
import com.ruoyi.jjh.declaration.entity.BmsTemplateInfo;
|
|
|
|
import com.ruoyi.jjh.declaration.mapper.BmsTemplateInfoMapper;
|
|
|
|
import com.ruoyi.jjh.declaration.mapper.BmsTemplateInfoMapper;
|
|
|
|
import com.ruoyi.jjh.declaration.service.IBmsEnterpriseDirectoryService;
|
|
|
|
import com.ruoyi.jjh.declaration.service.IBmsEnterpriseDirectoryService;
|
|
|
@ -12,12 +12,12 @@ import com.ruoyi.jjh.declaration.service.IBmsTemplateInfoService;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import javax.servlet.ServletOutputStream;
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
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.InputStream;
|
|
|
|
|
|
|
|
import java.time.LocalDate;
|
|
|
|
import java.time.LocalDate;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -57,61 +57,39 @@ public class BmsTemplateInfoServiceImpl extends ServiceImpl<BmsTemplateInfoMappe
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 导出模板表单
|
|
|
|
* 导出模板表单
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param request
|
|
|
|
|
|
|
|
* @param response
|
|
|
|
|
|
|
|
* @param id
|
|
|
|
* @param id
|
|
|
|
* @return
|
|
|
|
* @return
|
|
|
|
* @author emiya.xie
|
|
|
|
* @author emiya.xie
|
|
|
|
* @create 2023/8/31 10:25
|
|
|
|
* @create 2023/8/31 10:25
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void exportTemplate(HttpServletRequest request, HttpServletResponse response, Long id) {
|
|
|
|
public void exportTemplate(HttpServletResponse response, Long id) {
|
|
|
|
// BmsTemplateInfo bmsTemplateInfo = baseMapper.selectById(id);
|
|
|
|
|
|
|
|
// if (BeanUtil.isEmpty(bmsTemplateInfo)) {
|
|
|
|
|
|
|
|
// throw new ServiceException("没有找到合适的模板!");
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// String fileName = bmsTemplateInfo.getTemplateName() + ".xlsx";
|
|
|
|
|
|
|
|
// String file = "/template/" + fileName;
|
|
|
|
|
|
|
|
// InputStream is = this.getClass().getResourceAsStream(file);
|
|
|
|
|
|
|
|
// byte[] fileData = new byte[0];
|
|
|
|
|
|
|
|
// try {
|
|
|
|
|
|
|
|
// fileData = input2byte(is);
|
|
|
|
|
|
|
|
// } catch (IOException e) {
|
|
|
|
|
|
|
|
// e.printStackTrace();
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// FileUtils.downloadFile(response, request, fileName, fileData);
|
|
|
|
|
|
|
|
BmsTemplateInfo bmsTemplateInfo = baseMapper.selectById(id);
|
|
|
|
BmsTemplateInfo bmsTemplateInfo = baseMapper.selectById(id);
|
|
|
|
if (BeanUtil.isEmpty(bmsTemplateInfo)) {
|
|
|
|
JSONArray js1 = JSONObject.parse(bmsTemplateInfo.getTemplateJson()).getJSONArray("fields");
|
|
|
|
throw new ServiceException("没有找到合适的模板!");
|
|
|
|
List<List<String>> list = new ArrayList<>();
|
|
|
|
|
|
|
|
for (Object o : js1) {
|
|
|
|
|
|
|
|
JSONObject parse = JSONObject.parse(o.toString());
|
|
|
|
|
|
|
|
List<String> head0 = new ArrayList<>();
|
|
|
|
|
|
|
|
head0.add(parse.get("label").toString());
|
|
|
|
|
|
|
|
list.add(head0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
String fileName = bmsTemplateInfo.getTemplateName() + ".xlsx";
|
|
|
|
String fileName = bmsTemplateInfo.getTemplateName() + ".xlsx";
|
|
|
|
String file = "/template/" + fileName;
|
|
|
|
// 创建 Excel 写入流
|
|
|
|
|
|
|
|
// 设置响应头,告诉浏览器下载文件
|
|
|
|
// 使用 try-with-resources 自动关闭 InputStream
|
|
|
|
// 设置响应头,告诉浏览器下载文件
|
|
|
|
try (InputStream is = this.getClass().getResourceAsStream(file)) {
|
|
|
|
response.setContentType("application/vnd.ms-excel");
|
|
|
|
if (is == null) {
|
|
|
|
response.setHeader("Content-Disposition", "attachment; filename="+fileName);
|
|
|
|
throw new ServiceException("模板文件未找到!");
|
|
|
|
try (ServletOutputStream outputStream = response.getOutputStream()) {
|
|
|
|
}
|
|
|
|
// 使用 EasyExcel 写数据到响应流
|
|
|
|
|
|
|
|
EasyExcel.write(outputStream)
|
|
|
|
byte[] fileData = input2byte(is);
|
|
|
|
// 这里放入动态头
|
|
|
|
FileUtils.downloadFile(response, request, fileName, fileData);
|
|
|
|
.head(list).sheet("模板")
|
|
|
|
|
|
|
|
// 当然这里数据也可以用 List<List<String>> 去传入
|
|
|
|
|
|
|
|
.doWrite(new ArrayList<>());
|
|
|
|
} catch (IOException e) {
|
|
|
|
} catch (IOException e) {
|
|
|
|
log.error("导出模板时发生错误", e);
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
throw new ServiceException("导出模板失败,请重试!");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private byte[] input2byte(InputStream inStream)
|
|
|
|
|
|
|
|
throws IOException {
|
|
|
|
|
|
|
|
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
|
|
|
|
|
|
|
|
byte[] buff = new byte[100];
|
|
|
|
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
while ((rc = inStream.read(buff, 0, 100)) > 0) {
|
|
|
|
|
|
|
|
swapStream.write(buff, 0, rc);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
byte[] in2b = swapStream.toByteArray();
|
|
|
|
|
|
|
|
return in2b;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|