springboot excel操作

springboot 对Excel 操作

1 pom.xml

org.apache.poi poi 3.17 org.apache.poi poi-ooxml 3.15

2 mapper
@Mapper
public interface SysExcelMapper {
int excelToDB(@Param(“name”) String name,@Param(“url”) String url);
}

service
public interface SysExcelService {
//处理上传的excel文件
List<List> getBankListByExcel(InputStream in, String fileName)throws Exception;
//判断excel文件的格式
Workbook getWorkbook(InputStream inStr, String fileName)throws Exception;

int excelToDB(String name,String url);

}

3 serviceImpl
package com.back.service.impl;

import com.back.mapper.SysExcelMapper;
import com.back.service.SysExcelService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**

  • @ClassName SysExcelServiceImpl

  • @Description TODO

  • @Author

  • @Date 2019/1/11 14:00
    **/
    @Service
    public class SysExcelServiceImpl implements SysExcelService {
    @Autowired
    private SysExcelMapper sysExcelMapper;
    private final static String excel2003 =".xls";
    private final static String excel2007 =".xlsx";
    @Override
    public List<List> getBankListByExcel(InputStream in, String fileName) throws Exception {
    List<List> list = null;
    //创建excel
    Workbook work = this.getWorkbook(in,fileName);
    if(nullwork){
    throw new Exception(“创建Excel工作薄为空!”);
    }
    Sheet sheet = null;
    Row row = null;
    Cell cell = null;
    list = new ArrayList<List>();
    for (int i = 0;i<work.getNumberOfSheets();i++){
    sheet = work.getSheetAt(i);
    if(sheet
    null){
    continue;
    }
    for(int j = sheet.getFirstRowNum();j<=sheet.getLastRowNum();j++){
    row = sheet.getRow(j);
    if(row==null||row.getFirstCellNum()==j){
    continue;
    }
    List li = new ArrayList();
    for (int y = row.getFirstCellNum(); y < row.getLastCellNum(); y++) {
    cell = row.getCell(y);
    li.add(cell);
    }
    list.add(li);
    }
    }
    work.close();
    return list;
    }

    @Override
    public Workbook getWorkbook(InputStream inStr, String fileName) throws Exception {
    Workbook wb = null;
    String fileType = fileName.substring(fileName.lastIndexOf("."));
    if(excel2003.equals(fileType)){
    wb = new HSSFWorkbook(inStr);
    }else if(excel2007.equals(fileType)){
    wb = new XSSFWorkbook(inStr);
    }else{
    throw new Exception(“解析的文件格式有误!”);
    }
    return wb;
    }

    @Override
    public int excelToDB(String name, String url) {
    return sysExcelMapper.excelToDB(name,url);
    }
    }

4 controller
package com.back.controller;

import com.back.entity.ResponseEntity;
import com.back.service.SysExcelService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**

  • @ClassName SysExcelController
  • @Description TODO
  • @Author
  • @Date 2019/1/11 14:15
    **/
    @RestController
    @Slf4j
    @Api(description = “excel文档”)
    public class SysExcelController {
    @Autowired
    private SysExcelService sysExcelService;
    @RequestMapping(value = “/excel”,method = RequestMethod.POST)
    public ResponseEntity excel(@RequestParam(“file”) MultipartFile file) {
    ResponseEntity responseEntity = new ResponseEntity();
    InputStream inputStream =null;
    List<List> list = null;
    if(file.isEmpty()){
    log.info(“文件不能为空”);
    return responseEntity.fail(300,“文件不能为空”);
    }
    try {
    inputStream = file.getInputStream();
    list = sysExcelService.getBankListByExcel(inputStream,file.getOriginalFilename());
    inputStream.close();
    //连接数据库部分
    for (int i = 0; i < list.size(); i++) {
    List lo = list.get(i);
    //调用mapper中的excelToDB方法
    sysExcelService.excelToDB(String.valueOf(lo.get(0)),String.valueOf(lo.get(1)));
    }
    log.info(“处理成功”);
    return responseEntity.success(200,“处理成功”);
    } catch (IOException e) {
    log.info(“文件流异常”);
    return responseEntity.fail(300,“文件流异常”);
    } catch (Exception e) {
    log.info(“文件流异常”);
    return responseEntity.fail(300,“文件流异常”);
    }
    }
    }
Spring Boot 中处理包含时间的 Excel 文件通常涉及到读取和写入操作。如果你的文件名中包含了时间,比如 "report_20230401.xlsx",你可能会想要根据文件名获取特定日期的数据。这可以通过一些简单的字符串处理和日期解析库(如 Apache POI 或者 Spring-provided XSSF)来实现。 例如,你可以创建一个服务类,用于查找指定日期范围内的Excel文件: ```java import org.apache.commons.io.FileUtils; import org.springframework.stereotype.Service; import java.io.File; import java.time.LocalDate; import java.util.Date; @Service public class FileService { private static final String TEMPLATE_FILE_NAME_FORMAT = "report_%d%m%y.xlsx"; public void processExcelFiles(LocalDate startDate, LocalDate endDate) { File directory = new File("path/to/excel/files"); // 替换为实际文件夹路径 for (File file : directory.listFiles()) { if (file.getName().matches(String.format(TEMPLATE_FILE_NAME_FORMAT, startDate.year(), startDate.monthValue(), startDate.dayOfMonth()))) { // 解析并处理文件内容 processFile(file); } else if (file.getName().matches(String.format(TEMPLATE_FILE_NAME_FORMAT, endDate.year(), endDate.monthValue(), endDate.dayOfMonth()))) { processFile(file); } } } private void processFile(File file) { // 使用Apache POI或者其他库读取文件内容并执行相应的业务逻辑 } } ``` 在这个例子中,`processExcelFiles` 方法会遍历目录中的所有文件,如果文件名称匹配模板格式,就会调用 `processFile` 方法来进一步处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值