2011-10-13 30 views
2

我將SP的內容導出爲ex​​cel。其中一列將日期格式設置爲08/2015,但導出爲ex​​cel時,格式將更改爲2015年8月。導出爲ex​​cel失去日期格式

我做了一個谷歌相同,發現包括下面的代碼沒有辦法;

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

導出爲ex​​cel(數據集爲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(); 
    } 

我在哪裏犯錯?請指導!

謝謝!

+0

沒有評論???請幫助 –

回答

0

我真的不明白一點點的代碼(不是流利的asp.net),但我會說,如果你想強制在Excel表格中的文本,你需要定義目標區域爲文本之前把你的數據在那裏。

如果我的代碼的理解是正確的:

response.Write(style); 

需要是在此之前。

dgrExport.RenderControl(htmlWrite); 

編輯:也許一個替代的解決方案

你已經找到了的谷歌碼位設置細胞作爲文本的格式。在所有可能的情況下,您希望擅長將日期作爲顯示格式爲MM/YYYY的日期處理。

也許嘗試更換此:

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

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

我不知道是否/或\是在ASP.net中的轉義字符,以便準確snytax可能會有所不同。在Excel中,數字格式@表示文本,mm/yyyy表示具有所需顯示格式的日期。

+0

即使您的修改後的代碼不起作用! –

+0

我已經添加了別的東西來嘗試 – Pynner

+0

指定一個'也會帶來正確格式的日期 –

2

問題不是日期格式,Excel根據CELL的DataType(Default is GENERAL)轉換數據。爲防止數據轉換,您必須提供數據類型(TEXT)以及數據。

您使用了正確的代碼,但樣式表.text未應用於您的數據。在所有<TD>標籤上應用樣式表。它將100%的工作,並會保留你的數據,你會提供(Date- 08/2015, 0001 or any data)。

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

史密斯,添加的'是會改變你的原始數據。你有沒有嘗試過上面的代碼? –

+0

是.... string style = @「」;工作完美 – hks

1

這是一些示例代碼。

Response.AddHeader("content-disposition", "attachment; filename=Report.xls"); 
Response.Charset = ""; 
Response.ContentType = "application/vnd.xls"; 
System.IO.StringWriter stringWrite = new System.IO.StringWriter(); 
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); 
DataGrid g = new DataGrid(); 
DataTable d = new System.Data.DataTable(); 
d = (DataTable)Session["ReportData"];   
g.DataSource = d; 
g.DataBind();  
foreach (DataGridItem i in g.Items) 
{ 
    foreach (TableCell tc in i.Cells) 
     tc.Attributes.Add("class", "text"); 
} 
g.RenderControl(htmlWrite); 
string style = @"<style> .text { mso-number-format:\@; } </style> "; 
Response.Write(style); 
Response.Write(stringWrite.ToString()); 
Response.End();