Java implementa la descarga y carga de archivos xlsx

Java implementa la descarga y carga de archivos xlsx

Descarga de archivos: (caso de descarga xlsx)

Cargue el código directamente (nota: la codificación del encabezado del archivo del encabezado debe estar configurada correctamente; de ​​lo contrario, pueden aparecer caracteres confusos)

 public void downloadTemplate(HttpServletRequest request, HttpServletResponse response) {
    
    
        //设置ContentType文件下载类型
        response.setContentType("application/x-msdownload");
        //在Header上设置文件名和编码
        String dateName = "下载文件.xlsx";
        response.setHeader("Content-Disposition", "attachment;filename=" + new String(dateName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
        //声明一个输出流
        OutputStream out = null;
        try {
    
    
            //拿到response输出流
            out = response.getOutputStream();
            //sheet页name
            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();
            }
        }
    }

Subir archivo

Para el procesamiento posterior de xlsx, puede ir a la página de inicio para ver otro artículo que escribí sobre cómo leer y analizar 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);
        }
    }

Supongo que te gusta

Origin blog.csdn.net/icemeco/article/details/125654289
Recomendado
Clasificación