Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
387 views
in Technique[技术] by (71.8m points)

javascript - how to pass html table values to excel sheet cells

function toExcel(tableID)
{    
    var detailsTable = document.getElementById(tableID);
    var oExcel = new ActiveXObject("Scripting.FileSystemObject");
    var oBook = oExcel.Workbooks.Add;
    var oSheet = oBook.Worksheets(1);
    for (var y=0;y<detailsTable.rows.length;y++)
    {
        for (var x=0;x<detailsTable.rows(y).cells.length;x++)
        {
            oSheet.Cells(y+1,x+1) =detailsTable.rows(y).cells(x).innerText;
        }
    }
    oExcel.Visible = true;
    oExcel.UserControl = true;
} 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As I posted here. However, can't test it myself. So let me know, if it works :)

<html>
<head>
<script type="text/javascript">
    function CreateExcelSheet() {
        var x = myTable.rows;

        var xls = new ActiveXObject("Excel.Application");
            xls.visible = true;
            xls.Workbooks.Add;
        for (i = 0; i < x.length; i++) {
            var y = x[i].cells;
            for (j = 0; j < y.length; j++) {
                xls.Cells( i+1, j+1).Value = y[j].innerText;
            }
        }
    }
</script>
</head>
<body>

<form>
    <input type="button" onclick="CreateExcelSheet()" value="Create Excel Sheet">
</form>

<table id="myTable" border="1">
    <tr>
        <td>Name</td>
        <td>Age</td>
    </tr>
    <tr>
        <td>Shivani</td>
        <td>25</td>
    </tr>
    <tr>
        <td>Naren </td>
        <td>28</td>
    </tr>
    <tr>
        <td>Logs</td>
        <td>57</td>
    </tr>
    <tr>
        <td>Kas</td>
        <td>54</td>
    </tr>
    <tr>
        <td>Sent</td>
        <td>26</td>
    </tr>
    <tr>
        <td>Bruce</td>
        <td>7</td>
    </tr>
</table>

</body>
</html>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...