Java는 xlsx 파일 다운로드 및 파일 업로드를 구현합니다.
파일 다운로드: (xlsx 다운로드 사례)
코드를 직접 업로드하세요. (참고: 헤더의 파일 헤더 인코딩을 올바르게 설정해야 합니다. 그렇지 않으면 잘못된 문자가 나타날 수 있습니다.)
public void downloadTemplate(HttpServletRequest request, HttpServletResponse response) {
response.setContentType("application/x-msdownload");
String dateName = "下载文件.xlsx";
response.setHeader("Content-Disposition", "attachment;filename=" + new String(dateName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
OutputStream out = null;
try {
out = response.getOutputStream();
XSSFSheet sheet1 = workbook.createSheet("sheetName");
XSSFRow titleRow1 = sheet1.createRow(0);
titleRow1.createCell(0).setCellValue("行1列1");
titleRow1.createCell(1).setCellValue("行1列2");
titleRow1.createCell(2).setCellValue("行1列3");
XSSFRow titleRow2 = sheet1.createRow(1);
titleRow2.createCell(0).setCellValue("行2列1");
titleRow2.createCell(1).setCellValue("行2列2");
titleRow2.createCell(2).setCellValue("行2列3");
try {
workbook.write(out);
} catch (Exception e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(out != null){
out.flush();
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
파일 업로드
후속 xlsx 처리를 위해 홈 페이지로 이동하여 xlsx 읽기 및 구문 분석에 관해 제가 쓴 다른 기사를 볼 수 있습니다.
@PostMapping("/upload")
public void Upload(HttpServletRequest request, @RequestPart("file") MultipartFile files) {
String fileName = files.getOriginalFilename();
long size = files.getSize();
String prefix = null;
if (fileName != null) {
prefix = fileName.substring(fileName.lastIndexOf(".") + 1);
}
}