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

14
我有一个HTML表格(不是GridView),它没有适当的标题和行。相反,它具有自定义的结构和数据。我想使用ASP.NET将此表导出到Excel。我该怎么做? enter image description here标签是固定文本,整数值来自数据库。因此,表格结构是固定的,只有整数/小数值会更改。

1
这需要是一个可重复的过程吗?还是只需从您的网页中复制粘贴一次即可? - Randy
2
表格的数据来自哪里? - Didaxis
与其他人不同,我会解释我为何给您的问题投了反对票:这是一个贫乏的问题,缺乏足够的细节来引出任何有价值的实际答案。请提供更多细节,否则我怀疑这个问题将被关闭。 - Didaxis
4个回答

9
你想使用ASP.NET将自定义结构和数据的HTML表(而非Gridview)导出到Excel。尝试以下方法:
  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();
    

1
非常好。如果我想在Excel文件中放置图像,或者填充单元格颜色等,该怎么办?我的意思是,在导出后格式化Excel文件。 - Haminteu
1
检查以下链接:https://dev59.com/i1rUa4cB1Zd3GeqPlJNz#9858841 和 https://dev59.com/72DVa4cB1Zd3GeqPgcS8#9653194,这些与编程有关。 - Arun Singh
1
我正在尝试在我的SP网站中使用Visual Web Part的导出功能。Response 给了我一个错误。 - SearchForKnowledge

4
您可以使用以下代码:
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中显示。


2
不一定需要内联CSS,您可以在头部添加样式类,它将正常工作。 - Kronass

1
如果dtReport包含表格(即要导出的数据),则可以使用以下代码将表格导出到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 格式不会避免前导零,但它会将文本转换为字符串,这对于进行操作是不可取的。

0

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

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


1
谁是那个没有解释就投反对票的笨蛋? - Jonathan Wood
我并不是那个点踩的“小丑”:),但也许你的回答被点踩了,因为原帖明确说明这不是在GridView中-也没有提示此数据从何而来。我们所知道的是,这可能是一个HTML表格,位于原帖作者无法控制的网站上。 - Didaxis
@ErOx:好的,收到。 :) 鉴于原帖作者说他正在使用ASP.NET,我会假设他是从自己的网站上做这个。但我同意这个问题存在歧义。 - Jonathan Wood

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接