asp.net - 导出到excel丢失日期格式

标签 asp.net visual-studio gridview export-to-excel

我正在将 SP 的内容导出到 excel。其中一列的日期格式为 08/2015,但在导出到 Excel 时,格式更改为 2015 年 8 月。

我用谷歌搜索了一下,发现包含下面的代码就可以了;

string style = @"<style> .text { mso-number-format:\@; } </style> ";

导出到 excel(数据集到 excel)的工作如下;

 /// <summary>
    /// This method can be used for exporting data to excel from dataset
    /// </summary>
    /// <param name="dgrExport">System.Data.DataSet</param>
    /// <param name="response">System.Web.Httpresponse</param>
    public static void DataSetToExcel(System.Data.DataSet dtExport, System.Web.HttpResponse response, string strFileName)
    {

        string style = @"<style> .text { mso-number-format:\@; } </style> ";

        //Clean up the response Object
        response.Clear();
        response.Charset = "";

        //Set the respomse MIME type to excel
        response.ContentType = "application/vnd.ms-excel";

        //Opens the attachment in new window
        response.AddHeader("Content-Disposition", "attachment; filename=" + strFileName.ToString() + ".xls;");
        response.ContentEncoding = Encoding.Unicode;
        response.BinaryWrite(Encoding.Unicode.GetPreamble());

        //Create a string writer
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();

        //Create an htmltextwriter which uses the stringwriter
        System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);

        //Instantiate the datagrid

        System.Web.UI.WebControls.GridView dgrExport = new System.Web.UI.WebControls.GridView();

        //Set input datagrid to dataset table
        dgrExport.DataSource = dtExport.Tables[0];

        //bind the data with datagrid
        dgrExport.DataBind();

        //Make header text bold
        dgrExport.HeaderStyle.Font.Bold = true;

        //bind the modified datagrid
        dgrExport.DataBind();

        //Tell the datagrid to render itself to our htmltextwriter
        dgrExport.RenderControl(htmlWrite);

        response.Write(style);

        //Output the HTML
        response.Write(stringWrite.ToString());

        response.End();
    }

我哪里出错了?请指导!

谢谢!

最佳答案

问题不在于日期格式,Excel 根据 CELL 的数据类型 (Default is GENERAL) 转换数据。为防止数据转换,您必须提供数据类型 ( TEXT) 以及数据。

您使用了正确的代码,但是样式表 .text不适用于您的数据。在所有 <TD> 上应用样式表标签。它将 100% 正常工作,并将保留您提供的数据 (Date- 08/2015, 0001 or any data)。

string style = @"<style> TD { mso-number-format:\@; } </style> ";

关于asp.net - 导出到excel丢失日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7750603/

相关文章:

asp.net - url 内的双转义序列 : The request filtering module is configured to deny a request that contains a double escape sequence

ASP.NET 多个 Eval 字段

android - 在网格布局的适配器中应用排序

android - 如何使用android中的imageAdapter将特定文件夹的sdcard中的图像上传到 GridView

asp.net - 无法从模式对话框下载,window.showModalDialog

c# - WCF 双工 : Send callback to a specific subscribed ASP. NET Webforms 客户端

html - 如何在垂直对齐的 gridView 单元格中显示最多 3 行内容?

visual-studio - 如何区分 Visual Studio 项目类型和现有 Visual Studio 项目

c++ - 在 Visual Studio 的 C++ 项目中是否可以使用宏来识别当前的 SOLUTION 配置?

c - visual studio 2008 目标机器类型列表