springboot上传xls文件直接解析内容不保存

主要使用 InputStream inputStream = mFile.getInputStream(); 读取上传文件 “ mFile为MultipartFile”

前端Augular 10

  • html
     <a href="javascript:;" class=" file btn btn-warning q7 ">录入展会信息
            <input type="file" name="" accept=".xls, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel" (change)="importExcel($event)">
          </a>
  • ts
  importExcel(event) {
   
    const that = this;
    const file = event.target.files[0];
    var form = new FormData();
    form.append('file',file);
    form.append('id','1');
    var token =document.getElementById('token').innerHTML;
    $.ajax({
   
      url: urlRequest + '/EnergyConsumptionData/importExcel',
      data: form,
      type: 'post',
      contentType: false,
      processData: false,
      dataType: 'json',
      headers: {
   Authorization: token},
      async: false,
      success(data) {
   
        console.log(data);
        if( data.data.code == 200){
   
          that.msg = data.data.msg;
          that.msg.push(data.data.runTime);
        }else {
   
          alert(data.data.msg);
        }
      }
    });
  }
  • css
.file {
   
  position: relative;
  display: inline-block;
  /*background: #D0EEFF;*/
  /*border: 1px solid #99D3F5;*/
  /*border-radius: 4px;*/
  /*padding: 4px 12px;*/
  overflow: hidden;
  /*color: #1E88C7;*/
  text-decoration: none;
  text-indent: 0;
  /*line-height: 20px;*/
}
.file input {
   
  position: absolute;
  /*font-size: 100px;*/
  right: 0;
  top: 0;
  opacity: 0;
}
.file
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot 提供了强大的功能来处理MultipartFile,这是一种在HTTP请求中上传文件类型。当你需要解析XLS文件时,通常会涉及到读取Excel内容并将其转换成可操作的数据结构。以下是一个简单的步骤来解析`.xls`文件: 1. 引入依赖:首先在你的Spring Boot项目中添加Apache POI库,它是Java处理Microsoft Office文件(包括.xls)的流行库。可以在pom.xml或build.gradle文件中添加依赖: ```xml <!-- Maven --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>5.0.0</version> <!-- 或者最新版本 --> </dependency> <!-- Gradle --> implementation 'org.apache.poi:poi-ooxml:5.0.0' <!-- 或者最新版本 --> ``` 2. 处理上传:当用户上传文件时,在Controller层接收并处理MultipartFile: ```java @PostMapping("/upload") public String handleExcelUpload(@RequestParam("file") MultipartFile file) { if (file.isEmpty()) { return "File is empty"; } try { // 将MultipartFile转换为InputStream InputStream inputStream = file.getInputStream(); // 创建一个FileCopyHelper实例来读取Excel内容 FileCopyHelper copyHelper = new FileCopyHelper(new File("temp.xlsx")); // 将InputStream写入临时文件 copyHelper.copyTo(inputStream, new FileOutputStream("temp.xlsx")); // 使用HSSFWorkbook读取文件 Workbook workbook = new HSSFWorkbook(new FileInputStream("temp.xlsx")); // 打开第一个sheet进行处理 Sheet sheet = workbook.getSheetAt(0); // ...解析Excel内容... // 关闭资源 workbook.close(); inputStream.close(); } catch (IOException e) { e.printStackTrace(); return "Error while processing file"; } return "File uploaded successfully"; } ``` 3. 解析数据:使用POI库提供的类如`Row`和`Cell`来逐行读取和处理Excel中的数据。例如,你可以遍历每一行,获取单元格值,并根据需要处理这些数据。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值