package com.meritdata.cloud.cosp.bank.util;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Base64;
import java.util.List;
public class DownLoadUtils {
public static String getFileName(String agent, String filename) throws UnsupportedEncodingException {
if (agent.contains("MSIE")) {
filename = URLEncoder.encode(filename, "utf-8");
filename = filename.replace("+", " ");
} else if (agent.contains("Firefox")) {
Base64.Encoder base64Encoder = Base64.getEncoder();
filename = "=?utf-8?B?" + new String(base64Encoder.encode(filename.getBytes("utf-8")),"UTF-8") + "?=";
} else {
filename = URLEncoder.encode(filename, "utf-8");
}
return filename;
}
}
@ApiOperation("导出")
@GetMapping("/export")
public ResultBody export(HttpServletRequest request, HttpServletResponse response) throws Exception {
HSSFWorkbook book = template();
String finalFileName = DownLoadUtils.getFileName(request.getHeader("USER-AGENT"), "产品表.xls");
response.reset();
response.setContentType("application/vnd.ms-excel");
response.addHeader("Content-Disposition", "attachment;filename=" + finalFileName + "");
OutputStream os = response.getOutputStream();
book.write(os);
os.close();
return ResultBody.success("导出成功");
}
public HSSFWorkbook template(){
HSSFWorkbook template = new HSSFWorkbook();
HSSFSheet sheet = template.createSheet();
int count = 0;
HSSFRow row = sheet.createRow(count);
row.createCell(0).setCellValue("id");
row.createCell(1).setCellValue("类型ID");
row.createCell(2).setCellValue("编码");
row.createCell(3).setCellValue("名字");
row.createCell(4).setCellValue("描述");
row.createCell(5).setCellValue("状态");
row.createCell(6).setCellValue("排序号");
row.createCell(7).setCellValue("创建时间");
row.createCell(8).setCellValue("创建人");
count++;
List<CospProduct> areas = cospProductRepository.findAll();
for (CospProduct area : areas) {
row = sheet.createRow(count);
row.createCell(0).setCellValue(area.getId());
row.createCell(1).setCellValue(area.getTypeId());
row.createCell(2).setCellValue(area.getCode());
row.createCell(3).setCellValue(area.getName());
row.createCell(4).setCellValue(area.getDescription());
row.createCell(5).setCellValue(area.getStatus());
row.createCell(6).setCellValue(area.getSortno());
row.createCell(7).setCellValue(area.getCreateTime().toString());
row.createCell(8).setCellValue(area.getCreateUser());
count++;
}
return template;
}