博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java基础之导出(Excel)
阅读量:6581 次
发布时间:2019-06-24

本文共 16069 字,大约阅读时间需要 53 分钟。

function exportExcel() {    $.messager            .confirm(                    '提示信息',                    '您确定要导出到Excel?',                    function(r) {                        if (r) {                            var columns = $("#queryTmallTcMsgGrid").datagrid(                                    "options").columns[0];                            var stringTitle = "";                            var stringFields = "";                            for (var i = 0; i < columns.length; i++) {                                //导出剔除'联系手机'                                if (columns[i].title == "联系手机") {                                    continue;                                }                                stringTitle = stringTitle + columns[i].title                                        + ",";                                if (columns[i].title == "客户性质") {                                    stringFields = stringFields                                            + "customerNatureName,";                                } else if (columns[i].title == "是否回访") {                                    stringFields = stringFields                                            + "isCallbackName,";                                } else if (columns[i].title == "是否购买") {                                    stringFields = stringFields + "isBuyName,";                                } else if (columns[i].title == "留言来源") {                                    stringFields = stringFields                                            + "messageSourceName,";                                } else if (columns[i].title == "客户类型") {                                    stringFields = stringFields                                            + "userTypeName,";                                } else if (columns[i].title == "分配类型") {                                    stringFields = stringFields                                            + "distributionTypeName,";                                } else {                                    stringFields = stringFields                                            + columns[i].field + ",";                                }                            }                            var queryParams = $("#queryTmallTcMsgGrid")                                    .datagrid("options").queryParams;                            queryParams.gridTitle = stringTitle;                            queryParams.gridField = stringFields;                            queryParams.moduleName = "天猫留言.xls";                            var formObj = $("
") .attr("method", "post") .attr( "action", appPath + "/page/tmallTcMessageAct/exportExcel"); formObj .append( "") .append( "") .append( "") .append( "") .append( "") .append( "") .append( "") .append( "") .append( "") .append( "") .append( "") .append( "") .append( "") .append( "") .append( "") .append( "") .append( "") .append( "") .append("") .append( "") .append( "") .append( "") .append( "") .append( "") .append( "") .append( "") .append( "") .append( "") .append( "") .append( "") //8459修正 /*.append( "")*/ .append( "") .append( "") .append( "") .css('display', 'none').appendTo("body"); formObj.form("load", queryParams); formObj.submit(); formObj.remove(); } });}

 

 

@RequestMapping(value = "/exportExcel")    public String exportExcel(HttpServletRequest request, HttpServletResponse response, TmallTcMessage tmallTcMessage) {        String objList = "";//        Integer userId = (Integer) request.getSession().getAttribute("userId");//        Integer num = systemService.checkName(userId);        Integer strLength = 4;//        if (num != null && num > 0) {//            strLength = 0;//        }        tmallTcMessage.setStrLength(strLength);        try {            objList = exportService.exportExcelFile(request,                response,                tmallTcMessage,                "TmallTcMessage.TmallMessageExportExcel");        } catch (Exception e) {            logger.debug(e.getMessage());            e.printStackTrace();        }        return objList;    }=============================================基类==========================================================================    public String exportExcelFile(HttpServletRequest request,HttpServletResponse response,            Object obj, String sqlMap) throws Exception {        String fileChName = request.getParameter("moduleName");        String gridTitle = request.getParameter("gridTitle");        String gridField = request.getParameter("gridField");        response.reset();        response.setCharacterEncoding("ISO8859-1");        response.setHeader("Content-Disposition", "attachment; filename="+new String(fileChName.getBytes("GBK"), "ISO8859-1"));//fileChName为下载时用户看到的文件名        response.setHeader("Connection", "close");        response.setHeader("Content-Type", "application/octet-stream");        OutputStream out = response.getOutputStream();                        Map map = new HashMap();        List paymentInfoList = commonDao.queryForList(sqlMap, obj);                if(paymentInfoList.size()>10000){            WritableWorkbook wbook = Workbook.createWorkbook(out);//直接写入内存,不要存放到硬盘中            jxl.write.WritableSheet wsheet = wbook.createSheet("Sheet1", 0);//定义sheet的名称            jxl.write.WritableFont wfont = null; // 字体            jxl.write.WritableCellFormat wcfFC = null; // 字体格式            jxl.write.Label wlabelString = null; // Excel表格的Cell,文本格式            for(int i=0;i<1;i++){                wsheet.setColumnView(i, 40);//设置列宽            }            // 设置excel标题字体            wfont = new jxl.write.WritableFont(WritableFont.ARIAL, 14, WritableFont.BOLD,                false, jxl.format.UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);            wcfFC = new jxl.write.WritableCellFormat(wfont);            wcfFC.setAlignment(jxl.format.Alignment.LEFT); // 设置对齐方式            // 添加excel标题            jxl.write.Label wlabel1 = new jxl.write.Label(5, 0,"导出提示",wcfFC);            wsheet.addCell(wlabel1);           // 设置正文字体          wfont = new jxl.write.WritableFont(WritableFont.TIMES, 12, WritableFont.NO_BOLD,            false, jxl.format.UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);          wcfFC = new jxl.write.WritableCellFormat(wfont);          wcfFC.setAlignment(jxl.format.Alignment.LEFT); // 设置对齐方式         //往Excel输出数据          wlabelString = new jxl.write.Label(5, 1, "导出数据超过一万条,系统不支持,请分批导出",wcfFC);          wsheet.addCell(wlabelString);          wbook.write(); // 写入文件          wbook.close();          out.flush();          out.close();          return "ok";        }                String[] title = gridTitle.split(",");        String[] propery = gridField.split(",");        Integer titleNum = title.length;//Excel中字段的个数        String FileTitle = "";//Excel中正文标题                //--建立EXCEL索引、字段名、字段值的关系,存放到map中        for(int i=0;i

 

==========================================================js=======================================================

 

//导出function exportExcel(){    $.messager.confirm('提示信息','您确定要导出到Excel?',function(r){        if(r){            var columns = $("#linksCommonGrid").datagrid("options").columns[0];                         //--------把标题grid标题和grid的field,拼接成字符串-----------            var stringTitle = "";            var stringFields = "";            for(var i = 1; i < columns.length; i++){                stringTitle = stringTitle + columns[i].title + ",";                stringFields = stringFields + columns[i].field + ",";            }            if(stringTitle.lastIndexOf(",") == (stringTitle.length-1)){                stringTitle = stringTitle.substring(0, stringTitle.lastIndexOf(","));                }            if(stringFields.lastIndexOf(",") == (stringFields.length-1)){                stringFields = stringFields.substring(0, stringFields.lastIndexOf(","));                }                        var queryParams = $("#linksCommonGrid").datagrid("options").queryParams;            queryParams.gridTitle = stringTitle;            queryParams.gridField = stringFields;            queryParams.moduleName = "友情链接.xls";                        var formObj = $("
").attr("method","post").attr("action",appPath+"/page/pageIndexMrgAct/exportLinksList"); formObj.append("") .append("") .append("") .append("") .append("") .append("") .append("") .append("") .append("") .append("") .css('display','none') .appendTo("body"); formObj.form("load",queryParams); formObj.submit(); formObj.remove(); } });}

 

 

=====================================================java=======================================================

 

        

/**             * 导出excel             *              * @param request             * @param response             * @param website             */            @RequestMapping(value = "/exportLinksList")            @ResponseBody            public String exportLinksList(HttpServletRequest request,                    HttpServletResponse response, PageIndexLinks pageIndexLinks) {            try {                    List LinksList=null;                    ServiceMessage
> res = pageIndexLinksService.getLinksListForExport(pageIndexLinks); if(res.getStatus().getCode().equals("0")){ LinksList=res.getResult(); if(LinksList!=null && LinksList.size()>0){ Map
paramMap=new HashMap
(); String fileChName = request.getParameter("moduleName"); String gridTitle = request.getParameter("gridTitle"); String gridField = request.getParameter("gridField"); paramMap.put("moduleName", fileChName); paramMap.put("gridTitle", gridTitle); paramMap.put("gridField", gridField); return exportService.exportExcelFileSEO(response, paramMap, LinksList); } } } catch (Exception e) { e.printStackTrace(); } return null; }

            =========================基类===========================

/**     * SEO daochu Excel     */            public String exportExcelFileSEO(HttpServletResponse response, Map
paramMap, List
objList)throws Exception { String fileChName = paramMap.get("moduleName"); String gridTitle = paramMap.get("gridTitle"); String gridField = paramMap.get("gridField"); response.reset(); response.setCharacterEncoding("ISO8859-1"); response.setHeader("Content-Disposition", "attachment; filename="+new String(fileChName.getBytes("GBK"), "ISO8859-1"));//fileChName为下载时用户看到的文件名 response.setHeader("Connection", "close"); response.setHeader("Content-Type", "application/octet-stream"); OutputStream out = response.getOutputStream(); Map map = new HashMap(); if(objList.size()>10000){ WritableWorkbook wbook = Workbook.createWorkbook(out);//直接写入内存,不要存放到硬盘中 jxl.write.WritableSheet wsheet = wbook.createSheet("Sheet1", 0);//定义sheet的名称 jxl.write.WritableFont wfont = null; // 字体 jxl.write.WritableCellFormat wcfFC = null; // 字体格式 jxl.write.Label wlabelString = null; // Excel表格的Cell,文本格式 for(int i=0;i<1;i++){ wsheet.setColumnView(i, 40);//设置列宽 } // 设置excel标题字体 wfont = new jxl.write.WritableFont(WritableFont.ARIAL, 14, WritableFont.BOLD, false, jxl.format.UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK); wcfFC = new jxl.write.WritableCellFormat(wfont); wcfFC.setAlignment(jxl.format.Alignment.LEFT); // 设置对齐方式 // 添加excel标题 jxl.write.Label wlabel1 = new jxl.write.Label(5, 0,"导出提示",wcfFC); wsheet.addCell(wlabel1); // 设置正文字体 wfont = new jxl.write.WritableFont(WritableFont.TIMES, 12, WritableFont.NO_BOLD, false, jxl.format.UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK); wcfFC = new jxl.write.WritableCellFormat(wfont); wcfFC.setAlignment(jxl.format.Alignment.LEFT); // 设置对齐方式 //往Excel输出数据 wlabelString = new jxl.write.Label(5, 1, "导出数据超过一万条,系统不支持,请分批导出",wcfFC); wsheet.addCell(wlabelString); wbook.write(); // 写入文件 wbook.close(); out.flush(); out.close(); return "ok"; } String[] title = gridTitle.split(","); String[] propery = gridField.split(","); Integer titleNum = title.length;//Excel中字段的个数 String FileTitle = "";//Excel中正文标题 //--建立EXCEL索引、字段名、字段值的关系,存放到map中 for(int i=0;i

 

转载地址:http://wuino.baihongyu.com/

你可能感兴趣的文章
设置分录行按钮监听事件
查看>>
C Primer Plus 第5章 运算符、表达式和语句 5.2基本运算符
查看>>
java并发库之Executors常用的创建ExecutorService的几个方法说明
查看>>
23种设计模式(1):单例模式
查看>>
socket 编程入门教程(五)UDP原理:4、“有连接”的UDP
查看>>
Jquery获取iframe中的元素
查看>>
Laravel 学习笔记5.3之 Query Builder 源码解析(下)
查看>>
Struts2简单入门实例
查看>>
2012CSDN年度博客之星评选http://vote.blog.csdn.net/item/blogstar/xyz_lmn
查看>>
BZOJ 4037 [HAOI2015]数字串拆分 ——动态规划
查看>>
SpringBoot实战总汇--详解
查看>>
2018年7月1日笔记
查看>>
尝试使用iReport4.7(基于Ubuntu Desktop 12.04 LTS)
查看>>
动态规划:金矿模型
查看>>
子元素应该margin-top为何会影响父元素【转】
查看>>
AJAX 状态值(readyState)与状态码(status)详解
查看>>
BZOJ3668:[NOI2014]起床困难综合症(贪心)
查看>>
LightOJ 1245(Harmonic Number (II))
查看>>
小知识记录
查看>>
css3 animate 和关键帧 @-webkit-keyframes
查看>>