|
@@ -1,5 +1,6 @@
|
|
|
package cn.com.ty.lift.enterprise.custom.service.impl;
|
|
|
|
|
|
+import cn.com.ty.lift.common.export.ExportUtils;
|
|
|
import cn.com.ty.lift.enterprise.custom.dao.entity.PropertyCompany;
|
|
|
import cn.com.ty.lift.enterprise.custom.dao.entity.model.PropertyCompanyAndPropertyContactReq;
|
|
|
import cn.com.ty.lift.enterprise.custom.dao.entity.model.PropertyCompanyReq;
|
|
@@ -7,8 +8,10 @@ import cn.com.ty.lift.enterprise.custom.dao.mapper.PropertyCompanyMapper;
|
|
|
import cn.com.ty.lift.enterprise.custom.service.PropertyCompanyService;
|
|
|
import cn.com.xwy.boot.web.dto.RestResponse;
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
+import cn.hutool.core.io.IoUtil;
|
|
|
import cn.hutool.poi.excel.BigExcelWriter;
|
|
|
import cn.hutool.poi.excel.ExcelUtil;
|
|
|
+import cn.hutool.poi.excel.ExcelWriter;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
|
@@ -18,10 +21,13 @@ import lombok.AllArgsConstructor;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
+import javax.servlet.ServletOutputStream;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.*;
|
|
|
import java.time.LocalDate;
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
|
|
|
/**
|
|
@@ -92,70 +98,38 @@ public class PropertyCompanyServiceImpl extends ServiceImpl<PropertyCompanyMappe
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public void export(PropertyCompany propertyCompany, HttpServletResponse res) {
|
|
|
- String fileName = "甲方公司_" + LocalDate.now().toString() + ".xlsx";
|
|
|
+ public void export(PropertyCompany propertyCompany, HttpServletResponse response) {
|
|
|
List<PropertyCompany> companies = this.list(new QueryWrapper<PropertyCompany>()
|
|
|
.select("id", "name", "mailing_address", "status")
|
|
|
.eq("mt_company_id", propertyCompany.getMtCompanyId())
|
|
|
.ne("status", 0));
|
|
|
-
|
|
|
List<List<?>> rows = new ArrayList<>();
|
|
|
//设置列头
|
|
|
- rows.add(CollUtil.newArrayList("甲方公司id", "名称", "通讯地址", "状态,1正常 2有关联项目"));
|
|
|
+ rows.add(CollUtil.newArrayList("甲方公司id", "名称", "通讯地址", "状态"));
|
|
|
companies.forEach(c -> {
|
|
|
- List<?> row = CollUtil.newArrayList(c.getId().toString(), c.getName(), c.getMailingAddress(), c.getStatus());
|
|
|
+ List<?> row = CollUtil.newArrayList(
|
|
|
+ c.getId().toString()
|
|
|
+ , c.getName()
|
|
|
+ , null != c.getMailingAddress() ? c.getMailingAddress() : "无"
|
|
|
+ , c.getStatus().equals(1) ? "无关联项目" : "有关联项目");
|
|
|
rows.add(row);
|
|
|
});
|
|
|
- BigExcelWriter writer = ExcelUtil.getBigWriter(System.getProperty("user.dir") + "/" + fileName);
|
|
|
+
|
|
|
+ ExcelWriter writer = ExcelUtil.getWriter(true);
|
|
|
//设置列宽
|
|
|
writer.setColumnWidth(0, 25);
|
|
|
writer.setColumnWidth(1, 35);
|
|
|
writer.setColumnWidth(2, 40);
|
|
|
writer.setColumnWidth(3, 25);
|
|
|
- // 一次性写出内容,使用默认样式
|
|
|
- writer.write(rows);
|
|
|
- // 关闭writer,释放内存
|
|
|
- writer.close();
|
|
|
-
|
|
|
- res.setHeader("Content-Disposition", "attachment; filename=" + fileName);
|
|
|
- res.setHeader("content-type", "application/x-msdownload;");
|
|
|
- res.setContentType("text/plain; charset=utf-8");
|
|
|
- byte[] buff = new byte[1024];
|
|
|
- BufferedInputStream bis = null;
|
|
|
- OutputStream os = null;
|
|
|
|
|
|
- File file = new File(System.getProperty("user.dir") + "/" + fileName);
|
|
|
- try {
|
|
|
- os = res.getOutputStream();
|
|
|
- bis = new BufferedInputStream(new FileInputStream(file));
|
|
|
- int i = bis.read(buff);
|
|
|
-
|
|
|
- while (i != -1) {
|
|
|
- os.write(buff, 0, buff.length);
|
|
|
- os.flush();
|
|
|
- i = bis.read(buff);
|
|
|
- }
|
|
|
+ response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
|
|
|
+ response.setHeader("Content-Disposition", "attachment;filename=" + LocalDate.now().toString() + ".xlsx");
|
|
|
+ try (ServletOutputStream out = response.getOutputStream()) {
|
|
|
+ writer.write(rows);
|
|
|
+ writer.flush(out, true);
|
|
|
+ writer.close();
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
- } finally {
|
|
|
- if (os != null) {
|
|
|
- try {
|
|
|
- os.close();
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
- if (bis != null) {
|
|
|
- try {
|
|
|
- bis.close();
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (file.exists()) {
|
|
|
- file.delete();
|
|
|
}
|
|
|
}
|
|
|
}
|