package com.fdkankan.manage.controller; import cn.hutool.poi.word.WordUtil; import com.alibaba.nacos.common.codec.Base64; import com.fdkankan.manage.common.ResultCode; import com.fdkankan.manage.exception.BusinessException; import com.fdkankan.manage.common.ResultData; import com.fdkankan.manage.service.IExcelService; import org.apache.commons.io.FileUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; 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.net.URLEncoder; import java.nio.charset.StandardCharsets; @RestController @RequestMapping("/service/manage/excel") public class ExcelController { @Autowired IExcelService excelService; /** * 下载出入库模板 * type 0 入库模板, 1出库模板 ,2 客户关联模板,3 rtk设备模版,4rtk账号模版,5 rtk账号过期时间 */ @GetMapping("/downTemplate") public void downInTemplate(@RequestParam(required = false,defaultValue = "0") Integer type, HttpServletResponse res, HttpServletRequest req) throws IOException { String fileName = ""; switch (type){ case 0 : fileName = "cameraIn.xlsx"; break; case 1 : fileName = "cameraOut.xlsx"; break; case 2 : fileName = "cameraCompany.xlsx"; break; case 3 : fileName = "rtkDevice.xlsx"; break; case 4 : fileName = "rtkAccount.xlsx"; break; case 5 : fileName = "rtkAccountFailureTime.xlsx"; break; default: throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS); } InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("template/"+fileName); OutputStream os = res.getOutputStream(); try { String agent = req.getHeader("USER-AGENT"); if (agent != null && !agent.contains("MSIE") && !agent.contains("like Gecko")) {// FF String enableFileName = "=?UTF-8?B?" + (new String(Base64.encodeBase64(fileName.getBytes(StandardCharsets.UTF_8)))) + "?="; res.setHeader("Content-Disposition", "attachment; filename=" + enableFileName); } else { // IE String enableFileName = URLEncoder.encode(fileName, "UTF-8"); res.setHeader("Content-Disposition", "attachment; filename=" + enableFileName); } File file = new File(fileName); FileUtils.copyInputStreamToFile(inputStream,file); os.write(FileUtils.readFileToByteArray(file)); os.flush(); } finally { os.close(); } } /** * 导入excel * type 0 入库模板, 1出库模板 ,2 客户关联模板,3 rtk设备模版,4rtk账号模版,5 rtk账号过期时间 */ @PostMapping("/uploadExcel") public ResultData uploadExcel(@RequestParam(required = false) MultipartFile file, @RequestParam(required = false,defaultValue = "0") Integer type) throws IOException { Integer count = excelService.uploadExcel(file, type); return ResultData.ok("成功导入"+count+"条数据。"); } }