追风小马仔2016-12-31 16:53:10
关于tableExport导出excel的中文乱码的demo
最近项目中用到tableExport这个插件,遇到中文乱码的问题。查找了好多资料,并完美解决:再次贡献源码,希望更多地人受益。
把从官网下载的tableExport.js文件替换成以下的代码,即可成功!
(function($){ $.fn.extend({ tableExport: function(options) { var defaults = { separator: ',', ignoreColumn: [], tableName:'yourTableName', type:'csv', pdfFontSize:14, pdfLeftMargin:20, escape:'true', htmlContent:'false', consoleLog:'false' }; var options = $.extend(defaults, options); var el = this; if(defaults.type == 'csv' || defaults.type == 'txt'){ // Header var tdData =""; $(el).find('thead').find('tr').each(function() { tdData += "\n"; $(this).filter(':visible').find('th').each(function(index,data) { if ($(this).css('display') != 'none'){ if(defaults.ignoreColumn.indexOf(index) == -1){ tdData += '"' + parseString($(this)) + '"' + defaults.separator; } } }); tdData = $.trim(tdData); tdData = $.trim(tdData).substring(0, tdData.length -1); }); // Row vs Column $(el).find('tbody').find('tr').each(function() { tdData += "\n"; $(this).filter(':visible').find('td').each(function(index,data) { if ($(this).css('display') != 'none'){ if(defaults.ignoreColumn.indexOf(index) == -1){ tdData += '"'+ parseString($(this)) + '"'+ defaults.separator; } } }); //tdData = $.trim(tdData); tdData = $.trim(tdData).substring(0, tdData.length -1); }); //output if(defaults.consoleLog == 'true'){ console.log(tdData); } var base64data = "base64," + $.base64.encode(tdData); window.open('data:application/'+defaults.type+';filename=exportData;' + base64data); }else if(defaults.type == 'sql'){ // Header var tdData ="INSERT INTO `"+defaults.tableName+"` ("; $(el).find('thead').find('tr').each(function() { $(this).filter(':visible').find('th').each(function(index,data) { if ($(this).css('display') != 'none'){ if(defaults.ignoreColumn.indexOf(index) == -1){ tdData += '`' + parseString($(this)) + '`,' ; } } }); tdData = $.trim(tdData); tdData = $.trim(tdData).substring(0, tdData.length -1); }); tdData += ") VALUES "; // Row vs Column $(el).find('tbody').find('tr').each(function() { tdData += "("; $(this).filter(':visible').find('td').each(function(index,data) { if ($(this).css('display') != 'none'){ if(defaults.ignoreColumn.indexOf(index) == -1){ tdData += '"'+ parseString($(this)) + '",'; } } }); tdData = $.trim(tdData).substring(0, tdData.length -1); tdData += "),"; }); tdData = $.trim(tdData).substring(0, tdData.length -1); tdData += ";"; //output //console.log(tdData); if(defaults.consoleLog == 'true'){ console.log(tdData); } var base64data = "base64," + $.base64.encode(tdData); window.open('data:application/sql;filename=exportData;' + base64data); }else if(defaults.type == 'json'){ var jsonHeaderArray = []; $(el).find('thead').find('tr').each(function() { var tdData =""; var jsonArrayTd = []; $(this).filter(':visible').find('th').each(function(index,data) { if ($(this).css('display') != 'none'){ if(defaults.ignoreColumn.indexOf(index) == -1){ jsonArrayTd.push(parseString($(this))); } } }); jsonHeaderArray.push(jsonArrayTd); }); var jsonArray = []; $(el).find('tbody').find('tr').each(function() { var tdData =""; var jsonArrayTd = []; $(this).filter(':visible').find('td').each(function(index,data) { if ($(this).css('display') != 'none'){ if(defaults.ignoreColumn.indexOf(index) == -1){ jsonArrayTd.push(parseString($(this))); } } }); jsonArray.push(jsonArrayTd); }); var jsonExportArray =[]; jsonExportArray.push({header:jsonHeaderArray,data:jsonArray}); //Return as JSON //console.log(JSON.stringify(jsonExportArray)); //Return as Array //console.log(jsonExportArray); if(defaults.consoleLog == 'true'){ console.log(JSON.stringify(jsonExportArray)); } var base64data = "base64," + $.base64.encode(JSON.stringify(jsonExportArray)); window.open('data:application/json;filename=exportData;' + base64data); }else if(defaults.type == 'xml'){ var xml = '<?xml version="1.0" encoding="utf-8"?>'; xml += '<tabledata><fields>'; // Header $(el).find('thead').find('tr').each(function() { $(this).filter(':visible').find('th').each(function(index,data) { if ($(this).css('display') != 'none'){ if(defaults.ignoreColumn.indexOf(index) == -1){ xml += "<field>" + parseString($(this)) + "</field>"; } } }); }); xml += '</fields><data>'; // Row Vs Column var rowCount=1; $(el).find('tbody').find('tr').each(function() { xml += '<row id="'+rowCount+'">'; var colCount=0; $(this).filter(':visible').find('td').each(function(index,data) { if ($(this).css('display') != 'none'){ if(defaults.ignoreColumn.indexOf(index) == -1){ xml += "<column-"+colCount+">"+parseString($(this))+"</column-"+colCount+">"; } } colCount++; }); rowCount++; xml += '</row>'; }); xml += '</data></tabledata>' if(defaults.consoleLog == 'true'){ console.log(xml); } var base64data = "base64," + $.base64.encode(xml); window.open('data:application/xml;filename=exportData;' + base64data); }else if(defaults.type == 'excel' || defaults.type == 'doc'|| defaults.type == 'powerpoint' ){ var excel="<table>"; // Header $(el).find('thead').find('tr').each(function() { excel += "<tr>"; // $(this).filter(':visible') $(this).find('th').each(function(index,data) { if ($(this).css('display') != 'none'){ if(defaults.ignoreColumn.indexOf(index) == -1){ var colspan = $(this).attr('colspan'); if(!colspan) colspan = 1; excel += "<td style='text-align: center; vertical-align: middle;' colspan='" + colspan + "'>" + parseString($(this))+ "</td>"; } } }); // $(this).filter(':visible') $(this).find('td').each(function(index,data) { if ($(this).css('display') != 'none'){ if(defaults.ignoreColumn.indexOf(index) == -1){ var colspan = $(this).attr('colspan'); var rowspan = $(this).attr('rowspan'); if(!colspan) colspan = 1; if(!rowspan) rowspan = 1; excel += "<td style='text-align:center;' colspan='" + colspan + "' rowspan='" + rowspan + "'>" + parseString($(this))+ "</td>"; } } }); excel += '</tr>'; }); // Row Vs Column var rowCount=1; $(el).find('tbody').find('tr').each(function() { excel += "<tr>"; var colCount=0; // $(this).filter(':visible') $(this).find('td').each(function(index,data) { if ($(this).css('display') != 'none'){ if(defaults.ignoreColumn.indexOf(index) == -1){ var rowspan = $(this).attr('rowspan'); if(!rowspan) rowspan = 1; excel += "<td rowspan='" + rowspan + "' style='color:" + $(this).css('color') + "'>"+parseString($(this))+"</td>"; } } colCount++; }); rowCount++; excel += '</tr>'; }); excel += '</table>' if(defaults.consoleLog == 'true'){ console.log(excel); } var excelFile = "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:"+defaults.type+"' xmlns='http://www.w3.org/TR/REC-html40'>"; excelFile += "<head>"; excelFile += "<!--[if gte mso 9]>"; excelFile += "<xml>"; excelFile += "<x:ExcelWorkbook>"; excelFile += "<x:ExcelWorksheets>"; excelFile += "<x:ExcelWorksheet>"; excelFile += "<x:Name>"; excelFile += "{worksheet}"; excelFile += "</x:Name>"; excelFile += "<x:WorksheetOptions>"; excelFile += "<x:DisplayGridlines/>"; excelFile += "</x:WorksheetOptions>"; excelFile += "</x:ExcelWorksheet>"; excelFile += "</x:ExcelWorksheets>"; excelFile += "</x:ExcelWorkbook>"; excelFile += "</xml>"; excelFile += "<![endif]-->"; excelFile += "</head>"; excelFile += "<body>"; excelFile += excel; excelFile += "</body>"; excelFile += "</html>"; var base64data = "base64," + $.base64({ data: excelFile, type: 0 }); //window.open('data:application/vnd.ms-'+defaults.type+';filename=exportData.doc;' + base64data); $('<a style="display:none" href="data:application/vnd.ms-'+defaults.type+';filename=exportData.doc;'+base64data+'" download="'+defaults.tableName.toString()+'.xls"><span></span></a>').appendTo(document.body).find('span').trigger("click").parent().remove(); }else if(defaults.type == 'png'){ html2canvas($(el), { onrendered: function(canvas) { var img = canvas.toDataURL("image/png"); window.open(img); } }); }else if(defaults.type == 'pdf'){ var doc = new jsPDF('p','pt', 'a4', true); doc.setFontSize(defaults.pdfFontSize); // Header var startColPosition=defaults.pdfLeftMargin; $(el).find('thead').find('tr').each(function() { $(this).filter(':visible').find('th').each(function(index,data) { if ($(this).css('display') != 'none'){ if(defaults.ignoreColumn.indexOf(index) == -1){ var colPosition = startColPosition+ (index * 50); doc.text(colPosition,20, parseString($(this))); } } }); }); // Row Vs Column var startRowPosition = 20; var page =1;var rowPosition=0; $(el).find('tbody').find('tr').each(function(index,data) { rowCalc = index+1; if (rowCalc % 26 == 0){ doc.addPage(); page++; startRowPosition=startRowPosition+10; } rowPosition=(startRowPosition + (rowCalc * 10)) - ((page -1) * 280); $(this).filter(':visible').find('td').each(function(index,data) { if ($(this).css('display') != 'none'){ if(defaults.ignoreColumn.indexOf(index) == -1){ var colPosition = startColPosition+ (index * 50); doc.text(colPosition,rowPosition, parseString($(this))); } } }); }); // Output as Data URI doc.output('datauri'); } function parseString(data){ if(defaults.htmlContent == 'true'){ content_data = data.html().trim(); }else{ content_data = data.text().trim(); } if(defaults.escape == 'true'){ content_data = escape(content_data); } return content_data; } } }); })(jQuery);
猜你喜欢
- eXtremeComponents组件应用
- java 导出成EXCEL或XML
- struts2 jxl 导出excel
- 利用poi.jar导入导出EXCEL
- struts2 poi导出excel实例代码下载
- java 通过Apache poi导出excel代码demo实例
- java poi 导出excel表格
- java web项目中通过apache poi将本地excel文件导入到系统中并打印到控制台
- jxls2.4版本多sheet模板内容导出示例
- java实现一个配置简单功能强大的excel工具类,搞定大多数excel文件导入导出
- nodejs实现从mysql数据库中导出excel报表,支持按日期导出
- java通过apache poi导出excel报表(综合运用单元格合并,公式,颜色设定等)
请下载代码后再发表评论
- 证HTML5+jQuery制作温馨浪漫爱心表白动画特效
- 精Apache Shiro+SpringMVC+Hibernate Search+Hibernate+Bootstrap企业信息管理系统基础框架搭建整合实例代码教程
- 证精spring MVC+easyUI+mybatis开发网站后台管理系统源代码下载
- 一套EASYUI的经典后台管理模板
- 证精java开源CMS管理系统jeetemp
- 精html网站内容管理系统后台模板源代码下载,花钱买的模板。
- 原精spring mvc+spring+mybatis+shiro+easyui整合开发后台用户权限管理系统
- 一款漂亮的轻量级bootstrap中文后台管理系统模板ace
- js框架jquery实现幸运大转盘抽奖程序代码,兼容多种浏览器
- 证HTML5音乐播放器效果非常漂亮
- 证精企业人力资源管理项目SSH+EXTJS+MySQL整合开发
- 证jsp+servlet+mysql员工管理系统源代码下载
- 证精java开源CMS管理系统jeetemp
- java牛官方想开发一个全部开源的系统
- 证精企业人力资源管理项目SSH+EXTJS+MySQL整合开发
- 精基于springMVC+springSecurity3.x+Mybaits3.x的权限系统,,开放源码,支持开源
- 证精spring MVC+easyUI+mybatis开发网站后台管理系统源代码下载
- 【猪猪-前端】基于HTML5 Bootstrap搭建的后台模板charisma,分页,模糊查询已经全部JS实现,无需编码,嵌入数据即可开发,内置8款皮肤,欧美风格,非常好用!
- 精Apache Shiro+SpringMVC+Hibernate Search+Hibernate+Bootstrap企业信息管理系统基础框架搭建整合实例代码教程
- 证精springmvc+hibernate+jbpm开发OA自动化办公后台管理系统源码下载
- 证精struts2.3+hibernate4.1+spring3.2+EasyUI1.36整合实现的java后台管理系统
- 证HTML5+jQuery制作温馨浪漫爱心表白动画特效
- 证精spring+spring mvc+mybatis+bootstrap框架整合搭建ssm完整项目
- 证精年末最代码部分源码大出血分享-freemarker,bootstrap,springdata jpa分页代码
- 原S2SH开发的企业办公OA管理系统
- 原证SSM+MySQL开发java CRM客户关系管理系统
- 原证jsp+servlet开发java web图书后台管理系统
- 证Spring+JMS+ActiveMQ+Tomcat整合项目实例
- 证java通过授权码机制调用腾讯邮箱smtp服务发送邮件
- 原证html5故宫展评展示页面
- 原fastjson实现json比较
- 原证精ssm学生宿舍管理系统,maven多模块搭建,实现用户分角色登录+分页等技术
- 原证ssm开发实现简单的oa办公管理系统,maven搭建
- 原ssh实现简单的论坛帖子增删改查,分页,回复帖子的项目实例
- 原java servlet调用百度AI开放平台动物识别API和百度百科API实现动物识别的实例
- java编写一个词法分析器,输入算术表达式计算该字符串中的所有运算数和运算符



安安an前天
牛牛大鳄4月23日
暂无贡献等级
RuoYi_4月22日
lichangliu10984月20日
Arshia4月12日
暂无贡献等级
lszloz4月8日
暂无贡献等级
187390563534月2日
暂无贡献等级
4681374104月1日
暂无贡献等级
yyf5073月29日
26022753483月20日
暂无贡献等级