2011-09-01 28 views

回答

0

只需使用樣式表格

protected void Btn_ExportClick(object sender, EventArgs e) 
{ 

    string style = @"<style> .text { mso-number-format:\@; } </style> "; 
    Response.ClearContent(); 
    Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls"); 
    Response.ContentType = "application/excel"; 
    StringWriter sw = new StringWriter(); 
    HtmlTextWriter htw = new HtmlTextWriter(sw); 
    gvUsers.RenderControl(htw); 

    // Style is added dynamically 
    Response.Write(style); 
    Response.Write(sw.ToString()); 
    Response.End(); 
} 

protected void gvUsers_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) { 
     e.Row.Cells[1].Attributes.Add("class", "text"); 
    } 
} 
0

字符串引號,使Excel將其作爲一個字符串,而不是數字

+0

我不希望看到的 「」我只想回答爲0001不是「0001」 –

0

00001不是數字,1是...因爲沒有數字可以從0開始,除非數值本身是0.
如果您想讓它顯示00001,請使用qoutes「」。所以它變成了「00001」。
這也是excel在將列或單元轉換爲一般文本時的功能。

我覺得沒有其他辦法可以做到。

您可以隨時向Microsoft發送包含您投訴的電子郵件,並要求提供更新以解決此問題:)。

0

有一種更好的方法來達到同樣的效果,只需添加一條線,它會工作。使用循環創建樣式表(.text)並添加所有標籤的屬性,而不是使用循環在所有TD標籤上直接應用樣式。

更換

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

隨着

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

和消除環路

+0

amitvyas100688 - 你有沒有試過上面的代碼?這個有幫助嗎? –