1、System.getProperty(user.dir) 獲取的是啟動項目的容器位置
2、 Files.copy(sourceFile.toPath(), destinationFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
- StandardCopyOption.REPLACE_EXISTING 來忽略文件已經存在的異常,如果存在就去覆蓋掉它
- StandardCopyOption.COPY_ATTRIBUTES copy文件的屬性,最近修改時間,最近訪問時間等信息,不僅copy文件的內容,連文件附帶的屬性一并復制
?
//獲得要下載的excel的模板、其中mb.xlsx是模板String sourceFilePath = System.getProperty("user.dir") + File.separator + "mb.xlsx";//獲得用戶的當前工作目錄String currentPath = System.getProperty("user.dir");//創建要生成的excel的路徑String fileName1 = "mb" + System.currentTimeMillis();String destinationFilePath = currentPath + File.separator + fileName1 + ".xlsx";// 創建源文件和目標文件對象File sourceFile = new File(sourceFilePath);File destinationFile = new File(destinationFilePath);try {// 復制文件Files.copy(sourceFile.toPath(), destinationFile.toPath(), StandardCopyOption.REPLACE_EXISTING);System.out.println("文件復制成功!");} catch (IOException e) {e.printStackTrace();}//填充數據exportRawData(list, 1, destinationFilePath);String fileName = "分析-" + s + "月.xlsx";File file = new File(destinationFilePath); if (file.exists()) {String mimeType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";response.setContentType(mimeType);response.setContentLength((int) file.length());response.setCharacterEncoding("UTF-8");response.setHeader("content-Type", "application/vnd.ms-excel");response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));try (BufferedInputStream inStream = new BufferedInputStream(new FileInputStream(file));BufferedOutputStream outStream = new BufferedOutputStream(response.getOutputStream())) {byte[] buffer = new byte[4096];int bytesRead;while ((bytesRead = inStream.read(buffer)) != -1) {outStream.write(buffer, 0, bytesRead);}}} else {response.sendError(HttpServletResponse.SC_NOT_FOUND);}Files.delete(Paths.get(destinationFilePath)); }
?填充數據的代碼:
boolean exportRawData(List<ImpactIndexTable> list, Integer type, String filePath) {String sheetName ="原始數據";// 要填充數據的起始行數int rowNum = 4;// 要填充數據的起始列數int colNum = 0;int rowTotal = 18;try (FileInputStream fis = new FileInputStream(filePath);Workbook workbook = new XSSFWorkbook(fis)) {Sheet sheet = workbook.getSheet(sheetName);if (sheet == null) {// 如果指定的工作表不存在,可以在這里進行處理log.info("指定的工作表不存在!");}for (int i = 0; i < list.size(); i++) {// 創建并設置單元格樣式CellStyle style = workbook.createCellStyle();style.setAlignment(HorizontalAlignment.CENTER);style.setVerticalAlignment(VerticalAlignment.CENTER);SXSSFWorkbook wb = new SXSSFWorkbook(-1);// 獲取要填充的行Row row = sheet.getRow(rowNum + i);if (row == null) {row = sheet.createRow(rowNum + i);}for (int n = 0; n < rowTotal; n++) {Cell cell = row.createCell(colNum + n);switch (n) {case 0:cell.setCellValue(list.get(i).getProjectName());cell.setCellStyle(style);break;case 1:cell.setCellValue(list.get(i).getHw());cell.setCellStyle(style);break;//后續按照需要填充數據default:}}}// 保存修改后的Excel文件try (FileOutputStream fos = new FileOutputStream(filePath)) {workbook.write(fos);log.info("數據導出成功:{}", type);return true;}} catch (Exception e) {log.error("導出錯誤:{}", e);}return false;}