将多个HTML表格输出到单个Excel表单

我在vb.net中创build了两个HTML表格,并在运行时将string文本附加到aspx页面的innerhtml。 代码如下(这是一个示例代码):

Dim oBuilder as stringbuilder With oBuilder .Append("<html xmlns:x=""urn:schemas-microsoft-com:office:excel"">") .Append("<head>") .Append("<meta http-equiv=""Content-Type"" content=""text/html;charset=windows-1252"">") .Append("<!--[if gte mso 9]>") .Append("<xml>") .Append("<x:ExcelWorkbook>") .Append("<x:ExcelWorksheets>") .Append("<x:ExcelWorksheet>") .Append("<x:Name>Summary</x:Name>") .Append("<div>") .Append("<table style="" border:solid 1px black; "">") .Append("<tr>") .Append("<td>Column 1</td>") .Append("</tr>") .Append("</table>") .Append("</div>") .Append("<div>") .Append("<table style="" border:solid 1px black; "">") .Append("<tr>") .Append("<td>Column 1 - Table 2</td>") .Append("</tr>") .Append("</table>") .Append("</div>") End With LogDetails.InnerHtml = oBuilder.ToString 

aspx页面实际上显示的表格很好(一个在另一个之下)。 我尝试添加以下行,并使用XML的办公function命名工作表。 我能够创build和命名两个工作表:

  .Append("<html xmlns:x=""urn:schemas-microsoft-com:office:excel"">") .Append("<head>") .Append("<meta http-equiv=""Content-Type"" content=""text/html;charset=windows-1252"">") .Append("<!--[if gte mso 9]>") .Append("<xml>") .Append("<x:ExcelWorkbook>") .Append("<x:ExcelWorksheets>") .Append("<x:ExcelWorksheet>") .Append("<x:Name>Summary</x:Name>") .Append("</x:ExcelWorksheet>") .Append("<x:ExcelWorksheet>") .Append("<x:Name>Summary no 2</x:Name>") .Append("</x:ExcelWorksheet>") .Append("</xml>") .Append("<![endif]-->") .Append("</head>") .Append("<body>") .Append(oBuilder) .Append("</body>") .Append("</html>") 

渲染页面后,我点击一个button,将表格导出为ex​​cel。 我的button单击事件执行以下操作:

 Context.Response.ContentType = "application/ms-excel" Context.Response.AddHeader("content-disposition", "attachment; filename=" + Session("ExcelFile").Trim + "") Context.Response.AddHeader("Content-Transfer-Encoding", "binary") Context.Response.Write(LogDetails.InnerHtml) 

这个button给我一个保存/打开对话框。 当我打开保存的excel表单时,发现它有两个表格在另一个的下面…我希望表格在单独的工作表上。 我不想重新创build我的整个代码,因为它有多年的工作…有没有办法,我可以把第一个HTML表格在“摘要”工作表和第二个HTML表格到“总结no 2”工作表没有修改整个代码?

任何帮助将不胜感激,因为我已经研究了这个3天以上…(不多说我的研究技巧)

问候,阳光明媚

我build议您需要将第一个表格渲染到第一个电子表格中,然后更改活动工作表并渲染第二个表格。