Java实现xlsx文件下载和文件上传

Java实现xlsx文件下载和文件上传

文件下载:(xlsx下载案例)

直接上代码 (注:header的文件头编码要设置好否则可能会出现乱码)

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

文件上传

后续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);
        }
    }
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Java实现上传Excel文件,你可以使用一些常见的框架和库来简化该过程。下面是一个基本的示例代码,使用Spring Boot框架和Apache POI库来实现上传Excel文件: 1. 首先,确保在pom.xml文件中添加以下依赖: ```xml <!-- Spring Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Apache POI --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <!-- 读取和写出xlsx文件需要的依赖 --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> ``` 2. 创建一个Controller类来处理上传请求: ```java import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; @RestController public class ExcelUploadController { @PostMapping("/upload") public ResponseEntity<String> uploadExcel(@RequestParam("file") MultipartFile file) { // 处理上传的Excel文件 try { // 使用Apache POI库解析Excel文件 Workbook workbook = WorkbookFactory.create(file.getInputStream()); // 对Excel文件进行处理,如读取数据或写入数据库等操作 return ResponseEntity.ok("Excel文件上传成功!"); } catch (Exception e) { e.printStackTrace(); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Excel文件上传失败!"); } } } ``` 3. 运行Spring Boot应用程序,并使用HTTP POST请求上传Excel文件: ``` POST /upload Content-Type: multipart/form-data Body: - file: [选择要上传的Excel文件] ``` 这是一个简单的示例,你可以根据自己的需求进行进一步的处理和操作。请确保在代码中处理异常情况,并根据实际需求进行适当的错误处理和返回。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值