先上代码javascript
-
<script type=
"text/javascript" language="javascript">
-
-
-
-
var explorer = window.navigator.userAgent;
-
-
if (explorer.indexOf("MSIE") >= 0) {
-
-
-
-
else if (explorer.indexOf("Firefox") >= 0) {
-
-
-
-
else if (explorer.indexOf("Chrome") >= 0) {
-
-
-
-
else if (explorer.indexOf("Opera") >= 0) {
-
-
-
-
else if (explorer.indexOf("Safari") >= 0) {
-
-
-
-
-
function method1(tableid,name="1.xlsx") { //整个表格拷贝到EXCEL中
-
if (getExplorer() == 'ie') {
-
var curTbl = document.getElementById(tableid);
-
var oXL = new ActiveXObject("Excel.Application");
-
-
-
var oWB = oXL.Workbooks.Add();
-
-
var xlsheet = oWB.Worksheets(1);
-
-
var sel = document.body.createTextRange();
-
sel.moveToElementText(curTbl);
-
-
-
-
sel.execCommand(
"Copy");
-
-
-
-
-
-
-
-
var fname = oXL.Application.GetSaveAsFilename("Excel.xls", "Excel Spreadsheets (*.xls), *.xls");
-
-
print(
"Nested catch caught " + e);
-
-
-
-
oWB.Close(savechanges =
false);
-
-
-
-
-
//window.setInterval("Cleanup();",1);
-
idTmr =
window.setInterval("Cleanup();", 1);
-
-
-
-
-
tableToExcel(tableid,name)
-
-
-
-
-
window.clearInterval(idTmr);
-
-
-
var tableToExcel = (function () {
-
var uri = 'data:application/vnd.ms-excel;base64,',
-
-
'<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>',
-
-
return window.btoa(unescape(encodeURIComponent(s)))
-
-
format =
function (s, c) {
-
return s.replace(/{(\w+)}/g,
-
-
-
-
-
return function (table, name) {
-
-
if (!table.nodeType) table = document.getElementById(table)
-
-
worksheet: name || 'Worksheet',
-
-
-
-
window.location.href = uri + base64(format(template, ctx))
-
-
-
-
$(
'#export').click(function () {
-
-
$('#export').attr('href',uri + base64(format(template, ctx))) //解决文件无扩展名的问题
转载自http://blog.csdn.net/sinat_15114467/article/details/51098522php
github也有写好的插件:css
-
jQuery table2excel - v1
.1.1
-
* jQuery plugin to
export an .xls file in browser from an HTML table
-
* https:
//github.com/rainabba/jquery-table2excel
这个地址也是相关的介绍https://segmentfault.com/a/1190000000336643html
个人html实例html5
-
-
-
-
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
<input type="text" class="startTm">
-
<input type="text" class="endTm">
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
<a href="javascript:void(0)" class="prev">上一页</a>
-
<a href="javascript:void(0)" class="next">下一页</a>
-
-
-
<a href="javascript:void(0)" id="export" download="对话记录.xlsx">导出对话记录</a>
-
-
<h6><span></span>的对话</h6>
-
<a href="javascript:void(0)" class="close">X</a>
-
<ul class="contentKey clearfix">
-
-
客服:
<span>dfsdfsfs</span>
-
-
<li class="wait_in_secs">
-
-
-
-
-
-
<li class="visitor_location">
-
-
-
<li class="search_engine_kw">
-
搜索关键词:
<span>sdfsfsfsdfsfsfsgsdsg</span>
-
-
<li class="conv_start_tm">
-
对话开始时间:
<span>sdfsfsfsdfsfsfsgsdsg</span>
-
-
-
对话结束时间:
<span>sdfsfsfsdfsfsfsgsdsg</span>
-
-
-
<div class="mainCon clearfix">
-
-
-
-
<script src="js/jquery-1.11.3.min.js"></script>
-
-
-
-
-
-
-
-
<th class="kefu">客服</th>\
-
-
-
-
-
-
-
$(
document).ready(function () {
-
var pageAll='';//计算总数时用的参数
-
-
var limit = 20; //每页显示的数目
-
var offset = pagesize * limit; //跳过的数目
-
var startTm = $('.startTm').val();
-
var endTm = $('.endTm').val();
-
-
var startTm = getFormatDate('ymd');
-
var endTm = getFormatDate('ymd');
-
$(
'.startTm').val(getFormatDate('ymd'));
-
$(
'.endTm').val(getFormatDate('ymd'));
-
-
-
-
-
-
-
-
-
-
-
-
-
success: function (data) {
-
-
var newJson = JSON.parse(data);
-
if (newJson.result.length < 20) {
-
$(
'.page span').text("已是最后一页");
-
}
else if (newJson.result.length <= 0) {
-
$(
'.page span').text("已是最后一页");
-
-
-
var Nowpage = pagesize + 1;
-
$(
'.page span').text("当前是第" + Nowpage + "页");
-
-
// addContent(newJson.result)
-
tableList+=addContent(newJson.result);
-
$(
'#table').append(addContent(newJson.result));
-
-
-
-
-
-
-
-
success: function (data) {
-
var newJson = JSON.parse(data);
-
tableList+=addContent(newJson.result);
-
if (newJson.result.length < 20 || newJson.result.length <= 0) {
-
// console.log(tableList)
-
var dateN=(new Date()).toLocaleString();
-
-
-
method1(tableList,dateN);
-
-
-
-
-
-
-
-
-
-
-
var offset = pageAll * limit; //跳过的数目
-
var startTm = $('.startTm').val();
-
var endTm = $('.endTm').val();
-
-
-
-
-
-
-
-
-
-
function addContent(rs) {
-
-
-
-
for (var i = 0; i < length; i++) {
-
-
-
arr.push(
'<td>' + i + '</td>');
-
arr.push(
'<td>' + rs[i].conv_start_tm + '</td>');
-
arr.push(
'<td>' + rs[i].conv_end_tm + '</td>');
-
arr.push(
'<td>' + rs[i].client_info.visitor_name + '</td>');
-
arr.push(
'<td>' + rs[i].search_engine_kw + '</td>');
-
arr.push(
'<td>' + rs[i].agent_name + '</td>');
-
arr.push(
'<td>' + rs[i].wait_in_secs + '</td>');
-
arr.push(
'<td>' + rs[i].conv_visitor_msg_count + '</td>');
-
if(rs[i].conv_visitor_msg_count>0){
-
-
let conLen = rs[i].conv_content.length;
-
for(let j=0;j<conLen;j++){
-
con+=rs[i].conv_content[j].from+rs[i].conv_content[j].timestamp+rs[i].conv_content[j].content;
-
-
arr.push(
'<td class="convId">' + con + '<span>' + rs[i].conv_id + '</span></td>');
-
-
arr.push(
'<td class="convId">' + '点击显示内容<span>' + rs[i].conv_id + '</span></td>');
-
-
arr.push(
'<td>' + rs[i].visitor_location + '</td>');
-
arr.push(
'<td>'+rs[i].visitor_ip+'</td>');
-
-
-
// tableList+=arr.join('')
-
// $('#table').append(arr.join(''));
-
-
-
$(
'.prev').click(function () {
-
$(
"#table tr").not($("#table tr:first")).remove();
-
pagesize = (--pagesize <
0) ? 0 : pagesize;
-
offset = pagesize * limit;
//跳过的数目
-
startTm = $(
'.startTm').val();
-
endTm = $(
'.endTm').val();
-
-
-
-
-
-
-
-
$(
'.next').click(function () {
-
$(
"#table tr").not($("#table tr:first")).remove();
-
if ($('.page span').text() == '已是最后一页') {
-
-
-
pagesize = (++pagesize <
0) ? 0 : pagesize;
-
-
var offset = pagesize * limit; //跳过的数目
-
var startTm = $('.startTm').val();
-
var endTm = $('.endTm').val();
-
-
-
-
-
-
-
-
-
$(
'#choose button').click(function () {
-
-
var startTm = $('.startTm').val();
-
var endTm = $('.endTm').val();
-
-
-
-
-
-
$(
"#table tr").not($("#table tr:first")).remove();
-
-
-
-
-
* @param param string 肯定时间的显示格式 'ymd' => 年-月-日
-
-
* @param num num +1表明后一天,-1表明前一天
-
-
-
function getFormatDate(param, num = 0) {
-
-
-
-
-
var y = date.getFullYear();
-
var m = date.getMonth() + 1;
-
var d = date.getDate() + num;
-
-
var i = date.getMinutes();
-
var s = date.getSeconds();
-
-
-
-
-
-
-
-
-
var currentdate = y + seperator1 + m + seperator1 + d;
-
-
var currentdate = y + seperator1 + m + seperator1 + d +
-
seperator3 + h + seperator2 + i +
-
-
-
-
-
-
$(
document).on('click', '.convId', function () {
-
var convId = $(this).find('span').text();
-
-
-
-
-
-
-
success: function (data) {
-
var newJson = (new Function("", "return " + data))(data).result;
-
-
$(
'.convBox h6 span').text(convId);
-
$(
'.convBox .agent_name span').text(newJson.agent_name);
-
$(
'.convBox .wait_in_secs span').text(newJson.wait_in_secs);
-
$(
'.convBox .visitor_ip span').text(newJson.visitor_ip);
-
$(
'.convBox .visitor_location span').text(newJson.visitor_location);
-
$(
'.convBox .search_engine_kw span').text(newJson.search_engine_kw);
-
$(
'.convBox .conv_end_tm span').text(newJson.conv_end_tm);
-
$(
'.convBox .conv_start_tm span').text(newJson.conv_start_tm);
-
$(
'.convBox .visitor_location span').text(newJson.visitor_location);
-
let convLen = newJson.conv_content.length;
-
-
-
for (let j = 0; j < convLen; j++) {
-
// conv_content.push(111)
-
conv_content.push(
'<div class="' + newJson.conv_content[j].from +
-
'"><p>' + newJson.conv_content[j].timestamp + '</p>' +
-
newJson.conv_content[j].content +
'</div>');
-
-
// console.log(conv_content)
-
$(
'.convBox .mainCon').html(conv_content.join(''));
-
-
-
-
-
$(
document).on('click', '.convBox .close', function () {
-
-
-
-
$(
'#export').click(function () {
-
if ($('.page span').text() == '已是最后一页') {
-
// method1(tableList,'聊天数据');
-
method1(tableList,
'1.xlsx');
-
-
-
-
-
-
// method1(allrecord(),'美洽对话');
-
-
-
-
<script type="text/javascript" language="javascript">
-
-
-
-
var explorer = window.navigator.userAgent;
-
-
if (explorer.indexOf("MSIE") >= 0) {
-
-
-
-
else if (explorer.indexOf("Firefox") >= 0) {
-
-
-
-
else if (explorer.indexOf("Chrome") >= 0) {
-
-
-
-
else if (explorer.indexOf("Opera") >= 0) {
-
-
-
-
else if (explorer.indexOf("Safari") >= 0) {
-
-
-
-
-
function method1(tableid,name="1.xlsx") { //整个表格拷贝到EXCEL中
-
if (getExplorer() == 'ie') {
-
var curTbl = document.getElementById(tableid);
-
var oXL = new ActiveXObject("Excel.Application");
-
-
-
var oWB = oXL.Workbooks.Add();
-
-
var xlsheet = oWB.Worksheets(1);
-
-
var sel = document.body.createTextRange();
-
sel.moveToElementText(curTbl);
-
-
-
-
sel.execCommand(
"Copy");
-
-
-
-
-
-
-
-
var fname = oXL.Application.GetSaveAsFilename("Excel.xls", "Excel Spreadsheets (*.xls), *.xls");
-
-
print(
"Nested catch caught " + e);
-
-
-
oWB.Close(savechanges =
false);
-
-
-
-
-
//window.setInterval("Cleanup();",1);
-
idTmr =
window.setInterval("Cleanup();", 1);
-
-
-
-
-
tableToExcel(tableid,name)
-
-
-
-
-
window.clearInterval(idTmr);
-
-
-
var tableToExcel = (function() {
-
var uri = 'data:application/vnd.ms-excel;base64,', template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>'
-
-
return window.btoa(unescape(encodeURIComponent(s)))
-
-
, format =
function(s, c) {
-
return s.replace(/{(\w+)}/g, function(m, p) {
-
-
-
-
return function(table, name) {
-
var ctx = {worksheet:name , table:table}
-
// console.log(uri + base64(format(template, ctx)))
-
-
// $('#export').attr('href',uri + base64(format(template, ctx)))
-
window.location.href = uri + base64(format(template, ctx));
-
-
-
-
-
-
https://blog.csdn.net/qiphon3650/article/details/77921087
java
如今各大浏览器基本都支持data协议,因此咱们可使用该协议去将网页中的table转化为excel下载下来node
- 对html 进行base64编码处理
- 编码后的html内容增长前缀 data:application/vnd.ms-excel; ,便可使浏览器将其中的数据当作excel来处理,浏览器将提示下载或打开excel文件
代码小例:
<html> <head> <meta http-equiv="content-Type" content="text/html;charset=utf-8"/> <script type="text/javascript"> function base64 (content) { return window.btoa(unescape(encodeURIComponent(content))); } function exportOffice(tableID){ var type = 'doc'; var table = document.getElementById(tableID); var excelContent = table.innerHTML; var excelFile = "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:"+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 += excelContent; excelFile += "</body>"; excelFile += "</html>"; var base64data = "base64," + base64(excelFile); switch(type){ case 'excel': window.open('data:application/vnd.ms-'+type+';'+base64data); break; case 'powerpoint': window.open('data:application/vnd.ms-'+type+';'+base64data); break; } } </script> </head> <body> <table id="targetTable"> <tr align="center"> <th>名次</th> <th>姓名</th> <th>成绩</th> </tr> <tr align="center"> <td>1</td> <td>小明</td> <td>100</td> </tr> <tr align="center"> <td>2</td> <td>小红</td> <td>95.5</td> </tr> </table> </br> <input id="Button1" type="button" value="导出" onclick="exportOffice('targetTable')" /> </body> </html>
连接:https://www.jianshu.com/p/a3642877d590
这五种方法前四种方法只支持IE浏览器,最后一个方法支持当前主流的浏览器(火狐,IE,Chrome,Opera,Safari)
-
-
-
-
-
<title>html 表格导出道</title>
-
<script language="JavaScript" type="text/javascript">
-
-
function method1(tableid) {
-
-
var curTbl = document.getElementById(tableid);
-
var oXL = new ActiveXObject("Excel.Application");
-
var oWB = oXL.Workbooks.Add();
-
var oSheet = oWB.ActiveSheet;
-
var sel = document.body.createTextRange();
-
sel.moveToElementText(curTbl);
-
-
sel.execCommand(
"Copy");
-
-
-
-
-
-
function method2(tableid)
-
-
-
var curTbl = document.getElementById(tableid);
-
var oXL = new ActiveXObject("Excel.Application");
-
var oWB = oXL.Workbooks.Add();
-
var oSheet = oWB.ActiveSheet;
-
var Lenr = curTbl.rows.length;
-
for (i = 0; i < Lenr; i++)
-
{
var Lenc = curTbl.rows(i).cells.length;
-
for (j = 0; j < Lenc; j++)
-
-
oSheet.Cells(i +
1, j + 1).value = curTbl.rows(i).cells(j).innerText;
-
-
-
-
-
-
-
-
function getXlsFromTbl(inTblId, inWindow){
-
-
-
-
-
if (inTblId != null && inTblId != "" && inTblId != "null") {
-
-
curStr = getTblData(inTblId, inWindow);
-
-
-
-
-
-
-
-
-
-
-
-
var fileName = getExcelFileName();
-
doFileExport(fileName, allStr);
-
-
-
-
-
-
alert(
"导出发生异常:" + e.name + "->" + e.description + "!");
-
-
-
-
-
-
function getTblData(inTbl, inWindow) {
-
-
-
var tblDocument = document;
-
if (!!inWindow && inWindow != "") {
-
-
if (!document.all(inWindow)) {
-
-
-
-
-
tblDocument =
eval(inWindow).document;
-
-
-
-
-
var curTbl = tblDocument.getElementById(inTbl);
-
-
-
for (var j = 0; j < curTbl.rows.length; j++) {
-
for (var i = 0; i < curTbl.rows[j].cells.length; i++) {
-
-
if (i == 0 && rows > 0) {
-
-
-
-
-
outStr += curTbl.rows[j].cells[i].innerText +
"t";
-
if (curTbl.rows[j].cells[i].colSpan > 1) {
-
for (var k = 0; k < curTbl.rows[j].cells[i].colSpan - 1; k++) {
-
-
-
-
-
if (rows == 0 && curTbl.rows[j].cells[i].rowSpan > 1) {
-
rows = curTbl.rows[j].cells[i].rowSpan -
1;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
function getExcelFileName() {
-
-
var curYear = d.getYear();
-
var curMonth = "" + (d.getMonth() + 1);
-
var curDate = "" + d.getDate();
-
var curHour = "" + d.getHours();
-
var curMinute = "" + d.getMinutes();
-
var curSecond = "" + d.getSeconds();
-
if (curMonth.length == 1) {
-
curMonth =
"0" + curMonth;
-
-
-
if (curDate.length == 1) {
-
-
-
-
if (curHour.length == 1) {
-
-
-
-
if (curMinute.length == 1) {
-
curMinute =
"0" + curMinute;
-
-
-
if (curSecond.length == 1) {
-
curSecond =
"0" + curSecond;
-
-
var fileName = "table" + "_" + curYear + curMonth + curDate + "_"
-
+ curHour + curMinute + curSecond +
".csv";
-
-
-
-
-
function doFileExport(inName, inStr) {
-
-
if (!!document.all("glbHideFrm")) {
-
-
-
-
-
-
var openPara = "left=" + (window.screen.width / 2 - width / 2)
-
+
",top=" + (window.screen.height / 2 - height / 2)
-
+
",scrollbars=no,width=" + width + ",height=" + height;
-
xlsWin =
window.open("", "_blank", openPara);
-
-
xlsWin.document.write(inStr);
-
-
xlsWin.document.execCommand(
'Saveas', true, inName);
-
-
-
-
-
-
function method4(tableid){
-
-
var curTbl = document.getElementById(tableid);
-
-
-
oXL =
new ActiveXObject("Excel.Application"); //建立AX对象excel
-
-
alert(
"没法启动Excel!\n\n若是您确信您的电脑中已经安装了Excel,"+"那么请调整IE的安全级别。\n\n具体操做:\n\n"+"工具 → Internet选项 → 安全 → 自定义级别 → 对没有标记为安全的ActiveX进行初始化和脚本运行 → 启用");
-
-
-
var oWB = oXL.Workbooks.Add(); //获取workbook对象
-
var oSheet = oWB.ActiveSheet;//激活当前sheet
-
var sel = document.body.createTextRange();
-
sel.moveToElementText(curTbl);
//把表格中的内容移到TextRange中
-
sel.select();
//全选TextRange中内容
-
sel.execCommand(
"Copy");//复制TextRange中内容
-
oSheet.Paste();
//粘贴到活动的EXCEL中
-
oXL.Visible =
true; //设置excel可见属性
-
var fname = oXL.Application.GetSaveAsFilename("将table导出到excel.xls", "Excel Spreadsheets (*.xls), *.xls");
-
-
-
-
-
-
-
-
-
-
var explorer = window.navigator.userAgent ;
-
-
if (explorer.indexOf("MSIE") >= 0) {
-
-
-
-
else if (explorer.indexOf("Firefox") >= 0) {
-
-
-
-
else if(explorer.indexOf("Chrome") >= 0){
-
-
-
-
else if(explorer.indexOf("Opera") >= 0){
-
-
-
-
else if(explorer.indexOf("Safari") >= 0){
-
-
-
-
function method5(tableid) {
-
-
-
var curTbl = document.getElementById(tableid);
-
var oXL = new ActiveXObject("Excel.Application");
-
var oWB = oXL.Workbooks.Add();
-
var xlsheet = oWB.Worksheets(1);
-
var sel = document.body.createTextRange();
-
sel.moveToElementText(curTbl);
-
-
sel.execCommand(
"Copy");
-
-
-
-
-
var fname = oXL.Application.GetSaveAsFilename("Excel.xls", "Excel Spreadsheets (*.xls), *.xls");
-
-
print(
"Nested catch caught " + e);
-
-
-
oWB.Close(savechanges =
false);
-
-
-
idTmr =
window.setInterval("Cleanup();", 1);
-
-
-
-
-
-
-
-
-
-
window.clearInterval(idTmr);
-
-
-
var tableToExcel = (function() {
-
var uri = 'data:application/vnd.ms-excel;base64,',
-
template =
'<html><head><meta charset="UTF-8"></head><body><table>{table}</table></body></html>',
-
base64 =
function(s) { return window.btoa(unescape(encodeURIComponent(s))) },
-
format =
function(s, c) {
-
return s.replace(/{(\w+)}/g,
-
function(m, p) { return c[p]; }) }
-
return function(table, name) {
-
if (!table.nodeType) table = document.getElementById(table)
-
var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
-
window.location.href = uri + base64(format(template, ctx))
-
-
-
-
-
-
-
-
-
<button type="button" onclick="method1('tableExcel')">导出Excel方法一</button>
-
<button type="button" onclick="method2('tableExcel')">导出Excel方法二</button>
-
<button type="button" onclick="getXlsFromTbl('tableExcel','myDiv')">导出Excel方法三</button>
-
<button type="button" onclick="method4('tableExcel')">导出Excel方法四</button>
-
<button type="button" onclick="method5('tableExcel')">导出Excel方法五</button>
-
-
-
<table id="tableExcel" width="100%" border="1" cellspacing="0" cellpadding="0">
-
-
<td colspan="5" align="center">html 表格导出道Excel</td>
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
今天上来发现,好多人,会遇到文件名,格式等问题。这里添加一种方法。兼容性我没有测试,你们能够试下,不过须要利用JQ直接贴代码了。jquery 引入文件在http://download.csdn.net/download/aa122273328/10103711 注意必定要引jquery-3.2.1.min.js,jquery.table2excel.js对应的文件。jquery-3.2.1.min.js这个看你对应的文件版本,不重要。若有问题,欢迎批评指导。
-
-
-
-
-
<title>html 表格导出道</title>
-
<script src="js/vendor/jquery-3.2.1.min.js"></script>
-
<script src="jquery.table2excel.js"></script>
-
<script language="JavaScript" type="text/javascript">
-
-
$(
document).ready(function () {
-
$(
"#btnExport").click(function () {
-
$(
"#tableExcel").table2excel({
-
exclude : ".noExl", //过滤位置的 css 类名
-
filename :
"你想说啥" + new Date().getTime() + ".xls", //文件名称
-
name:
"Excel Document Name.xlsx",
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
<button type="button" id="btnExport" onclick="method5('tableExcel')">导出Excel</button>
-
-
-
<table id="tableExcel" width="100%" border="1" cellspacing="0" cellpadding="0">
-
-
<td colspan="5" align="center">html 表格导出道Excel</td>
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
https://blog.csdn.net/aa122273328/article/details/50388673
有时候咱们须要把网页中的数据导出excel格式来,那么咱们用下面两种方法能够完成。jquery
第一种.自写代码
<html> <head> <meta http-equiv="content-Type" content="text/html;charset=utf-8"/> <script type="text/javascript"> function base64 (content) { return window.btoa(unescape(encodeURIComponent(content))); } /* *@tableId: table的Id *@fileName: 要生成excel文件的名字(不包括后缀,可随意填写) */ function tableToExcel(tableID,fileName){ var table = document.getElementById(tableID); var excelContent = table.innerHTML; var excelFile = "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel' xmlns='http://www.w3.org/TR/REC-html40'>"; excelFile += "<head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head>"; excelFile += "<body><table>"; excelFile += excelContent; excelFile += "</table></body>"; excelFile += "</html>"; var link = "data:application/vnd.ms-excel;base64," + base64(excelFile); var a = document.createElement("a"); a.download = fileName+".xlsx"; a.href = link; a.click(); } </script> </head> <body> <button type="button" onclick="tableToExcel('item','data')">导出</button> <table id="item"> <tr> <th>编号</th> <th>姓名</th> <th>年龄</th> </tr> <tr> <td>1</td> <td>小明</td> <td>19</td> </tr> <tr> <td>2</td> <td>小芳</td> <td>20</td> </tr> <tr> <td>3</td> <td>大军</td> <td>22</td> </tr> </table> </body> </html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
**git
第二种.jquery插件
**
首先要先下载一个jquery.table2excel.js插件(网上搜搜),而后使用。github
<!doctype html> <html lang="zh"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>table2excel</title> <link rel="stylesheet" href="http://libs.baidu.com/bootstrap/3.2.0/css/bootstrap.min.css"> </head> <body> <header class="jq22-header"> <h4>table2excel-可将HTML表格内容导出到Excel中的jQuery插件 <span>jQuery Plugin to export HTML tabled to Excel Spreadsheet Compatible Files</span></h4> </header> <section class="jq22-container"> <div class="container" style="padding:30px 0"> <div class="row"> <div class="md-col-8"> <div class="table-responsive table2excel" data-tableName="Test Table 1"> <table class="table table-striped table-bordered table-hover"> <thead> <tr class="noExl"> <td class="danger">带<code>noExl</code>class的行不会被输出到excel中</td> <td class="danger">带<code>noExl</code>class的行不会被输出到excel中</td> </tr> <tr> <td class="success">这一行会被导出到excel中</td> <td class="success">这一行会被导出到excel中</td> </tr> </thead> <tbody> <tr> <td>单元格1-1</td> <td>单元格1-2</td> </tr> <tr> <td>单元格2-1</td> <td>单元格2-2</td> </tr> <tr> <td>单元格3-1</td> <td>单元格3-2</td> </tr> </tbody> <tfoot> <tr> <td colspan="2" class="warning">合并2个单元格</td> </tr> </tfoot> </table> </div> </div> </div> <button id="btn" class="btn btn-primary">点击这里将表格内容导出到excel中</button> </div> </section> <script src="http://www.jq22.com/jquery/1.11.1/jquery.min.js"></script> <script>window.jQuery || document.write('<script src="js/jquery-1.11.0.min.js"><\/script>')</script> <script src="dist/jquery.table2excel.js"></script> <script> $(function() { $("#btn").click(function(){ $(".table2excel").table2excel({ exclude: ".noExl", name: "Excel Document Name", filename: "myFileName", exclude_img: true, exclude_links: true, exclude_inputs: true }); }); }); </script> </body> </html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
详情请浏览:html中table导出Excel