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'