Commit 9264d8d7 by yangxiujun

fix:优化模版下载

1 parent c904e08e
...@@ -12,6 +12,9 @@ import com.dituhui.pea.order.service.impl.OrderCreateServiceImpl; ...@@ -12,6 +12,9 @@ import com.dituhui.pea.order.service.impl.OrderCreateServiceImpl;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import com.alibaba.excel.EasyExcel; import com.alibaba.excel.EasyExcel;
...@@ -112,20 +115,25 @@ public class FileController { ...@@ -112,20 +115,25 @@ public class FileController {
* @param response * @param response
*/ */
@GetMapping(value = "/file/downOrder") @GetMapping(value = "/file/downOrder")
public void downOrderTemplate(HttpServletResponse response) { public ResponseEntity<byte[]> downOrderTemplate() {
try { try {
ArrayList arrayList = new ArrayList<OrderInfoExcelDTO>(); ArrayList<OrderInfoExcelDTO> arrayList = new ArrayList<>();
response.setContentType("application/vnd.ms-excel"); // 假设您有方法获取需要导出的数据,然后添加到 arrayList 中
response.setCharacterEncoding("utf-8");
HttpHeaders headers = new HttpHeaders();
String fileName = URLEncoder.encode("ORDER-" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()), "UTF-8").replaceAll("\\+", "%20"); String fileName = URLEncoder.encode("ORDER-" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()), "UTF-8").replaceAll("\\+", "%20");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); headers.setContentDispositionFormData("attachment", fileName + ".xlsx");
EasyExcel.write(response.getOutputStream(), OrderInfoExcelDTO.class) headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
EasyExcel.write(outputStream, OrderInfoExcelDTO.class)
.registerWriteHandler(ExcelUtil.getDefaultWriteHandler()) .registerWriteHandler(ExcelUtil.getDefaultWriteHandler())
//注入原生 或 自定义转换器
.registerConverter(new DateStringConverter()) .registerConverter(new DateStringConverter())
.registerConverter(new BigDecimalStringConverter()) .registerConverter(new BigDecimalStringConverter())
.sheet("工单列表").doWrite(arrayList); .sheet("工单列表").doWrite(arrayList);
byte[] excelBytes = outputStream.toByteArray();
return new ResponseEntity<>(excelBytes, headers, HttpStatus.OK);
} catch (IOException e) { } catch (IOException e) {
throw new BusinessException("订单列表模版下载异常:" + e.getMessage()); throw new BusinessException("订单列表模版下载异常:" + e.getMessage());
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!