代码改变世界

C# 导出excel文件处理科学计数法办法

2013-10-22 19:16  calm_水手  阅读(1549)  评论(0编辑  收藏  举报

在邦定gridview控件时在rowdatabound事件中队数据格式化

protected void DataGridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
/* 文本:vnd.ms-excel.numberformat:@          
  日期:vnd.ms-excel.numberformat:yyyy/mm/dd         
  数字:vnd.ms-excel.numberformat:#,##0.00         
  货币:vnd.ms-excel.numberformat:¥#,##0.00         
  百分比:vnd.ms-excel.numberformat: #0.00%    
**/ 
 for (int i = 0; i < e.Row.Cells.Count; i++)        
 {
  if (e.Row.RowType == DataControlRowType.DataRow)
  {   
   e.Row.Cells[i ].Attributes.Add("style", "vnd.ms-excel.numberformat:@");        
  }               
 }
}   
   
protected void btnOut_Click(object sender, EventArgs e)    
{               
 Response.Clear();      
 Response.Buffer = true;        
 Response.Charset = "GB2312";       
 Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls");             
 Response.ContentEncoding = System.Text.Encoding.UTF7;     
 //设置输出文件类型为excel文件。     
 Response.ContentType = "application/ms-excel";       
 System.IO.StringWriter oStringWriter = new System.IO.StringWriter();       
 System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);       
 this.gError.RenderControl(oHtmlTextWriter);        
 Response.Output.Write(oStringWriter.ToString());        
 Response.Flush();        
 Response.End();    
 }