Browse Source

添加下载功能

lwx_dev
erdanergou 3 years ago
parent
commit
f3c85ccad8
  1. 59
      src/main/java/com/dreamchaser/depository_manage/controller/DepositoryRecordController.java
  2. 2
      src/main/java/com/dreamchaser/depository_manage/controller/DownLoadFileController.java
  3. 1
      src/main/java/com/dreamchaser/depository_manage/service/ExcelService.java
  4. 18
      src/main/java/com/dreamchaser/depository_manage/service/impl/ExcelServiceImpl.java
  5. BIN
      src/main/resources/static/upload/PrintTemplate.xlsx
  6. 10
      src/main/resources/templates/pages/application/form-step-look_back.html
  7. 10
      target/classes/templates/pages/application/form-step-look_back.html

59
src/main/java/com/dreamchaser/depository_manage/controller/DepositoryRecordController.java

@ -14,7 +14,13 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.*; import java.util.*;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -35,7 +41,7 @@ public class DepositoryRecordController {
private MaterialService materialService; private MaterialService materialService;
@Autowired @Autowired
private CompanyService companyService; private ExcelService excelService;
@Autowired @Autowired
private RedisTemplate<String, String> redisTemplateForHash; private RedisTemplate<String, String> redisTemplateForHash;
@Autowired @Autowired
@ -1554,4 +1560,55 @@ public class DepositoryRecordController {
} }
// 用于下载待打印出库订单
@GetMapping("/downloadOutRecord")
public void downloadOutRecord(@RequestParam Map<String,Object> map, HttpServletRequest request, HttpServletResponse response){
String token = request.getHeader("user-token");
if (token == null) {
token = (String) request.getSession().getAttribute("userToken");
}
UserByPort userToken = AuthenticationTokenPool.getUserToken(token);
if(map.containsKey("id")){
Integer id = ObjectFormatUtil.toInteger(map.get("id"));
List<String> strings = excelService.writeExcelForPrint(id, 4, userToken);
List<File> files = new ArrayList<>();
for (String string : strings) {
File file = new File(string);
files.add(file);
}
ZipMultiFileUtil.zipDownload(response,"出库订单.zip",files);
ZipMultiFileUtil.deleteFile(files);
/*try {
String path = "static/upload/materialTypeImport.xlsx";
String fileName = "materialTypeImport.xlsx";
response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
response.setContentType("content-type:octet-stream");
*//* .getClassLoader() 方法是在静态文件路径添加一个/ *//*
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(path);
OutputStream outputStream = response.getOutputStream();
try {
byte[] buffer = new byte[1024];
int len;
assert inputStream != null;
while ((len = inputStream.read(buffer)) != -1) { *//* 将流中内容写出去 .*//*
outputStream.write(buffer, 0, len);
}
}catch (IOException e){
e.printStackTrace();
}finally {
assert inputStream != null;
inputStream.close();
outputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}*/
}else{
throw new MyException("缺少必要参数");
}
}
} }

2
src/main/java/com/dreamchaser/depository_manage/controller/DownLoadFileController.java

@ -165,4 +165,6 @@ public class DownLoadFileController {
} }

1
src/main/java/com/dreamchaser/depository_manage/service/ExcelService.java

@ -5,6 +5,7 @@ import com.dreamchaser.depository_manage.exception.MyException;
import com.dreamchaser.depository_manage.utils.ResultVo; import com.dreamchaser.depository_manage.utils.ResultVo;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.util.List; import java.util.List;
public interface ExcelService { public interface ExcelService {

18
src/main/java/com/dreamchaser/depository_manage/service/impl/ExcelServiceImpl.java

@ -19,10 +19,13 @@ import com.dreamchaser.depository_manage.utils.*;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.*; import java.util.*;
import java.util.concurrent.Future; import java.util.concurrent.Future;
@ -436,6 +439,7 @@ public class ExcelServiceImpl implements ExcelService {
public List<String> writeExcelForPrint(Integer id, Integer pageSize, UserByPort userToken) { public List<String> writeExcelForPrint(Integer id, Integer pageSize, UserByPort userToken) {
// 生成的表格名称列表 // 生成的表格名称列表
List<String> fileNameList = new ArrayList<>(); List<String> fileNameList = new ArrayList<>();
// 获取要打印的出库主单 // 获取要打印的出库主单
ApplicationOutRecord record = depositoryRecordMapper.findApplicationOutRecordPById(id); ApplicationOutRecord record = depositoryRecordMapper.findApplicationOutRecordPById(id);
// 获取要打印的所有子单 // 获取要打印的所有子单
@ -446,8 +450,9 @@ public class ExcelServiceImpl implements ExcelService {
if (recordMinList.size() > pageSize) { if (recordMinList.size() > pageSize) {
pagesum = (int) Math.ceil(recordMinList.size() / (double) pageSize); pagesum = (int) Math.ceil(recordMinList.size() / (double) pageSize);
} }
// 读取表格模板 // 读取表格模板
String templateFileName = "D://PrintTemplate.xlsx"; String templateFileName = "static/upload/PrintTemplate.xlsx";
// 待打印主数据列表 // 待打印主数据列表
List<ExcelInfoByWrite> data = ListUtils.newArrayList(); List<ExcelInfoByWrite> data = ListUtils.newArrayList();
@ -572,8 +577,15 @@ public class ExcelServiceImpl implements ExcelService {
data.add(excelInfoByWrite); data.add(excelInfoByWrite);
} }
// 生成的表格 // 生成的表格
String fileName = "D://PrintTemplate" + System.currentTimeMillis() + ".xlsx"; String fileName = System.getProperty("java.io.tmpdir") + File.separator+"PrintTemplate" + System.currentTimeMillis() + ".xlsx";
try (ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build()) { ClassPathResource classPathResourceForTemplate = new ClassPathResource(templateFileName);
File fileForTemplate = null;
try {
fileForTemplate = classPathResourceForTemplate.getFile();
} catch (IOException e) {
e.printStackTrace();
}
try (ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(fileForTemplate).build()) {
WriteSheet writeSheet = EasyExcel.writerSheet().build(); WriteSheet writeSheet = EasyExcel.writerSheet().build();
FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build(); FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build();
// 如果有多个list 模板上必须有{前缀.} 这里的前缀就是 data1,然后多个list必须用 FillWrapper包裹 // 如果有多个list 模板上必须有{前缀.} 这里的前缀就是 data1,然后多个list必须用 FillWrapper包裹

BIN
src/main/resources/static/upload/PrintTemplate.xlsx

Binary file not shown.

10
src/main/resources/templates/pages/application/form-step-look_back.html

@ -18,6 +18,7 @@
<div class="layui-card-body" style="padding-top: 0px;"> <div class="layui-card-body" style="padding-top: 0px;">
<div id="stepForm" lay-filter="stepForm" style="margin: 0 auto;"> <div id="stepForm" lay-filter="stepForm" style="margin: 0 auto;">
<div style="margin-top: 30px"> <div style="margin-top: 30px">
<button onclick="downloadExcel()">下载</button>
<table id="demo" class="layui-table" <table id="demo" class="layui-table"
style="margin: 0 auto;max-width: 800px;"> style="margin: 0 auto;max-width: 800px;">
<colgroup> <colgroup>
@ -30,6 +31,7 @@
<td>申请编号</td> <td>申请编号</td>
<td id="applicationId" th:text="${record.getCode()}">123456</td> <td id="applicationId" th:text="${record.getCode()}">123456</td>
</tr> </tr>
<input style="display: none" id="id" th:value="${record.getId()}">
<tr th:each="recordMin,iterStar: ${recordMinList}"> <tr th:each="recordMin,iterStar: ${recordMinList}">
<td>物料名称</td> <td>物料名称</td>
<td> <td>
@ -123,6 +125,9 @@
<script> <script>
function selectThisMinRecord(obj){ function selectThisMinRecord(obj){
}
function downloadExcel(){
} }
layui.use(['form', 'step'], function () { layui.use(['form', 'step'], function () {
var $ = layui.$, var $ = layui.$,
@ -169,6 +174,11 @@
fixed:false, fixed:false,
content: '/form_step_lookByminRecordOut?id='+minId, content: '/form_step_lookByminRecordOut?id='+minId,
}); });
};
downloadExcel = function () {
let id = $("#id").val();
window.open("/depositoryRecord/downloadOutRecord?id="+id,"_self");
} }
}) })

10
target/classes/templates/pages/application/form-step-look_back.html

@ -18,6 +18,7 @@
<div class="layui-card-body" style="padding-top: 0px;"> <div class="layui-card-body" style="padding-top: 0px;">
<div id="stepForm" lay-filter="stepForm" style="margin: 0 auto;"> <div id="stepForm" lay-filter="stepForm" style="margin: 0 auto;">
<div style="margin-top: 30px"> <div style="margin-top: 30px">
<button onclick="downloadExcel()">下载</button>
<table id="demo" class="layui-table" <table id="demo" class="layui-table"
style="margin: 0 auto;max-width: 800px;"> style="margin: 0 auto;max-width: 800px;">
<colgroup> <colgroup>
@ -30,6 +31,7 @@
<td>申请编号</td> <td>申请编号</td>
<td id="applicationId" th:text="${record.getCode()}">123456</td> <td id="applicationId" th:text="${record.getCode()}">123456</td>
</tr> </tr>
<input style="display: none" id="id" th:value="${record.getId()}">
<tr th:each="recordMin,iterStar: ${recordMinList}"> <tr th:each="recordMin,iterStar: ${recordMinList}">
<td>物料名称</td> <td>物料名称</td>
<td> <td>
@ -123,6 +125,9 @@
<script> <script>
function selectThisMinRecord(obj){ function selectThisMinRecord(obj){
}
function downloadExcel(){
} }
layui.use(['form', 'step'], function () { layui.use(['form', 'step'], function () {
var $ = layui.$, var $ = layui.$,
@ -169,6 +174,11 @@
fixed:false, fixed:false,
content: '/form_step_lookByminRecordOut?id='+minId, content: '/form_step_lookByminRecordOut?id='+minId,
}); });
};
downloadExcel = function () {
let id = $("#id").val();
window.open("/depositoryRecord/downloadOutRecord?id="+id,"_self");
} }
}) })

Loading…
Cancel
Save