JXL导出

jxl动态导出Excel 一、概述 在工作过程中,都会遇到这样一个需求,动态选择想要导出的字段,这里写成通用的导出Excel的工具。 二、项目实现 1、构建pom.xml 我的工程是利用Maven来构建的,这里仅给出最核心的包 <!--jxi导出--> <dependency> <groupId>net.sourceforge.jexcelapi</gro... 阅读详情

今天在项目中要做一个导出Excel的功能,首先想到的是使用POI,但是项目中要求使用JXL,所以最终使用了JXL。
这次的导出不是按传统的先创建表再设计各种样式,而是采用建立一个模板,导出时先获取模板,根据模板生成导出的Excel文件,然后添加数据。最终代码如下:

页面js方法:(注意:ajax的返回值类型是json,text,html,xml类型,或者可以说ajax的接收类型只能是string字符串,不是流类型,所以无法实现文件下载。)
    /*receiveOrderName 、paymentCityId 是页面条件查询的数据,因为请求路径拼接不能有空,所以下面做了判断,
值为空的时候拼一个0,后续再controller中会做处理*/
    function exportEntrustSocialSecurityFund() {
        $.messager.confirm('确认框', '将按排接单和缴费地区筛选导出', function (r) {
            if (r){
                var receiveOrderName = $('#search_receiveOrderName').val(); 
                var paymentCityId = $('#search_paymentCity').val();
                if ($.trim(receiveOrderName)){
                    if ($.trim(paymentCityId)) {
                        window.location.href='${ctx}/user/handleEntrustSocialSecurityFundIncrease/exportEntrustSocialSecurityFund/' + receiveOrderName + '/' + paymentCityId;
                    }else {
                        window.location.href='${ctx}/user/handleEntrustSocialSecurityFundIncrease/exportEntrustSocialSecurityFund/' + receiveOrderName + '/' + 0;
                    }
                }else if($.trim(paymentCityId)){
                    window.location.href='${ctx}/user/handleEntrustSocialSecurityFundIncrease/exportEntrustSocialSecurityFund/' + 0 + '/' + paymentCityId;
                }else {
                    window.location.href='${ctx}/user/handleEntrustSocialSecurityFundIncrease/exportEntrustSocialSecurityFund/' + 0 + '/' + 0;
                }
            }
        })
    }
controller:
    /**
     * 功能:导出
     */
    @RequestMapping(value = "exportEntrustSocialSecurityFund/{receiveOrderName}/{paymentCityId}", method = RequestMethod.GET)
    @ResponseBody
    public void exportPhonecard(@PathVariable("receiveOrderName") String receiveOrderName,
                                @PathVariable("paymentCityId") String paymentCityId,
                                HttpServletRequest request,HttpServletResponse response) throws Exception {

        /*
         * 查询参数
         */
        Map<String, Object> searchParams = Maps.newHashMap();
        if (StringUtils.isNotBlank(receiveOrderName)&&!receiveOrderName.equals("0")){
            searchParams.put("LIKE_receiveOrder.name", DecodeUtil.decodeParam(receiveOrderName));
        }
        if (StringUtils.isNotBlank(paymentCityId)&&!paymentCityId.equals("0")) {
            searchParams.put("EQ_paymentArea.id", paymentCityId);
        }
        searchParams.put("EQ_handlePart", CommonStatus.HandlePart.EXTERNAL_SUPPLIERS.toString());
        //因为要导出的是两个sheet页,且数据不同,所以先将需要的数据查出来
        List<ProjectEmployeeVO> increase = entrustSocialSecurityFundService.getIncreaseEntrustSocialSecurityFund(searchParams);
        List<ProjectEmployeeVO> decrease = entrustSocialSecurityFundService.getDecreaseEntrustSocialSecurityFund(searchParams);
        if (increase.size()>0||decrease.size()>0){
            //获取路径
            String theWebPath = WebUtils.getRealPath(request.getSession().getServletContext(), "/");
            String templateDownloadDir = theWebPath + SysConstants.DOWNLOAD_ROOT + "/template/";
            String templateUploadDir = theWebPath + SysConstants.UPLOAD_ROOT + "/import/entrustSocialSecurityFund/";
            // 获取需要复制的模板
            String templatePath = templateDownloadDir + "exportEntrustSocialSecurityFund.xls";
            // 需要新生成的模板
            String exportPath = templateUploadDir + "exportEntrustSocialSecurityFund" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".xls";
            String oriname = "增减员_";
            String downLoadName = new String(oriname.getBytes("utf-8"), "iso8859-1");
            String exportFile = FileUtil.getFileName(downLoadName); // 导出文件名
            entrustSocialSecurityFundService.exportPhonecard(templatePath, exportPath,increase,decrease);
            try {
                //相应信息文件名
                response.setHeader("Content-disposition", "attachment;filename=\"" + exportFile);
            } catch (Exception e1) {
                e1.printStackTrace();
            }
            //设置数据种类,使客户端浏览器,区分不同种类的数据
            response.setContentType("application/msexcel");
            try {
                //读取文件
                FileInputStream fileInputStream = new FileInputStream(new File(exportPath));
                //输出流
                OutputStream out = response.getOutputStream();
                int i = 0;
                while ((i = fileInputStream.read()) != -1) {
                    //输出
                    out.write(i);
                }
                //关流
                fileInputStream.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
service:
    /**
     * 导出
     */
    public void exportPhonecard(String templatePath, String exportPath, List<ProjectEmployeeVO> increase, List<ProjectEmployeeVO> decrease) {
        Workbook wb = null;
        WritableWorkbook workbook = null;
        WritableSheet sheet = null;
        WritableSheet sheet2 = null;
        try {
            wb = Workbook.getWorkbook(new File(templatePath));
            //参照模板,创建要导出的
            workbook = Workbook.createWorkbook(new File(exportPath), wb);
            if (increase.size()>0){
                sheet = workbook.getSheet(0);
                writeToExcelSheet1(increase, sheet);
            }
            if (decrease.size()>0){
                sheet2 = workbook.getSheet(1);
                writeToExcelSheet2(decrease, sheet2);
            }
            workbook.write();
        } catch (BiffException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (workbook != null) {
                try {
                    workbook.close();
                } catch (WriteException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (wb != null) {
                wb.close();
            }
        }
    }

    /**
     * 写sheet1
     */
    private void writeToExcelSheet1(List<ProjectEmployeeVO> list, WritableSheet sheet) {
        Label label;
        try {
            // 向表中添加记录
            int rows = sheet.getRows();// 定义默认的行
            for (int i = 0; i < list.size(); i++) {
                ProjectEmployeeVO projectEmployeeVO = list.get(i);
                // 数据信息从第二行开始;一列一列插入行 Label(列号,行号,内容,风格)
                label = new Label(0, rows, i + 1 + ""); // 序号
                sheet.addCell(label);
                label = new Label(1, rows, projectEmployeeVO.getEmployeeName()); // 姓名
                sheet.addCell(label);
                label = new Label(2, rows, projectEmployeeVO.getEmployeeIdCard()); // 身份证号
                sheet.addCell(label);
                label = new Label(3, rows, projectEmployeeVO.getEmployeePhone()); // 电话号码
                sheet.addCell(label);
                if (StringUtils.isNotBlank(projectEmployeeVO.getHouseholdPlace())){
                    label = new Label(4, rows, projectEmployeeVO.getEmployeePhone()); // 户口所在地
                }else {
                    label = new Label(4, rows, "---"); // 户口所在地
                }
                sheet.addCell(label);
                if (projectEmployeeVO.getAccountNature()==2){
                    label = new Label(5, rows, "城镇户口"); // 2:城镇户口;
                }else if (projectEmployeeVO.getAccountNature()==3){
                    label = new Label(5, rows, "农村户口"); // 3:农村户口
                }
                sheet.addCell(label);
                if (projectEmployeeVO.getCensusRegisterType()==2){
                    label = new Label(6, rows, "本地户籍"); // 2:本地户籍;
                }else if (projectEmployeeVO.getCensusRegisterType()==3){
                    label = new Label(6, rows, "外地户籍"); // 3:外地户籍
                }
                sheet.addCell(label);
                label = new Label(7, rows, projectEmployeeVO.getPaymentAreaName()); // 缴费城市
                sheet.addCell(label);
                label = new Label(8, rows, projectEmployeeVO.getCheckIdCard()); // 身份证号校验
                sheet.addCell(label);
                label = new Label(9, rows, "---"); // 备注
                sheet.addCell(label);

                rows = rows + 1;
            }
        } catch (RowsExceededException e) {
            e.printStackTrace();
        } catch (WriteException e) {
            e.printStackTrace();
        }
    }
    //写sheet2
    private void writeToExcelSheet2(List<ProjectEmployeeVO> list, WritableSheet sheet) {
        Label label;
        try {
            // 向表中添加记录
            int rows = sheet.getRows();// 定义默认的行
            for (int i = 0; i < list.size(); i++) {
                ProjectEmployeeVO projectEmployeeVO = list.get(i);
                // 数据信息从第二行开始;一列一列插入行 Label(列号,行号,内容,风格)
                label = new Label(0, rows, i + 1 + ""); // 序号
                sheet.addCell(label);
                label = new Label(1, rows, projectEmployeeVO.getEmployeeName()); // 姓名
                sheet.addCell(label);
                label = new Label(2, rows, projectEmployeeVO.getEmployeeIdCard()); // 身份证号
                sheet.addCell(label);
                label = new Label(3, rows, projectEmployeeVO.getEmployeePhone()); // 电话号码
                sheet.addCell(label);
                label = new Label(4, rows, projectEmployeeVO.getPaymentAreaName()); // 缴费城市
                sheet.addCell(label);
                label = new Label(5, rows, projectEmployeeVO.getSsLastPayment()); // 社保最后缴费月
                sheet.addCell(label);
                label = new Label(6, rows, projectEmployeeVO.getPfLastPayment()); // 公积金最后缴费月
                sheet.addCell(label);
                label = new Label(7, rows, projectEmployeeVO.getCheckIdCard()); // 身份证号校验
                sheet.addCell(label);
                label = new Label(8, rows, "---"); // 备注
                sheet.addCell(label);

                rows = rows + 1;
            }
        } catch (RowsExceededException e) {
            e.printStackTrace();
        } catch (WriteException e) {
            e.printStackTrace();
        }
    }
完成
Java基于jxl与模板导出excel并弹出下载 操作Excel文件的开源工具有很多,用得比较多的就是POI与JXL,网上这方面的资料也很多,一般的导出操作需要涉及很多的单元格操作,比较繁琐。所以通过模板的方式先定好布局,那么就免去不少麻烦,我们只需要遍历数据即可。 首先,先看看不连接数据库的例子public static void test(){ ReportEnginer enginer = new ReportEnginer(); 阅读详情

相关推荐

jxl数据导入导出jxl数据导入导出jxl数据导入导出jxl数据导入导出

jxl数据导入导出jxl数据导入导出jxl数据导入导出 jxl数据导入导出jxl数据导入导出jxl数据导入导出

java 判断一个物料是否有下级 并去重累计数量

/** * 通过审核 cy 审核通过后生成sku 如果是成品 要得到需要多少半成品和多少原料 组成 * 生成采购建议单 和 采 购建议明细单 */ @RequestMapping(value = "approved", method = RequestMethod.POST) public synchronized String approve...

c230704的博客 438

php对接建行h5网页支付

php对接建行h5网页支付前言H5页面支付H5页面支付回调通知退款及其他的业务 前言 最近接到公司乙方要求,需要再支付上面增加建行的支付渠道。之前一直用的都是微信和支付宝这种第三方支付平台,银行直接对接比较少。没办法就只能上网搜索,搜索一番返现网上对接建行的资料比较少,没办法只好摸着石头过河,中间也借鉴了一些大佬的文章内容。 H5页面支付 根据建行的h5网页支付对接文档,还是可以看出是比较简单的。...

lihua100862的博客 5411

params.put()和params.add()的区别

两者的主要区别(除了 add() 的字符串仅支持) 是 put() 覆盖先前存在的 param 与现有的键,则 add() 不。

我是一只蘑菇17的博客 649

fMRI脑影像特征提取——Pearson相关与低阶功能连接LOFC(dpabi+nilearn)

一个连接由节点和连接强度(边)组成。功能连接中的节点可以是单个体素,也可以是由脑图谱定义的脑区ROI。通常的做法是将脑图谱中多个或者全部ROI作为节点,或者是将脑图谱中单个ROI与全脑体素作为节点。定义连接强度(边),最常用的方式是Pearson相关,两个节点之间可能没有物理连接,但如果他们的BOLD信号同步性很强,则它们之间可能存在很强的功能上的协作,即很强的功能连接,这是一个很好的表征两个节点间关系的特征。

意疏的学习笔记 2万+

(10)Proteus 8.9上的STM32仿真系列(HAL)---freertos

实验设计:通过cubemxide生成freertos工程,然后创建一个 pc13 led灯间隔200ms闪烁的任务。非常遗憾的是proteus8.9仿真失败,实物上没有问题,但还是把流程过一遍。 一、原理图 实物是淘宝上stm32f103 c8t6的核心板,proteus上没有这个芯片,这里图上大概表示一下,用的是核心板自带的pc13引脚的led灯。 二、cubemxide配置 1.RCC如之前工程一样配置 2、配置SYS timebase Source不要使用systick,自己选择一个定时器,我这

qq_35442550的博客 7344

获取下拉框所有信息,提交信息进行校验

后端代码: /** * 人员JSON * @return */ @RequestMapping(value = "/list",method = RequestMethod.GET) @ResponseBody public String list(HttpServletRequest request) { Long id = getCurrentUserId(); M...

星星点灯 431

基于FaceX-Zoo实现的Face Mask Adding人脸戴口罩

人脸系列文章目录 文章目录人脸系列文章目录前言一、加口罩效果二、实现原理二、代码实现 前言 目前人脸加口罩均为在人脸位置贴图式的,非常不真实。前段时间京东开源一个基于PRnet实现的3D口罩拟合人脸,加口罩非常逼真,但是只是单独一个加口罩demo,没有完整的流程。于是本人结合人脸检测,关键点对齐。PRnet加口罩实现一个完整的加口罩流程。只需一个命令简单方便调用,实现人脸加口罩。 https://github.com/JDAI-CV/FaceX-Zoo 一、加口罩效果 下图可以看出效果还是非常逼真的

zengwb的博客 3390

使用jxl导入和导出excel

前言       使用java来处理excel的读写操作,通常会有两种工具包,一种是jxl,一种是poi。jxl相对poi来说,比较轻量级,api简单一些。我在工作中通常采用jxl来处理excel的导入和导出,并且自己把常用的导入和导出写成了一个小工具类,方便excel的操作。不过有一点需要注意一下,jxl只能处理2003版的excel,也就是.xls结尾的文件,2007版的.xlsx结尾的文件...

yekong1225的专栏 3309

简单初识 Material Point Method(MPM)

简单初识 Material Point Method(MPM):先介绍了Lagrangian和Eulerian,并简单介绍了MPM Method

qq_45776432的博客 7368

Gazebo仿真UUV水下机器人:从环境搭建到避障算法实战

本文详细介绍了使用Gazebo仿真平台进行UUV水下机器人开发的全流程,涵盖从环境搭建、uuv_simulator配置、传感器集成到核心避障算法实战。通过搭建水下仿真实验室并实现基于激光雷达数据的反应式避障算法,为水下机器人算法验证与开发提供了高效、低成本的解决方案。

weixin_28840591的博客 265

jxl导出excel

上一篇讲到freemarker导出excel这篇讲用jxl导出excel  freemarker导出通用性比较高,只需要把freemarker的模板进行替换就可以,jxl则比freemarker相对繁琐一点,但是不会像freemarker一样打开excel提示文件格式不正确,用户体验感高一些;废话不多说 具体实现如下: 1:导入jxl的jar包 同样外部导出jar包或者pom文件加入相应的...

zhoumimi_comeon123的博客 1831

高通MBIM拨号流程简述

高通平台MBIM流程简述

不秃头的程序猿Gabriel的博客 5070

jxl导出Excel文件案例

导入jar包: <dependency> <groupId>net.sourceforge.jexcelapi</groupId> <artifactId>jxl</artifactId> <version>2.6.10</version> </dependency&g...

itboy 843

Django 6.0:有哪些新特性以及它们对实际项目的重要性

Django 6.0 感觉像是一个成熟的版本,它基于实际应用案例而打造。它减少了不必要的复杂性,同时扩展了框架的开箱即用功能。升级到 Django 6.0 不仅仅是为了保持版本更新,更是为了构建更易于维护、更安全、更能适应未来发展的应用程序。

iCloudEnd的博客 341

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

jxl实现导出excel

jxl在web项目中如何实现导出excel处理1.固定模板导出的处理,具体excel模板如下struts2中的对应action 方法代码。/** * 生成个人信息excel的action * @return */ public String printInformgr(){ //以下变量名按照具体模板中对应的(列数行数名命名,具体插入注意行数列数都从0开始的) inform=(Inform)this.publicDao.getObjectByHsql("from Inform

xiashan17的专栏 1967

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

java使用jxl实现excel导入导出

开发过程中经常需要用到数据的导入导出功能,之前用的是POI,这次使用JXL,JXL相对于POI来说要轻量简洁许多,在数据量不大的情况下还是非常实用的。这里做一下使用JXL的学习记录。首先需要导入相应的jar包,pom.xml中添加如下内容即可             &lt;dependency&gt;                     &lt;groupId&gt;net.source...

从放弃到开始 1万+

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

使用JXL实现excel导出

1、需要添加的依赖: <dependency> <groupId>jexcelapi</groupId> <artifactId>jxl</artifactId> <version>2.4.2</version> </dependency> 2、JXLUt...

qq_42794038的博客 311

利用jxl进行对excel导出

1.poi和jxl都可以进行对excel的导出,但两者的差别也是很明显的,读者可以细查 以下介绍jxl到处excel的方式 1.1无表头的方式 需要 OutputStream,List集合注意行和列都是从0开始 public static void createExcel(OutputStream os,             List&lt;Object&gt; object) thro...

杨宇 302

jxl导出Excel

欢迎技术交流。 QQ:138986722 以前是用Poi做Excel的操作、最近一个项目用jxl来做!现在项目完成了、做做总结! 首先呢、需要的当然是jxl的.jar包!---------->jxl.jar 代码:因为这次的导出业务不怎么复杂稍微把对Excel导出的操作提了一下 /** * @author 旦旦而学 * @param file 文件对象 * @param

胡汉三 1万+

JXL导入导出EXCEL

web项目中实现客户端导出Excel文件并下载 JXL下载地址:http://www.andykhan.com/jexcelapi/download.html 下载后,将JXL的包引入到web项目中,在项目中,可以写一个公共的类做为专门导出Excel的工具类。 [java] view plaincopyprint? try{ //打开指定的文件 ...

156

jxl导出excel的具体例子

作为一个刚工作不久的开发者来说,积累自己的代码很有必要。下面是总结jxl的导出excel文件的一个实例: 1、首先需要导入jxl.jar到自己的项目中 try {             //期刊             List plist = Publish.dao.find("select * from publish");             //业务员          

娜年夏天 550

Android 数据导出之Excle jxl.jar

很久没写Android代码了。 使用jxl导出excle数据 首先项目得导入jxl.jar 以下是导出方法: // 导出数据 public void export(ArrayList list) { Calendar c = Calendar.getInstance(); String filename = Integer.toString(c.get(Calendar.YEAR

moo'' 学习笔记 4035

使用jxl导出Excel时加快导出速度的方法

在处理一个导出Excel的功能时,使用了jxl进行导出,现场使用时发现导出特别耗时,大概需要2分钟左右,而且在导出这2分钟内,系统的其他菜单点击时没反应,后台线程CPU使用率50%,经测试,发现是以下两句代码耗时特别长, Workbook wb = Workbook.getWorkbook(in); WritableWorkbook wwb = Workbook.createWorkbook...

shuaijie506 2190

springboot整合jxl导出

注:本案例是写在单元测试中 创建数据库sql /* Navicat MySQL Data Transfer Source Server : 我的连接 Source Server Version : 50717 Source Host : 127.1.1.1:3306 Source Database : mybatis Target Server Type : MYSQL Target Server Version : 50717 File Encod

weixin_44805174的博客 673
上一篇: Spring Boot之定时任务
下一篇: mybatis plus的条件构造器
TARS_
博客等级 码龄6年 5粉丝 43原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值