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

时间:2012-09-24 10:25:17

标签: asp.net html vb.net excel

我在vb.net中用字符串变量创建两个HTML表,并在运行时将字符串文本附加到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的办公功能来命名工作表。我能够创建并命名两个工作表:

        .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>")

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

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)

此按钮为我提供了一个保存/打开对话框。当我打开保存的excel表时,我发现它有两个表一个在另一个之下......我希望这些表位于单独的工作表上。我不想重新创建我的整个代码,因为它有多年的工作...有没有办法可以将第一个HTML表放在“摘要”工作表中,将第二个HTML表放入“摘要号2”工作表中而不必重做整个代码?

我将非常感谢任何帮助,因为我已经对此进行了3天以上的研究......(对我的研究技巧没有太多了解)

此致 晴天

1 个答案:

答案 0 :(得分:0)

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

相关问题