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);
}
}