A. 如何用js導出數據Excel
(function ($) {
Date.prototype.Format = function (fmt) {
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小時
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
$.fn.ExportExcel = function (thread_id,tab_id, options) {
var defaults = {
height: '24px',
'line-height': '24px',
margin: '0 5px',
padding: '0 11px',
color: '#000',
background: '#02bafa',
border: '1px #26bbdb solid',
'border-radius': '3px',
/*color: #fff;*/
display: 'inline-block',
'text-decoration': 'none',
'font-size': '12px',
outline: 'none',
cursor: 'pointer'
}
var options = $.extend(defaults, options);
return this.each(function () {
var currentObject = $(this); //獲取當前對象
currentObject.css(defaults);
currentObject.onmouseover = function () {
$(this).css('cursor', 'hand');
};
currentObject.click(function () {
//From:jsfiddle.net/h42y4ke2/16/
var tab_text = '<html xmlns:x="urn:schemas-microsoft-com:office:excel">';
tab_text = tab_text + '<head><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet>';
tab_text = tab_text + '<x:Name>Test Sheet</x:Name>';
tab_text = tab_text + '<x:WorksheetOptions><x:Panes></x:Panes></x:WorksheetOptions></x:ExcelWorksheet>';
tab_text = tab_text + '</x:ExcelWorksheets></x:ExcelWorkbook></xml></head><body>';
tab_text = tab_text + "<table border='1px'>";
tab_text = tab_text + $('#' + thread_id).html();
tab_text = tab_text + $('#' + tab_id).html();
tab_text = tab_text + '</table></body></html>';
var data_type = 'data:application/vnd.ms-excel';
var timeStr = new Date().Format('yyyyMMddhhmmss');
$(this).attr('href', data_type + ', ' + encodeURIComponent(tab_text));
$(this).attr('download', '日常數據報表' + timeStr + '.xls');
});
})
}
})(jQuery);
<html>
<a href="#" id="export">導出</a>
<table>
<thead id="theadDate">
<tr>
<th>姓名</th>
<th>班級</th>
<th>年齡</th>
</tr>
</thead>
<tbody id="tbodyDate">
<tr> <td>張三</td>
<td>高二</td>
<td>18</td>
</tr>
<tr>
<td>李四</td>
<td>高三</td>
<td>20</td>
</tr>
</tbody>
</table>
<script src="assets/javascripts/autotest/export-excel.js" ></script><!-- 引入js文件-->
<script type="text/javascript">
//導出 調用
$(function () {
$('#export').ExportExcel('theadDate','tbodyDate'); //tbodyDate為table的id,export為a標簽。
});
</script>
</html>
B. js 里 實現 數據導出 導出成txt格式的文件
//前台
var appWindow = window.open(調用後台方法的路徑名);
appWindow.focus();
//後台
getResponse().setHeader("Content-disposition","attachment;filename = "+導出文件名);
getResponse().setContentType("text/plain"); //text/plain 就會導出成內txt了
。容。。。。。。。。。 後面的就是 讀取資料庫 在寫入到txt里就可以了
C. 請列舉出JS語言中輸出數據的幾種方式
使用 window.alert() 彈出警告框。
使用 document.write() 方法將內容寫到 HTML 文檔中。
使用 innerHTML 寫入到 HTML 元素。
使用 console.log() 寫入到瀏覽器的控制台。
這個是js輸出的主要方式:
D. 如何獲取JS變數類型
如何判斷js中的數據類型:typeof、instanceof、 constructor、 prototype方法比較
如何判斷js中的類型呢,先舉幾個例子:
var a = "iamstring.";
var b = 222;
var c= [1,2,3];
var d = new Date();
var e =
function(){alert(111);};
var f =
function(){this.name="22";};
最常見的判斷方法:typeof
alert(typeof a)
------------> string
alert(typeof b)
------------> number
alert(typeof c)
------------> object
alert(typeof d)
------------> object
alert(typeof e)
------------> function
alert(typeof f)
------------> function
其中typeof返回的類型都是字元串形式,需注意,例如:
alert(typeof a == "string")
-------------> true
alert(typeof a == String)
---------------> false
另外typeof
可以判斷function的類型;在判斷除Object類型的對象時比較方便。
判斷已知對象類型的方法: instanceof
alert(c instanceof Array)
---------------> true
alert(d instanceof
Date)
alert(f instanceof Function)
------------> true
alert(f instanceof function)
------------> false
注意:instanceof
後面一定要是對象類型,並且大小寫不能錯,該方法適合一些條件選擇或分支。
根據對象的constructor判斷:
constructor
alert(c.constructor ===
Array) ----------> true
alert(d.constructor === Date)
-----------> true
alert(e.constructor ===
Function) -------> true
注意: constructor 在類繼承時會出錯
eg,
function A(){};
function B(){};
A.prototype = new B(); //A繼承自B
var aObj = new A();
alert(aobj.constructor === B) ----------->
true;
alert(aobj.constructor === A) ----------->
false;
而instanceof方法不會出現該問題,對象直接繼承和間接繼承的都會報true:
alert(aobj instanceof B) ---------------->
true;
alert(aobj instanceof B) ---------------->
true;
言歸正傳,解決construtor的問題通常是讓對象的constructor手動指向自己:
aobj.constructor = A;
//將自己的類賦值給對象的constructor屬性
alert(aobj.constructor === A) ----------->
true;
alert(aobj.constructor === B) ----------->
false; //基類不會報true了;
通用但很繁瑣的方法: prototype
alert(Object.prototype.toString.call(a) === 『[object String]』)
-------> true;
alert(Object.prototype.toString.call(b) === 『[object Number]』)
-------> true;
alert(Object.prototype.toString.call(c) === 『[object Array]』)
-------> true;
alert(Object.prototype.toString.call(d) === 『[object Date]』)
-------> true;
alert(Object.prototype.toString.call(e) === 『[object Function]』)
-------> true;
alert(Object.prototype.toString.call(f) === 『[object Function]』)
-------> true;
大小寫不能寫錯,比較麻煩,但勝在通用。
通常情況下用typeof
E. js導出(文本格式)excel
你可以在導出的時候給數據前加一個英文半形的'逗號
F. js導出excel表格,怎樣使導出的表格中單元格里的數據是數字類型而非文本類型
=TRIM(a1)
下拉填充完成--復制---選擇性粘貼到原位置單元格即可
G. javascript怎麼轉換數據類型
<!--
數字字元串轉整數用window.parseInt方法;
帶小數的數字字元串轉浮點數(小數)用window.parseFloat;
其它對象轉換內成字元串,容用對象名.toString方法。
下面是一個簡單的例子。
-->
<html>
<body>
<script type="text/javascript">
var str1="10",str2="1.11",d=88;
var n1=window.parseInt(str1);
var n2=window.parseFloat(str2);
var str=d.toString();
var rq=new Date();
document.write("n1+n2="+(n1+n2)+"<br />");
document.write(str+"<br />");
document.write(rq.toString());
</script>
</body>
</html>
H. js怎麼彈出變數的數據類型
通過type of 變數名的方式獲取變數的數據類型。
因為js變數是鬆散類型(即弱類版型)的,可以用來權保存任何類型的數據,所以用typeof 用來檢測給定變數的數據類型,可能的返回值有:
1. 'undefined' --- 這個值未定義;
2. 'boolean' --- 這個值是布爾值;
3. 'string' --- 這個值是字元串;
4. 'number' --- 這個值是數值;
5. 'object' --- 這個值是對象或null;
6. 'function' --- 這個值是函數。
舉例:
var aa = 'test string';
alert(typeof aa); // 'string'
alert(typeof 90); // 'number'