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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9364107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 22:39:42  来源:igfitidea点击:

Export HTML Table to Excel using ASP.NET

htmlasp.nethtml-tableexport-to-excel

提问by hotcoder

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

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

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.

标签是固定文本,整数值来自数据库。所以表结构是固定的,只有整数/十进制值发生变化。

采纳答案by Arun Singh

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

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

Try the following Approach

尝试以下方法

  1. Provide the IDand 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();
    
  1. 提供ID和添加runat="server"属性

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

  2. 添加以下代码

    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();
    

回答by user3215480

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.

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

回答by Deepak Kothari

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

如果 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();
                }
            }

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.

使用 MSO 格式不会避免前导零,但它会将文本转换为不建议进行操作的字符串。

回答by IrishChieftain

If your data is databased (if not you could store in an XML file), then look at a similar question I posted recently.

如果您的数据已数据库化(如果没有,您可以存储在 XML 文件中),然后查看我最近发布的类似问题。

Writing DataReader Rows to Excel File

将 DataReader 行写入 Excel 文件

回答by Jonathan Wood

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.

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

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.

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