【问题标题】:Best way to turn sql selected data into a table on excel using asp.net使用asp.net将sql选择的数据转换为excel表的最佳方法
【发布时间】:2014-11-06 10:00:22
【问题描述】:

我有一个 sql 语句,它选择我想要以 .xls 格式导出到 excel 的数据表, 我将此表添加到网格视图中,然后渲染该网格视图以创建一个 html 编写器并使用 asp.net 将其写入 excel 文件。

但我一直收到文件格式和扩展名不匹配的警告。 问题是您正在创建的文件不是真正的 Excel 文件。它是带有 .xls 扩展名的 HTML。

拜托,我需要知道在没有警告的情况下将这些选定的数据导出到 xls 文件的最佳方法是什么。

【问题讨论】:

    标签: mysql asp.net excel gridview export


    【解决方案1】:

    我也试过直接从dataTable导出,但是打开excel时还是会收到警告。

       // these namespaces need to be added to your code behind file
    using System.Configuration;
    using System.Data.SqlClient;
    using System.Data;
    
    namespace MySpot.UserPages
    {
        public partial class Journal : System.Web.UI.Page
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MySpotDBConnStr"].ConnectionString);
            DataTable dt = new DataTable();
    
            // regular page_load from .aspx file
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                }
            }
    
            // added a button with ID=btnDownload and double clicked it's onclick event to auto create method
            protected void btnDownload_Click(object sender, EventArgs e)
            {
                string queryStr = "SELECT * from table";
                SqlDataAdapter sda = new SqlDataAdapter(queryStr, conn);
                sda.Fill(dt);
                ExportTableData(dt);
            }
    
            // this does all the work to export to excel
            public void ExportTableData(DataTable dtdata)
            {
                string attach = "attachment;filename=journal.xls";
                Response.ClearContent();
                Response.AddHeader("content-disposition", attach);
                Response.ContentType = "application/ms-excel";
                if (dtdata != null)
                {
                    foreach (DataColumn dc in dtdata.Columns)
                    {
                        Response.Write(dc.ColumnName + "\t");
                        //sep = ";";
                    }
                    Response.Write(System.Environment.NewLine);
                    foreach (DataRow dr in dtdata.Rows)
                    {
                        for (int i = 0; i < dtdata.Columns.Count; i++)
                        {
                            Response.Write(dr[i].ToString() + "\t");
                        }
                        Response.Write("\n");
                    }
                    Response.End();
                }
            }
        }
    }
    

    【讨论】:

    • 警报是 Excel 2007 中称为扩展强化的新安全功能,可确保打开的文件内容与尝试打开文件的 shell 命令中指定的扩展类型相匹配。 ...此问题仍在调查中,但考虑到代码复杂性的性质,以及 Excel 不希望降低安全措施以解决 IE 打开行为而没有完整的了解对其他浏览器用户的后果。
    【解决方案2】:

    http://blogs.msdn.com/b/vsofficedeveloper/archive/2008/03/11/excel-2007-extension-warning.aspx

    当前的设计不允许您在 Excel 中打开来自网站的 HTML 内容,除非 URL 的扩展名为 .HTM/.HTML/.MHT/.MHTML。因此,返回 HTML 并将 MIME 类型设置为类似 XLS 以尝试强制 HTML 在 Excel 而不是 Web 浏览器中打开的 ASP 页面(如预期的那样)将始终收到安全警报,因为内容与 MIME 类型不匹配。如果您使用 HTML MIME 类型,则 Web 浏览器将打开内容而不是 Excel。因此,对于这种情况没有好的解决方法,因为缺少针对 Excel 特定的 HTML/MHTML 的特殊 MIME 类型。如果您同时控制需要访问它的 Web 服务器和客户端桌面,则可以添加自己的 MIME 类型,但最好的选择是使用不同的文件格式或警告用户警告并告诉他们选择是对话框。

    【讨论】:

      猜你喜欢
      • 2018-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-24
      • 1970-01-01
      • 1970-01-01
      • 2010-09-23
      • 2014-10-15
      相关资源
      最近更新 更多