0

With F12 debug, the JQuery codes is able to skip hidden column cell, only exort cells not hidden, but the >last statement, window.open NOT able to bring it up on the 2010 EXCEL page. The following code has been simplied to focus the problem, not able to export HTML table to 2010 Execel

    <body>    
    <table id="myGrid">
     <tr><th style="display:">First Column</th>
     <th  style="display:">Second Column</th>
     <th  style="display:">Third Column</th>
     <th  style="display: none">Forth Column</th>
    </tr>
    <tr><td>  2</td><td>   two</td><td>   deux</td><td style="display: none">     zwei</td></tr>
    <tr><td>  3</td><td> three</td><td>  trois</td><td style="display: none">     drei</td></tr>
    <tr><td>  4</td><td>  four</td><td>quattre</td><td style="display: none">     vier</td></tr>
    <tr><td>  5</td><td>  five</td><td>   cinq</td><td style="display: none">f&uuml;nf</td></tr>
    <tr><td>  6</td><td>   six</td><td>    six</td><td style="display: none">    sechs</td></tr>
    </table>
    <br />
    Test: <input id="ExportExcel" type='submit' value='Export Excel'>

    <script type="text/javascript">

    $(document).ready(function () {
     $('#ExportExcel').click(function () {
        var html;
        var numofRows;
        var gTable = document.getElementById('myGrid');
        numofRows = gTable.rows.length - 1;
        var numofCells;
        var trhtml = "";
        numofCells = gTable.rows[0].cells.length - 1;
        for (r = 0; r <= numofRows; r++) {
            var c = 0;
            var tdhtml = "";
            for (c = 0; c <= numofCells; c++) {
                if (!(gTable.rows[r].cells[c].currentStyle.display == "none")) {
                    var tempstr = gTable.rows[r].cells[c].innerText;
                    tdhtml = tdhtml + "<td>" + gTable.rows[r].cells[c].innerText + "</td>";
                }
            }
            trhtml = trhtml + "<tr>" + tdhtml + "</tr>";
        }
        html = "<table border='1'>" + trhtml + "</table>";
        // MS OFFICE 2003  : data:application/vnd.ms-excel        
        // MS OFFICE 2007  : application/vnd.openxmlformats-officedocument.spreadsheetml.sheet        
        window.open('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,' + encodeURIComponent(html));
       });
     });
    </script>  
    </body>
4

2 回答 2

0
Ans For the above question:

Here i added class to hidden td 

<table id="myGrid">
     <tr><th style="display:">First Column</th>
     <th  style="display:">Second Column</th>
     <th  style="display:">Third Column</th>
     <th  style="display: none">Forth Column</th>
    </tr>
    <tr><td>  2</td><td>   two</td><td>   deux</td><td class="xyz" style="display: none">     zwei</td></tr>
    <tr><td>  3</td><td> three</td><td>  trois</td><td class="xyz" style="display: none">     drei</td></tr>
    <tr><td>  4</td><td>  four</td><td>quattre</td><td class="xyz" style="display: none">     vier</td></tr>
    <tr><td>  5</td><td>  five</td><td>   cinq</td><td class="xyz" style="display: none">f&uuml;nf</td></tr>
    <tr><td>  6</td><td>   six</td><td>    six</td><td class="xyz" style="display: none">    sechs</td></tr>
    </table>
when click on export add this statement -> $('.xyz').remove();
like this
$('#ExportExcel').click(function () {

$('.xyz').remove();

// add export statements here

});

it will work

导出后,与 xyz 类相关的 td 不会显示在您的 Excel 中。

于 2017-09-19T12:43:15.303 回答
-1

附加的 JQuery 代码会将可见的列标题和行单元格内容导出到客户端 Excel;只需复制并粘贴以下代码成为 Javascript 代码的一部分(插入问题代码),并将按钮 id 更改为 ExportExcel2。注意:假设:客户端安装了 Excel。

   $('#ExportExcel2').click(function () {

        str = "";
        var myTable = document.getElementById('myGrid');
        var rows = myTable.getElementsByTagName('tr');
        var rowCount = myTable.rows.length;
        var colCount = myTable.getElementsByTagName("tr")[0].getElementsByTagName("th").length;

        var ExcelApp = new ActiveXObject("Excel.Application");
        var ExcelWorkbook = ExcelApp.Workbooks.Add();
        var ExcelSheet = ExcelWorkbook.ActiveSheet; //new ActiveXObject("Excel.Sheet"); 
        //ExcelSheet.Application.Visible = true;
        ExcelApp.Visible = true;

        ExcelSheet.Range("A1", "Z1").Font.Bold = true;
        ExcelSheet.Range("A1", "Z1").Font.ColorIndex = 23;

        //Format table headers
        var tarcol = 0;
        for (var i = 0; i < 1; i++) {
            targetCol = 1;
            for (var j = 0; j < colCount; j++) {
                if (!(myTable.getElementsByTagName("tr")[i].getElementsByTagName("th")[j].currentStyle.display == "none")) {
                    str = myTable.getElementsByTagName("tr")[i].getElementsByTagName("th")[j].innerHTML;
                    //ExcelSheet.Cells(i + 1, j + 1).Value = str;
                    ExcelSheet.Cells(i + 1, targetCol).Value = str;
                    targetCol += 1;
                }
            }
            ExcelSheet.Range("A1", "BD1").EntireColumn.AutoFit();
        }
        for (var i = 1; i < rowCount; i++) {
            targetCol = 1;
            for (var k = 0; k < colCount; k++) {
                if (!(myTable.getElementsByTagName("tr")[i].getElementsByTagName("td")[k].currentStyle.display == "none")) {
                    str = rows[i].getElementsByTagName('td')[k].innerHTML;
                    //ExcelSheet.Cells(i + 1, k + 1).Value = myTable.rows[i].cells[k].innerText;
                    ExcelSheet.Cells(i + 1, targetCol).Value = myTable.rows[i].cells[k].innerText;
                    targetCol += 1;
                }
            }
            ExcelSheet.Range("A" + i, "Z" + i).WrapText = true;
            ExcelSheet.Range("A" + 1, "Z" + i).EntireColumn.AutoFit();
        }

        //ExcelSheet.SaveAs("C:\\TEST.XLS");
        //ExcelSheet.Application.Quit();
    });
于 2013-06-24T19:45:20.190 回答