var Lenc = curTbl.rows(i).cells.length;
for (j = 0; j value = curTbl.rows(i).cells(j).innerText;
}
}
oXL.Visible = true;
}
c、把表格输出到另一个页面,然后存成cvs格式
function getXlsFromTbl(inTblId, inWindow)
{
try {
var allStr = "";
var curStr = "";
if (inTblId != null && inTblId != "" && inTblId != "null") {
curStr = getTblData(inTblId, inWindow);
}
if (curStr != null) {
allStr += curStr;
}
else {
alert("你要导出的表不存在");
return;
}
var fileName = getExcelFileName();
doFileExport(fileName, allStr);
}
catch(e) {
alert("导出发生异常:" + e.name + "->" + e.description + "!");
}
}
function getTblData(inTbl, inWindow) {
var rows = 0;
var tblDocument = document;
if (!!inWindow && inWindow != "") {
if (!document.all(inWindow)) {
return null;
}
else {
tblDocument = eval(inWindow).document;
}
}
var curTbl = tblDocument.getElementById(inTbl);
var outStr = "";
if (curTbl != null) {
for (var j = 0; j length; j++) {
for (var i = 0; i length; i++) {
if (i == 0 && rows > 0) {
outStr += " /t";
rows -= 1;
}
outStr += curTbl.rows[j].cells[i].innerText + "/t";
if (curTbl.rows[j].cells[i].colSpan > 1) {
for (var k = 0; k /t";
}
}
if (i == 0) {
if (rows == 0 && curTbl.rows[j].cells[i].rowSpan > 1) {
rows = curTbl.rows[j].cells[i].rowSpan - 1;
}
}
}
outStr += "/r/n";
}
}
else {
outStr = null;
alert(inTbl + "不存在 !");
}
return outStr;
}
function getExcelFileName() {
var d = new Date();
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) {
curDate = "0" + curDate;
}
if (curHour.length == 1) {
curHour = "0" + curHour;
}
if (curMinute.length == 1) {
curMinute = "0" + curMinute;
}
if (curSecond.length == 1) {
curSecOnd= "0" + curSecond;
}
var fileName = "table" + "_" + curYear + curMonth + curDate + "_"
+ curHour + curMinute + curSecond + ".csv";
return fileName;
}
function doFileExport(inName, inStr) {
var xlsWin = null;
if (!!document.all("glbHideFrm")) {
xlsWin = glbHideFrm;
}
else {
var width = 6;
var height = 4;
var openPara = "left=" + (window.screen.width / 2 - width / 2)
+ ",top=" + (window.screen.height / 2 - height / 2)
+ ",scrollbars=no, + width + ", + height;
xlsWin = window.open("", "_blank", openPara);
}
xlsWin.document.write(inStr);
xlsWin.document.close();
xlsWin.document.execCommand('Saveas', true, inName);
xlsWin.close();
}
总结:比较上面3种方法,感觉第一种方法比较完美一些,因为这种方法比较完整的输出表格的格式。但,第一和第二种方法都用了ActiveX 对象,对客户端的安全有要求,而且最大的问题还有一个,就是excel 对象无法关闭。第3中方法虽然没有用ActiveX 对象,但是用了弹出窗口输出, 如果禁止了弹出窗口则无法使用。
对于execl 对象无法关闭的问题,下面的方法是一个权宜方法:
function Cleanup() {
window.clearInterval(idTmr);
CollectGarbage();
}
调用方法:
idTmr = window.setInterval("Cleanup();",1);
2、Asp.net(c#)中的方法
这种方法其实类似上面的js的第3中方法(也可以在其他的web脚本来实现,比如asp中Vbscript,或者php),把表格用文件流的方式
输出为excel。实例代码如下:
public void OutPutExcel(string title)
{
Response.Clear();
Response.Buffer = true;
Response.Charset = "utf-8";
Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(title + ".xls"));
Response.COntentEncoding= System.Text.Encoding.GetEncoding("utf-8");
Response.COntentType= "application/ms-excel";
Page.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.Page.RenderControl(oHtmlTextWriter);
string temp = oStringWriter.ToString();
Response.Write(temp);
Response.End();
}
这种方法的从本质上说并非标准的excel格式,不过把html格式的文件另存为excel的格式,然后用excel打开罢了。
3、利用ExceL Application或者MSOWC 或者ado.net
这种方法都是利用服务器的组件来时实现,要求服务端要安装excel,具体的代码可以看下面的链接:
http://www.cnblogs.com/pucumt/archive/2006/09/13/503120.html
http://support.microsoft.com/default.aspx?scid=kb;zh-cn;306023#top