2012-12-28 66 views
0

我通過GridView將數據從DataSet導出到Excel。框架是2.0。我正在使用下面的代碼片段:將DataSet/Gridview導出到Excel的格式列

Generate_Sequence_Data_BAL objAttBAL = new Generate_Sequence_Data_BAL(); 
    string PlanId = ""; 
    PlanId = Cryptography.Decrypt(Request.QueryString[0].ToString()); 
    //Get the data from database into datatable 
    Response.ClearHeaders(); 
    Response.Clear(); 
    Response.Buffer = true; 
    Response.AddHeader("content-disposition", "attachment;filename=SequenceData_" + Cryptography.Decrypt(Request.QueryString[0].ToString()) + ".xls"); 
    Response.Charset = ""; 
    Response.ContentType = "application/vnd.ms-excel"; 
    StringWriter sw = new StringWriter(); 
    HtmlTextWriter hw = new HtmlTextWriter(sw); 

    DataSet ds = objAttBAL.GetData(Convert.ToInt32(PlanId)); 
    GridView GridView1 = new GridView(); 
    GridView1.RowDataBound += GridView1_RowDataBound; 
    GridView1.DataSource = ds.Tables[0]; 
    GridView1.DataBind(); 

    for (int i = 0; i < GridView1.Rows.Count; i++) 
    { 
     //Apply text style to each Row 
     GridView1.Rows[i].Attributes.Add("class", "textmode"); 
    } 
    GridView1.RenderControl(hw); 
    //style to format numbers to string 
    string style = @"<style>.textmode{mso-number-format:'\@'}</style>"; 
    Response.Write(style); 
    Response.Write(sw.ToString()); 
    Response.Flush(); 
    GridView1.Dispose(); 
    Response.End(); 

我有一個數據像'5E02'的列。即使使用上述方法,它仍然以「5.00E + 02」輸出。我試圖從SQL中檢索連接('),但它然後顯示爲('5E02),而不是(5E02)。

請幫忙。還建議任何其他替代方案直接導出數據到沒有gridview的excel。

PS:我已經提到this question &它不工作,所以它不是重複的。

回答

0

我通過在列的末尾連接Alt + 0160(Non-breaking Space)解決了該問題。

Select <Column Name>+'<Alt+0160>'

現在柱正被導出爲 '5E02' 而不是 '5.00E + 02'。