slickgrid ( nsunleo-slickgrid ) 7 导出EXCEL

  • 导出Excel 是通过 excel-gen进行导出的
  • 扩展了excel-gen,可以通过指定数据进行导出
  • 导出的时候,如果数据中有特殊字符串,如"&"等, 导出失败,因此在导出前,进行了转义,转义代码如下:
        function htmlEncode(value) {
            var returnValue;
            if (value == null) {
                return null;
            }
 
            if (!value.replace) {
                return value;
            }
 
            returnValue = value.replace(/&/g, '&');
            returnValue = returnValue.replace(/</g, '&lt;');
            returnValue = returnValue.replace(/>/g, '&gt;');
 
            returnValue = returnValue.replace(/\n\n/g, '<br/>');
            returnValue = returnValue.replace(/\r\r/g, '<br/>');
            returnValue = returnValue.replace(/\n/g, '<br/>');
            returnValue = returnValue.replace(/\r/g, '<br/>');
            returnValue = returnValue.replace(/\t/g, '&nbsp;');
            return returnValue;
        }
  • 由于有树的存在,导出的时候需要指定除显示列之外的数据,因此扩展了导出配置
cfg:{formatter:function(data,field),ignoreCols:[],insert:[{index:0,field:"__path",title:"zzz"} ]}
  • 导出支持忽略列、插入列和格式化数据监听事件,以支持不同的导出扩展,示例:
 $("#excel").bind("click", function () {
        grid.excel({
            formatter: function (data, field) {
                return data[field];
            }, insert: [{
                index: 0, field: "__path", name: "层级"
            }]
        });
    });
 

posted @ 2021-05-21 22:22  猿语  阅读(154)  评论(0)    收藏  举报