2012-12-28 41 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'。