asp.net导出excel及科学计数问题

导出最简单方法:

const   string  style  =   @"   " ;
            Response.ClearContent();

            Response.AddHeader(
" content-disposition " " attachment; filename= "   +  Server.UrlEncode(Name)  +   " .xls " );

            Response.ContentType 
=   " application/excel " ;

            StringWriter sw 
=   new  StringWriter();

            HtmlTextWriter htw 
=   new  HtmlTextWriter(sw);
            
// htw.WriteLine(Name);
            System.Web.UI.LiteralControl lt  =   new  LiteralControl();
            lt.Text 
=   " " width: 100 % ;font - bold: true ;text - align:center;\ " > "   + Name  +   "
" ;
            
if (Name != "" )lt.RenderControl(htw);
            gvdata.RenderControl(htw);
            
//  Style is added dynamically

            Response.Write(style);

            Response.Write(sw.ToString());

            Response.End();

 

导出出现科学计数解决办法一:

 

GridView gvdata  =   new  GridView();
                gvdata.HeaderStyle.BackColor 
=  System.Drawing.Color.Silver;
                gvdata.HeaderStyle.Font.Bold 
=   true ;
                gvdata.HeaderStyle.HorizontalAlign 
=  HorizontalAlign.Center;
                gvdata.RowDataBound 
+=   new  GridViewRowEventHandler(gvdataRowDataBound);
                gvdata.DataSource 
=  dtData;
                gvdata.DataBind();
                
const   string  style  =   @"   " ;
                Response.ClearContent();

                Response.AddHeader(
" content-disposition " " attachment; filename= "   +  Server.UrlEncode(strFileName)  +   " .xls " );

                Response.ContentType 
=   " application/excel " ;

                StringWriter sw 
=   new  StringWriter();

                HtmlTextWriter htw 
=   new  HtmlTextWriter(sw);
                
// htw.WriteLine(Name);
                System.Web.UI.LiteralControl lt  =   new  LiteralControl();
                lt.Text 
=   " " width: 100 % ;font - bold: true ;text - align:center;\ " > "   +  strFileName  +   "
" ;
                
if  (strFileName  !=   "" ) lt.RenderControl(htw);
                gvdata.RenderControl(htw);
                
//  Style is added dynamically

                Response.Write(style);

                Response.Write(sw.ToString());

                Response.End();
    
public   void  gvdataRowDataBound( object  sender, GridViewRowEventArgs e)
    {
        
if  (e.Row.RowType  ==  DataControlRowType.DataRow)
        {
            e.Row.Cells[
0 ].Attributes.Add( " style " " vnd.ms-excel.numberformat:@ " );
            e.Row.Cells[
2 ].Attributes.Add( " style " " vnd.ms-excel.numberformat:@ " );
        }
    }
另注:  // 1)  文本:vnd.ms-excel.numberformat:@
         
// 2)  日期:vnd.ms-excel.numberformat:yyyy/mm/dd
        
// 3)  数字:vnd.ms-excel.numberformat:#,##0.00
        
// 4)  货币:vnd.ms-excel.numberformat:¥#,##0.00
        
// 5)  百分比:vnd.ms-excel.numberformat: #0.00%

你可能感兴趣的:(asp.net导出excel及科学计数问题)