导出最简单方法:

const string style = @"<style> .text { mso-number-format:\@; } </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 
= "<div style=\"width:100%;font-bold:true;text-align:center;\">" +Name + "</div>";
            
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 = @"<style> .text {mso-number-format:\@; } </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 
= "<div style=\"width:100%;font-bold:true;text-align:center;\">" + strFileName + "</div>";
                
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%
posted on 2009-02-17 15:17  茹古涵今  阅读(1148)  评论(0编辑  收藏  举报