智能商贸-day8-导入导出

这篇博客介绍了如何使用Java的Apache POI库和SpringMVC来处理Excel的导入导出功能。内容涵盖从引入依赖到创建、读取Excel文件,再到SpringMVC中实现导出和导入的详细步骤,包括EasyPOI的基本使用和对象注解,以及自定义验证功能的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.1. Java操作Excel

1.1.1. 引入poi需要的jar包

<!-- poi支持的jar包 -->
<dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi</artifactId>
  <version>3.11</version>
</dependency>
<dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi-ooxml</artifactId>
  <version>3.11</version>
</dependency>

1.1.2. 创建Excel(完成99乘法表)

package cn.itsource.pss.common;
…
public class ExcelTest {

    //创建一个Excel文件
    //在文件里加一个99乘法法
    //把这文件保存在项目中
    @Test
    public void testCreateExcel() throws Exception{
        //1.创建一个Excel文件(内存中)
        SXSSFWorkbook wb = new SXSSFWorkbook();
        //2.创建一张表
        Sheet sheet = wb.createSheet("99乘法表");
        //3.创建行
        for (int i = 1; i <= 9; i++) {
            Row row = sheet.createRow(i-1);
            //4.创建列(格子)
            for (int j = 1; j <= i; j++) {
                Cell cell = row.createCell(j-1);
                //5.格子中加数据
                cell.setCellValue(i+"*"+j+"="+(i*j));
            }
        }
        //从内存中写出来
        FileOutputStream out = new FileOutputStream("99.xlsx");
        wb.write(out);
        out.close();
    }
}

1.1.3. 读取Excel

可以使用我们已经准备好的excel文件做测试

/**
 * 读取我们使用相应的方案
 * @throws Exception
 */
@Test
public void readExcel() throws Exception{
    File file = new File("employee-3.xlsx");
    FileInputStream fis = new FileInputStream(file);
    //1.读取一个Excel文件(内存中)
    Workbook wb = new XSSFWorkbook(fis);
    //2.拿到第个sheet表
    Sheet sheet = wb.getSheetAt(0);
    //3.拿到wb中的行(不要拿头部)
    int lastRowNum = sheet.getLastRowNum();
    for (int i = 1; i <= lastRowNum; i++) {
        Row row = sheet.getRow(i);
        //4.拿到每一列(格子)
        short lastCellNum = row.getLastCellNum();
        for (int j = 0; j < lastCellNum; j++) {
            Cell cell = row.getCell(j);
            System.out.print(cell.getStringCellValue()+" ");
        }
        System.out.println();
    }
}

1.2. SpringMVC导出功能(了解认识)

response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); //mime类型
 response.setHeader("Content-disposition", "attachment;filename="+filename);
 response.setHeader("Pragma", "No-cache");//设置不要缓存
 OutputStream ouputStream = response.getOutputStream();
 wb.write(ouputStream);
 ouputStream.flush();
 ouputStream.close();

1.2.1. 直接导出Excel文件

  1. employee.jsp添加导出按钮
<a href="/employee/download "  class="easyui-linkbutton" iconCls="icon-redo" plain="true">导出</a>
  1. EmployeeController完成导出功能
@RequestMapping("/download")
public void download(HttpServletResponse response) throws Exception{
    //准备下载的文件名
    String filename = "employee.xlsx";
    response.setHeader("Content-disposition", filename);
    response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); //mime类型
    response.setHeader("Content-disposition", "attachment;filename="+filename);
    response.setHeader("Pragma", "No-cache");

    //1.创建一个Excel文件(内存中)
    SXSSFWorkbook wb = new SXSSFWorkbook();
    //2.创建一张表
    Sheet sheet = wb.createSheet("99乘法表");
    //3.创建行
    for (int i = 1; i <= 9; i++) {
        Row row = sheet.createRow(i-1);
        //4.创建列(格子)
        for (int j = 1; j <= i; j++) {
            Cell cell = row.createCell(j-1);
            //5.格子中加数据
            cell.setCellValue(i+"*"+j+"="+(i*j));
        }
    }
    //从内存中写出来
    OutputStream out = response.getOutputStream();
    wb.write(out);
    out.close();
}

1.2.2. 导出真实数据

导出数据时提交表单

  1. 完成导出功能的工具方法
package cn.itsource.pss.common;
…
//Excel的公共类
public class ExcelUtils {
    /**
     * 导出一个Excel文件
     * @param filename  文件名称
     * @param heads 文件头
     * @param datas 文件内容
     * @param response 响应
     * @throws Exception
     */
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值