先上代碼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