ExcelController.java 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package com.fdkankan.manage.controller;
  2. import cn.hutool.poi.word.WordUtil;
  3. import com.alibaba.nacos.common.codec.Base64;
  4. import com.fdkankan.manage.common.ResultCode;
  5. import com.fdkankan.manage.exception.BusinessException;
  6. import com.fdkankan.manage.common.ResultData;
  7. import com.fdkankan.manage.service.IExcelService;
  8. import org.apache.commons.io.FileUtils;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.web.bind.annotation.*;
  11. import org.springframework.web.multipart.MultipartFile;
  12. import javax.servlet.http.HttpServletRequest;
  13. import javax.servlet.http.HttpServletResponse;
  14. import java.io.File;
  15. import java.io.IOException;
  16. import java.io.InputStream;
  17. import java.io.OutputStream;
  18. import java.net.URLEncoder;
  19. import java.nio.charset.StandardCharsets;
  20. @RestController
  21. @RequestMapping("/service/manage/excel")
  22. public class ExcelController {
  23. @Autowired
  24. IExcelService excelService;
  25. /**
  26. * 下载出入库模板
  27. * type 0 入库模板, 1出库模板 ,2 客户关联模板,3 rtk设备模版,4rtk账号模版,5 rtk账号过期时间
  28. */
  29. @GetMapping("/downTemplate")
  30. public void downInTemplate(@RequestParam(required = false,defaultValue = "0") Integer type,
  31. HttpServletResponse res, HttpServletRequest req) throws IOException {
  32. String fileName = "";
  33. switch (type){
  34. case 0 : fileName = "cameraIn.xlsx"; break;
  35. case 1 : fileName = "cameraOut.xlsx"; break;
  36. case 2 : fileName = "cameraCompany.xlsx"; break;
  37. case 3 : fileName = "rtkDevice.xlsx"; break;
  38. case 4 : fileName = "rtkAccount.xlsx"; break;
  39. case 5 : fileName = "rtkAccountFailureTime.xlsx"; break;
  40. default: throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  41. }
  42. InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("template/"+fileName);
  43. OutputStream os = res.getOutputStream();
  44. try {
  45. String agent = req.getHeader("USER-AGENT");
  46. if (agent != null && !agent.contains("MSIE") && !agent.contains("like Gecko")) {// FF
  47. String enableFileName = "=?UTF-8?B?" + (new String(Base64.encodeBase64(fileName.getBytes(StandardCharsets.UTF_8))))
  48. + "?=";
  49. res.setHeader("Content-Disposition", "attachment; filename=" + enableFileName);
  50. } else { // IE
  51. String enableFileName = URLEncoder.encode(fileName, "UTF-8");
  52. res.setHeader("Content-Disposition", "attachment; filename=" + enableFileName);
  53. }
  54. File file = new File(fileName);
  55. FileUtils.copyInputStreamToFile(inputStream,file);
  56. os.write(FileUtils.readFileToByteArray(file));
  57. os.flush();
  58. } finally {
  59. os.close();
  60. }
  61. }
  62. /**
  63. * 导入excel
  64. * type 0 入库模板, 1出库模板 ,2 客户关联模板,3 rtk设备模版,4rtk账号模版,5 rtk账号过期时间
  65. */
  66. @PostMapping("/uploadExcel")
  67. public ResultData uploadExcel(@RequestParam(required = false) MultipartFile file,
  68. @RequestParam(required = false,defaultValue = "0") Integer type) throws IOException {
  69. Integer count = excelService.uploadExcel(file, type);
  70. return ResultData.ok("成功导入"+count+"条数据。");
  71. }
  72. }