久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

html頁面表格導出到excel總結(jié) | 守望軒

 青松卓然 2012-10-17

最近一個項目需要把報表的表格導入excel,在網(wǎng)上找了一些方法,比較研究了一下,記在這里,備忘,。

表格例子如下:

<table id="tableExcel" width="100%" border="1" cellspacing="0" cellpadding="0">
    <tr>
        <td colspan="5" align="center">html 表格導出道Excel</td>
    </tr>
    <tr>
        <td>列標題1</td>
        <td>列標題2</td>
        <td>類標題3</td>
        <td>列標題4</td>
        <td>列標題5</td>
    </tr>
    <tr>
        <td>aaa</td>
        <td>bbb</td>
        <td>ccc</td>
        <td>ddd</td>
        <td>eee</td>
    </tr>
    <tr>
        <td>AAA</td>
        <td>BBB</td>
        <td>CCC</td>
        <td>DDD</td>
        <td>EEE</td>
    </tr>
    <tr>
        <td>FFF</td>
        <td>GGG</td>
        <td>HHH</td>
        <td>III</td>
        <td>JJJ</td>
   </tr>
</table>

 

1、js的方法

A,、將整個表格拷貝到EXCEL中

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.select();
    sel.execCommand("Copy");
    oSheet.Paste();
    oXL.Visible = true;

}

 B,、讀取表格中每個單元到EXCEL中:

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;

        }

    }
    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("導出發(fā)生異常:" + 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 < curTbl.rows.length; j++) {
            for (var i = 0; i < curTbl.rows[j].cells.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 < curTbl.rows[j].cells[i].colSpan - 1; k++) {
                        outStr += " \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=" + width + ",height=" + height;
        xlsWin = window.open("", "_blank", openPara);
    }
    xlsWin.document.write(inStr);
    xlsWin.document.close();
    xlsWin.document.execCommand('Saveas', true, inName);
    xlsWin.close();

}

 總結(jié):比較上面3種方法,,感覺第一種方法比較完美一些,因為這種方法比較完整的輸出表格的格式,。但,,第一和第二種方法都用了ActiveX 對象,對客戶端的安全有要求,而且最大的問題還有一個,,就是excel 對象無法關(guān)閉,。第3中方法雖然沒有用ActiveX 對象,但是用了彈出窗口輸出,, 如果禁止了彈出窗口則無法使用,。

對于execl 對象無法關(guān)閉的問題,下面的方法是一個權(quán)宜方法:

function Cleanup() {
    window.clearInterval(idTmr);
    CollectGarbage();
  }

 調(diào)用方法:

idTmr = window.setInterval("Cleanup();",1);
2,、Asp.net(c#)中的方法
這種方法其實類似上面的js的第3中方法(也可以在其他的web腳本來實現(xiàn),,比如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();
    }
這種方法的從本質(zhì)上說并非標準的excel格式,不過把html格式的文件另存為excel的格式,,然后用excel打開罷了,。
3、利用ExceL Application或者MSOWC 或者ado.net
這種方法都是利用服務(wù)器的組件來時實現(xiàn),,要求服務(wù)端要安裝excel,,具體的代碼可以看下面的鏈接:
http://www.cnblogs.com/pucumt/archive/2006/09/13/503120.html
http://support.microsoft.com/default.aspx?scid=kb;zh-cn;306023#top
我不提倡用這種方法,因為需要占用服務(wù)器的資源,。
本文地址:http://www./programming-notes/html-table-2-excel.html

 


標簽: , , , , ,    分類: 編程札記

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點,。請注意甄別內(nèi)容中的聯(lián)系方式,、誘導購買等信息,謹防詐騙,。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報,。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多