Java 将HTML表格的内容导出到Excel表格,以便IE、Mozilla、Firefox等浏览器支持。?

Java 将HTML表格的内容导出到Excel表格,以便IE、Mozilla、Firefox等浏览器支持。?,java,javascript,jquery,html,excel,Java,Javascript,Jquery,Html,Excel,这是我的HTML页面 <html> <head> <title>Table </title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"> </script> <script> </script> </head> <body> <table border="

这是我的HTML页面

<html>
<head>
<title>Table </title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
</script>

</head>
<body>
<table border="1">
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
</tr>
</table>

<input id ="export" type = "button" value = "Export"></input>
</body>
</html>

桌子
标题1
标题2
第1行,第1单元
第1行第2单元
第2行第1单元
第2行,第2单元
现在,我希望用户将网页上的内容导出到excel工作表中。我该怎么做? 我不想使用javascript,因为在这种情况下,代码将依赖于浏览器

任何建议! 提前谢谢


注意:我在stackoverflow中遇到了一些问题,其中提到使用jQuery是一个不错的选择。但问题是我对jQuery一无所知。

使用以下java代码将Html导出到Excel:

var tableToExcel = (function() {
var uri = 'data:application/vnd.ms-excel;base64,'
, template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]
><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}
</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbo
ok></xml><![endif]--></head><body><table>{table}</table></body></html>'
, base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }
, format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) }
return function(table, name) {
if (!table.nodeType) table = document.getElementById(table)
var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
 window.location.href = uri + base64(format(template, ctx))
  }
})()
var tableToExcel=(函数(){
var uri='data:application/vnd.ms excel;base64,'
,模板=“{table}”
,base64=函数{return window.btoa(unescape(encodeURIComponent))}
,format=函数(s,c){返回s.replace(/{(\w+)}/g,函数(m,p){返回c[p];})}
返回函数(表、名称){
如果(!table.nodeType)table=document.getElementById(table)
var ctx={工作表:名称| |'工作表',表:table.innerHTML}
window.location.href=uri+base64(格式(模板,ctx))
}
})()

您可以轻松使用“我的代码”将html文件导出到excel文件。在本例中,我创建了上面的示例,只需将我的代码复制并粘贴到html文件中,然后您将看到结果,并可以下载excel文件

首先在中使用上述java代码,然后放入以下html代码:

<input type="button" onclick="tableToExcel('testTable', 'W3C Example Table')" value="Export to Excel">
<table class="tableizer-table" id="testTable" summary="Code page support in different versions of MS Windows." rules="groups" 
frame="hsides" border="2">
<tr class="tableizer-firstrow"><th>Header 1</th><th>Header 2</th></tr>
 <tr><td>row 1, cell 1</td><td>row 1, cell 2</td></tr>
 <tr><td>row 2, cell 1</td><td>row 2, cell 2</td></tr>
</table>

标题1标题2
第1行第1单元格第1行第2单元格
第2行第1单元格第2行第2单元格

它是java代码吗?这就是jQuery人,我遇到过它,但不知道如何使用它!我得到以下错误网页错误详细信息
用户代理:Mozilla/4.0(兼容;MSIE 8.0;Windows NT 5.1;Trident/4.0;.NET CLR 2.0.50727;.NET CLR 3.0.04506.648;.NET CLR 3.5.21022;.NET CLR 3.0.4506.2152;.NET CLR 3.5.30729)时间戳:Thu,2013年8月29日07:31:24 UTC消息:未终止字符串常量行:10字符:119代码:0 URI:file:///C:/Documents%20and%20Settings/592635/Desktop/table.html   消息:对象预期行:28字符:1代码:0 URI:file:///C:/Documents%20and%20Settings/592635/Desktop/table.html
没问题,您可以通过以下链接检查html:()