实现excel文件导入导出

一.首先引入读写excle的依赖

        我们这里使用poi技术进行导入导出,poi也有两个不同的jar包,分别是处理excel2003(xls)和excel2007(xlsx)+的,对应的是poi和poi-ooxml。

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>5.0.0</version>
</dependency>

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>5.0.0</version>
</dependency>

二.核心方法介绍

//工作蒲操作接口类
Workbook workbook=null;
//根据xls对应类实现接口
if (substring.equals(".xls")){
      workbook = new HSSFWorkbook(file.getInputStream());
}
//根据xlsx对应类实现接口
if (substring.equals(".xlsx")){
      workbook = new XSSFWorkbook(file.getInputStream());
}

//根据名称获取excle表
Sheet sheetAt = workbook.getSheet("name");
//根据下标获取excle表
Sheet sheetAt = workbook.getSheetAt(0);

//创建一个工作表
Sheet sheet= workbook.createSheet("工作表");
//创建一个行
Row row1 = sheet.createRow(0);
//创建行中的一个单元格
row1.createCell(0).setCellValue("标题");

//获取对应sheet的一行数据
Row row = sheetAt.getRow(0);
//获取到当前行的值
int rnum = row.getRowNum();
//这些都是根据行的那一列获取对应的值,值的类型无法指定,只能是根据表中的值随机生成 。(也可以强制为这一列设置值)
row.setCellType(CellType.STRING); //也可以这样强制设置类型
row.getCell(0).getStringCellValue() //获得的值为string类型
row.getCell(0).getNumericCellValue(); //获得的值为double类型

三.代码案例

1.导入功能的实现

//将表中信息导入到数据库
  @PostMapping("/importUser")
  public String export(MultipartFile file) throws Exception {
    String filename = file.getOriginalFilename();
    if (filename==null) return null;
    String substring = filename.substring(filename.lastIndexOf("."));
    Workbook workbook=null;
    if (substring.equals(".xls")){
      new HSSFWorkbook();
      workbook = new HSSFWorkbook(file.getInputStream());
    }else if (substring.equals(".xlsx")){
      new XSSFWorkbook();
      workbook = new XSSFWorkbook(file.getInputStream());
    }else{
      return "redirect:/error.html";
    }
    ArrayList<User> list = new ArrayList<>();
    ClassInfo classInfo=null;
    //根据名称获取excle表
    Sheet sheetAt = workbook.getSheet("信息记录");
    for (Row cells : sheetAt) {
      //获取到当前行的值
      int rnum=cells.getRowNum();
      System.out.println(rnum);
      if (rnum>1){
        User user = new User();
        //这些都是根据行的那一列获取对应的值         值的类型 无法指定 只能是根据表中的值 随机生成
        // Cell cell = cells.getCell(0);  cell.setCellType(CellType.STRING); 也可以这样强制设置类型
        user.setName(cells.getCell(0).getStringCellValue());
        user.setSex(cells.getCell(1).getStringCellValue());
        double numericCellValue = cells.getCell(2).getNumericCellValue();

        user.setAge((int)numericCellValue);
        user.setClassname(cells.getCell(3).getStringCellValue());
        double numericCellValue1 = cells.getCell(4).getNumericCellValue();
        user.setId((int)numericCellValue1);
        poiDao.insert(user);
      }
    }
    return null;
  }

  

2.导出功能的实现

//简单的下载测试 最普通的下载
  @GetMapping("/download")
  public void download(HttpServletResponse response) throws IOException {
    //设置响应头
    response.setHeader("Content-disposition", "attachment; filename=liutong.txt");
    //设置响应给前台的数据类型
    response.setContentType("text/html");
    //字符流输出  输出的类型就为上面设置的类型
    ServletOutputStream outputStream = response.getOutputStream();
    //写入数据
    outputStream.write("sss".getBytes());
    outputStream.write("sdsadsadas".getBytes());

  }

  //将数据信息导出
  @GetMapping("/exportUser")
  public void export( HttpServletResponse response) throws IOException {
    //创建一个excle文件
    XSSFWorkbook workbook = new XSSFWorkbook();
    //创建一个工作表
    XSSFSheet sheet= workbook.createSheet("刘桐信息");
    //创建一个行
    XSSFRow row1 = sheet.createRow(0);
    //创建行中的一个单元格
    row1.createCell(0).setCellValue("刘桐万岁");
    //合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列
    CellRangeAddress cellAddresses = new CellRangeAddress(0, 0, 0, 4);
    sheet.addMergedRegion(cellAddresses);
    //这里使用的mybatis plus  直接获取的数据   这里的对象是我自己写的测试对象   poiDao是自己写的一个测试接口
    List<User> list = poiDao.selectByMap(new HashMap<>());
    for (int i=0;i<list.size();i++){
      //创建一个行
      XSSFRow row = sheet.createRow(i + 1);
      //创建行中的一个单元格
      row.createCell(0).setCellValue(list.get(i).getId());
      row.createCell(1).setCellValue(list.get(i).getName());
      row.createCell(2).setCellValue(list.get(i).getSex());
      row.createCell(3).setCellValue(list.get(i).getAge());
      row.createCell(4).setCellValue(list.get(i).getClassname());
    }
    //字符流输出                                    //output.write("sss".getBytes());写入数据
    OutputStream output=response.getOutputStream();

    //清除首部的空白行  https://blog.csdn.net/songhuanfeng/article/details/91884316
    response.reset();
    //设置响应头              这里是下载头     https://blog.csdn.net/ccmm_/article/details/80885094
    response.setHeader("Content-disposition", "attachment; filename=liutong.xls");
    //设置响应给前台的数据类型  https://blog.csdn.net/qq_42108192/article/details/81938674
    response.setContentType("application/msexcel");
    workbook.write(output);
    workbook.close();
    output.close();
  }

 

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值