我通过 GridView 将数据从 DataSet 导出到 Excel。框架是2.0。我正在使用以下代码片段:
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:我已经提到了这个问题 并且它不起作用,所以它不是重复的。