使用 ASP.NET 将 HTML 表导出到 Excel

发布于 2025-01-07 12:30:39 字数 198 浏览 0 评论 0原文

我有一个 html 表(不是 Gridview),并且没有正确的标题和行。相反,它具有定制的结构和数据。我想将该表导出到 Excel。我该如何使用 ASP.NET? 在此处输入图像描述

标签是固定文本,整数值来自数据库。因此表结构是固定的,只有整数/小数值发生变化。

I have an html table (Not Gridview) and that has no proper Header and rows. Instead it has customized structure and data. I want to export this table to Excel. How can I do using ASP.NET?
enter image description here

The labels are fixed text and the integer values are coming from database. So the table structure is fixed only the integer/decimal values change.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

离线来电— 2025-01-14 12:30:39

您希望使用 ASP.NET 将 HTML 表(不是 Gridview)自定义结构和数据导出到 Excel。

尝试以下方法

  1. 提供ID并添加runat="server"属性

  2. 添加以下代码

    Response.ContentType = "application/x-msexcel"; 
    Response.AddHeader("内容处置", "附件;
    文件名=ExcelFile.xls");
    Response.ContentEncoding = Encoding.UTF8; 
    StringWriter tw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(tw);
    tbl.RenderControl(hw);
    Response.Write(tw.ToString());
    响应.End();
    

You want Export HTML table (Not Gridview) customized structure and data to Excel using ASP.NET.

Try the following Approach

  1. Provide the ID and add runat="server" attribute

    <table id="tbl" runat="server" >

  2. Add the following code

    Response.ContentType = "application/x-msexcel"; 
    Response.AddHeader("Content-Disposition", "attachment;
    filename=ExcelFile.xls");
    Response.ContentEncoding = Encoding.UTF8; 
    StringWriter tw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(tw);
    tbl.RenderControl(hw);
    Response.Write(tw.ToString());
    Response.End();
    
日久见人心 2025-01-14 12:30:39

您可以使用下面的代码:

Response.ContentType = "application/force-download";
Response.AddHeader("content-disposition", "attachment; filename=Print.xls");
Response.Write("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
Response.Write("<head>");
Response.Write("<META http-equiv=\"Content-Type\" content=\"text/html; charset=utf-     8\">");
Response.Write("<!--[if gte mso 9]><xml>");
Response.Write("<x:ExcelWorkbook>");
Response.Write("<x:ExcelWorksheets>");
Response.Write("<x:ExcelWorksheet>");
Response.Write("<x:Name>Report Data</x:Name>");
Response.Write("<x:WorksheetOptions>");
Response.Write("<x:Print>");
Response.Write("<x:ValidPrinterInfo/>");
Response.Write("</x:Print>");
Response.Write("</x:WorksheetOptions>");
Response.Write("</x:ExcelWorksheet>");
Response.Write("</x:ExcelWorksheets>");
Response.Write("</x:ExcelWorkbook>");
Response.Write("</xml>");
Response.Write("<![endif]--> ");
StringWriter tw = new StringWriter(); 
HtmlTextWriter hw = new HtmlTextWriter(tw);      
tbl.RenderControl(hw);
Response.Write(tw.ToString());
Response.Write("</head>");
Response.flush();

如果您希望导出的输出看起来与您的 UI 完全相同,建议提供内联 css。如果您将 css 类应用于表格,那么它不会显示在导出的 Excel 中。

You can use below code:

Response.ContentType = "application/force-download";
Response.AddHeader("content-disposition", "attachment; filename=Print.xls");
Response.Write("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
Response.Write("<head>");
Response.Write("<META http-equiv=\"Content-Type\" content=\"text/html; charset=utf-     8\">");
Response.Write("<!--[if gte mso 9]><xml>");
Response.Write("<x:ExcelWorkbook>");
Response.Write("<x:ExcelWorksheets>");
Response.Write("<x:ExcelWorksheet>");
Response.Write("<x:Name>Report Data</x:Name>");
Response.Write("<x:WorksheetOptions>");
Response.Write("<x:Print>");
Response.Write("<x:ValidPrinterInfo/>");
Response.Write("</x:Print>");
Response.Write("</x:WorksheetOptions>");
Response.Write("</x:ExcelWorksheet>");
Response.Write("</x:ExcelWorksheets>");
Response.Write("</x:ExcelWorkbook>");
Response.Write("</xml>");
Response.Write("<![endif]--> ");
StringWriter tw = new StringWriter(); 
HtmlTextWriter hw = new HtmlTextWriter(tw);      
tbl.RenderControl(hw);
Response.Write(tw.ToString());
Response.Write("</head>");
Response.flush();

Also it is advicable to give inline css if you want the exported output look exactly same as your UI. If you apply css classes to the table then it would not be displayed in exported excel.

小瓶盖 2025-01-14 12:30:39

如果 dtReport 包含表格(即要导出的数据),那么我们可以使用以下 LOC 将表格导出到 Excel,我们也可以

    if (dtReports != null && dtReports.Rows.Count > 0 && !string.IsNullOrEmpty(formName))
            {
                string filename = formName.ToUpper() + ParsConstant.XLS_EXTENSION;
                StringWriter tw = new StringWriter();

                using (HtmlTextWriter hw = new HtmlTextWriter(tw))
                {

                    //Binding Datatable to DataGrid.
                    DataGrid dgGrid = new DataGrid();
                    dgGrid.DataSource = dtReports;
                    dgGrid.DataBind();

                    //Some Properties for the Header
                    dgGrid.HeaderStyle.Font.Bold = true;
                    dgGrid.HeaderStyle.Font.Size = 13;

                    //Get the HTML for the control.
                    dgGrid.RenderControl(hw);


                    Response.ContentType = "application/vnd.ms-excel";
                    Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
                    //Response.Write("<style> TD { mso-number-format:\\@; } </style>");


                    Response.Write(tw.ToString());
                    Response.End();
                }
            }

使用 MSO 格式格式化标题,不会避免前导零,但它会将文本转换为字符串这对于进行操作来说是不可取的。

If dtReport contains the table(i.e data to be exported) then we can export the table to excel by using the following LOC and also we can format the header

    if (dtReports != null && dtReports.Rows.Count > 0 && !string.IsNullOrEmpty(formName))
            {
                string filename = formName.ToUpper() + ParsConstant.XLS_EXTENSION;
                StringWriter tw = new StringWriter();

                using (HtmlTextWriter hw = new HtmlTextWriter(tw))
                {

                    //Binding Datatable to DataGrid.
                    DataGrid dgGrid = new DataGrid();
                    dgGrid.DataSource = dtReports;
                    dgGrid.DataBind();

                    //Some Properties for the Header
                    dgGrid.HeaderStyle.Font.Bold = true;
                    dgGrid.HeaderStyle.Font.Size = 13;

                    //Get the HTML for the control.
                    dgGrid.RenderControl(hw);


                    Response.ContentType = "application/vnd.ms-excel";
                    Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
                    //Response.Write("<style> TD { mso-number-format:\\@; } </style>");


                    Response.Write(tw.ToString());
                    Response.End();
                }
            }

using MSO Format will not be avoiding leading zero's,but it will convert the text to string which is not advisable for doing operations.

影子是时光的心 2025-01-14 12:30:39

没有自动化的方法。但是您可以使用相同的代码来创建表并将其写入输出。如果您将其编写为简单的 CSV 文件,则用户只需单击下载的文件即可将其加载到 Excel 中。

通过设置正确的标头,您可以指示浏览器将内容视为下载内容而不是网页。我在本文中发布了执行此操作的代码。

There's no automated way. But you could use the same code to create the table and write it to the output instead. If you write it as a simple CSV file, then the user can load it into Excel by simply clicking the downloaded file.

By setting the correct headers, you can direct the browser to treat the content as a download instead of a web page. I posted the code to do this in this article.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文