Java操作Excel进行导入导出

Java操作办公软件

poi和jxl的比较

一、jxl
优点:
    Jxl对中文支持非常好,操作简单,方法看名知意。
    Jxl是纯javaAPI,在跨平台上表现的非常完美,代码可以再windows或者Linux上运行而无需重新编写
    支持Excel 95-2000的所有版本(网上说目前可以支持Excel2007了,还没有尝试过)
    生成Excel 2000标准格式
    支持字体、数字、日期操作
    能够修饰单元格属性
    支持图像和图表,但是这套API对图形和图表的支持很有限,而且仅仅识别PNG格式。

缺点:效率低,图片支持不完善,对格式的支持不如POI强大

二、POI
优点:

    效率高(数据来源:http://blog.csdn.net/jarvis_java/article/details/4924099)
    支持公式,宏,一些企业应用上会非常实用
    能够修饰单元格属性
    支持字体、数字、日期操作

缺点:不成熟,代码不能跨平台,貌似不少同行在使用工程中还碰到让人郁闷的BUG

推荐使用poi 兼容性好一下,支持办公软件多一些,jxl专注excel操作,excel要功能要丰富一点

使用EsayPOI导入导出Excel表格

在使用原生poi的api方式去完成导入导出的时候,是比较麻烦,所有使用更简单的方式,使用easypoi

1. easypoi是什么

easypoi 就是对原生poi进行封装,主打简单,就算不懂底层的poi的操作,也可以操作导入导出,而且提供的功能在项目里够用

2. easypoi的特点

1.设计精巧,使用简单
2.接口丰富,扩展简单
3.默认值多,write less do more
4.spring mvc支持,web导出可以简单明了

3. 使用easypoi

(1)导入jar包 —加入pom.xml的依赖(不能和原生的POI包同时导入,会有冲突)

<dependency>
  <groupId>cn.afterturn</groupId>
  <artifactId>easypoi-base</artifactId>
  <version>3.2.0</version>
</dependency>
<dependency>
  <groupId>cn.afterturn</groupId>
  <artifactId>easypoi-web</artifactId>
  <version>3.2.0</version>
</dependency>
<dependency>
  <groupId>cn.afterturn</groupId>
  <artifactId>easypoi-annotation</artifactId>
  <version>3.2.0</version>
</dependency>

(2)准备一个类 如EasyEmployee 和EasyDept

添加注解@Excel(name = “列名”)

@ExcelTarget("emp")
public class EasyEmployee extends BaseDomain{
    @Excel(name = "员工姓名")
    private String name;
    @Excel(name="邮件",width = 20)
    private String email;
    //性别
    @Excel(name="性别",replace={"男_true","女_false"})
    private boolean sex;
    @Excel(name="年纪")
    private Integer age;
    //出生日期
    @Excel(name="生日",format = "yyyy-MM-dd")
    private Date birthday;
    @ExcelEntity
    private EasyDept dept;
    @Excel(name="头像",type = 2)
    private String headImage;
}

(3)调用工具方法 导入导出

//导入
List<EasyEmployee> easyEmployees = ExcelImportUtil.importExcel(file, EasyEmployee.class, importParams);
//导出方法
 Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), EasyDept.class,
                list);

easypoi加入项目

项目里面员工的导入导出

1.导出功能

(1)导入依赖jar包

(2)在页面添加导出功能(提交整合表单)

<form id="searchForm" action="/employee/download" method="post">
                用户名: <input name="username" class="easyui-textbox" style="width:120px;height:32px">
                邮件: <input name="email" class="easyui-textbox" style="width:80px;height:32px">
                部门 :
                <input class="easyui-combobox" name="departmentId"
                       data-options="valueField:'id',textField:'name',panelHeight:'auto',url:'/util/queryDepartment'" />
                <a href="#" data-method="search"  class="easyui-linkbutton" iconCls="icon-search">查找</a>
                <!-- 这部门是查询的功能 点击下面的代码 等效于提交表单-->
                <button  class="easyui-linkbutton" iconCls="icon-redo">导出</button>
            </form>

(3)提交到EmployeeController 里面 download这个方法里面进行导出功能

(3.1)查询导出数据

(3.2)处理图片

(3.3)设置导出参数

(3.4)导出视图处理

(3.5)扫描视图的包

<context:component-scan base-package="cn.afterturn.easypoi.view" />
<bean id="beanNameViewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver"      p:order="0" />

导出代码的处理(Controller层)

public String download(EmployeeQuery employeeQuery, ModelMap map, HttpServletRequest req){
        //根据查询 查询数据
        List<Employee> employees = employeeService.findByQuery(employeeQuery);
        String realPath = req.getServletContext().getRealPath("");//E:\JavaEEIdeacode\aisell\src\main\web/images/head/avatar1.jpg
        employees.forEach(e->{
            //E:\JavaEEIdeacode\aisell\out\artifacts\aisell_Web_exploded\images\head
            e.setHeadImage(realPath+e.getHeadImage());
            System.out.println(e.getHeadImage());
        });
        //导出参数设置
        //E:\JavaEEIdeacode\aisell\src\main\web\images\head\sss.jpg
        ExportParams params = new ExportParams("员工数据", "员工表格1", ExcelType.XSSF);
        //固定两列的意思
        params.setFreezeCol(2);
        map.put(NormalExcelConstants.DATA_LIST, employees); // 数据集合
        map.put(NormalExcelConstants.CLASS, Employee.class);//导出实体
        map.put(NormalExcelConstants.PARAMS, params);//参数
        map.put(NormalExcelConstants.FILE_NAME, "xxx");//文件名称
        return NormalExcelConstants.EASYPOI_EXCEL_VIEW;//View名称
    }

3.3 导入功能

(1)新增页面import.jsp页面

​ 准备form表单,form表单里面提供一个上传的文件组件 easyui-filebox

<form action="/import/employeeXlsx" method="post" enctype="multipart/form-data">
    <input class="easyui-filebox" name="empFile" style="width:80%"
           data-options="prompt:'选择一个文件...',buttonText: '选择文件'" />
    <button class="easyui-linkbutton">导入</button>
</form>

(2)点击导入 提交到后台进行导入处理

开启自定义验证

​ 写一个类 去实现 接口

@Component
public class EmployeeExcelVerifyHandler implements IExcelVerifyHandler<Employee> {
    @Autowired
    private IEmployeeService employeeService;
    @Override
    public ExcelVerifyHandlerResult verifyHandler(Employee employee) {
        ExcelVerifyHandlerResult result = new ExcelVerifyHandlerResult(true);
        //检查用户名是否重复的方法
        boolean flag = employeeService.checkUsername(employee.getUsername());
        //检查用户名如果重复
        if(!flag){
            result.setSuccess(false);
            result.setMsg("用户名重复");
        }
        return result;
    }
}

注意扫描的包

<context:component-scan base-package="cn.itsource.aisell.common"/>

//开启验证 – 是否为null 最大值
importParams.setNeedVerfiy(true);
//加入自定义验证类 --验证用户名是否重复
importParams.setVerifyHandler(employeeExcelVerifyHandler);

b)调用方法进行导入

ExcelImportResult<Employee> result = ExcelImportUtil.importExcelMore(empFile.getInputStream(),
            Employee.class, importParams);

c)对于成功的结果 存入数据库

对于失败的结果 导出到前台展示

@RequestMapping("/employeeXlsx")
    public String employeeXlsx(MultipartFile empFile, HttpServletResponse response) throws Exception {
        ImportParams importParams = new ImportParams();
        importParams.setTitleRows(1);
        //开启验证 -- 是否为null 最大值
        importParams.setNeedVerfiy(true);
        //加入自定义验证类 --验证用户名是否重复
        importParams.setVerifyHandler(employeeExcelVerifyHandler);

        //完成导入 --调用导入
    /*    List<Employee> employees = ExcelImportUtil.importExcel(empFile.getInputStream(),
                Employee.class, importParams);*/
        //导入并且验证功能
        ExcelImportResult<Employee> result = ExcelImportUtil.importExcelMore(empFile.getInputStream(),
                Employee.class, importParams);

        result.getFailList().forEach(e->{
            System.out.println("失败数据:"+e);
        });
        //导入的数据 保存数据库
        result.getList().forEach(e->{
            System.out.println(e);
            //初始密码
            e.setPassword("123456");
            //根据部门名称 查询部门的id
            Department dept = departmentService.findByName(e.getDepartment().getName());
            e.setDepartment(dept);
            //存入数据库
            employeeService.save(e);
        });

        //把验证失败两个数据 导出到前台 error.xlsx
        if(result.isVerfiyFail()){
            //如果验证失败,代码到这里面来
            //失败的文件已经准备好了
            Workbook failWorkbook = result.getFailWorkbook();
            //把这个文件导出
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); //mime类型
            response.setHeader("Content-disposition", "attachment;filename=error.xlsx");
            response.setHeader("Pragma", "No-cache");//设置不要缓存
            OutputStream ouputStream = response.getOutputStream();
            failWorkbook.write(ouputStream);
            ouputStream.flush();
            ouputStream.close();
        }

        return "import";
    }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值