我有表格,可以导出到Excel。为此,它需要具有JQuery语法。我有两个分离的JQuery语法:1适用于IE,另一个适用于FF,Chrome。每个单独的语法都可以很好地工作(表格的数据出现在Excel中)。当我把它们放在一起时,FF和Chrome仍然可以正常工作,但IE中存在问题。
在IE中,当我点击按钮时,表格的数据显示在Excel中,但是还有另一个IE浏览器弹出窗口,它是空白的,URL字段中充满了表格的数据(<table> <tr> <td>....
)。如果页面中只有IE的JQuery代码,则不会发生此问题!
这是代码
JS
<!--[if IE]>
<script>
$(document).ready(function() {
$(".toExcelButton").click(function() {
var table = $("." + $(this).data('target'));
var strCopy = $('<div></div>').html(table.clone()).html(); window.clipboardData.setData("Text", strCopy);
var objExcel = new ActiveXObject("Excel.Application");
objExcel.visible = false;
var objWorkbook = objExcel.Workbooks.Add;
var objWorksheet = objWorkbook.Worksheets(1);
objWorksheet.Paste;
objExcel.visible = true;
});
});
</script>
<![endif]-->
<script>
$(document).ready(
function()
{
$(".toExcelButton").click(function(e) {
var table = $("." + $(this).data('target'));
window.open('data:application/vnd.ms-excel,' + $(table).html());
e.preventDefault();
});
}
);
</script>
HTML
<div class="dvData">
<table>
<tr>
<th>Billing System</th>
<th>Market Code</th>
<th>Payment Amount</th>
</tr>
<tr>
<td>RED</td>
<td>222</td>
<td>$103.00</td>
</tr>
<tr>
<td>BLUE</td>
<td>111</td>
<td>$13.00</td>
</tr>
<tr>
<td>GREEN</td>
<td>555</td>
<td>$143.00</td>
</tr>
</table>
</div>
<br />
<input type="button" id="btnExport" value="Export" class="toExcelButton" data-target="dvData" />
<p>
<div class="dvData1">
<table>
<tr>
<th>Account System</th>
<th>Market Code</th>
<th>Payment Amount</th>
</tr>
<tr>
<td>RED</td>
<td>222</td>
<td>$103.00</td>
</tr>
<tr>
<td>BLUE</td>
<td>111</td>
<td>$13.00</td>
</tr>
<tr>
<td>GREEN</td>
<td>555</td>
<td>$143.00</td>
</tr>
</table>
</div>
<br />
<input type="button" class="toExcelButton" data-target="dvData1" value="Export" />