Response.Clear();
Response.Buffer= true;
Response.ContentType = "application/ms-excel";  
Response.AppendHeader("Content-Disposition","attachment;filename=FileFlow.xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");;
Response.Charset="UTF-8";
this.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();;
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.DataGrid1.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();我用以上的代码,从datagrid中汇出excel  我的斓位有100个,值里面有汉字
汇出的时候汉字出现乱吗但是我将栏位缩减至30个时候就能正常显示了请高手帮忙,我想显示100个阑尾,汇出为什么会是乱吗呢....

解决方案 »

  1.   

    这个是我用来导了,多少都可以
    string strdata = String.Empty;
    strdata=GetData();
    string temp = string.Format("attachment;filename={0}","Hold.csv");
    Response.ClearHeaders();
    Response.AppendHeader("Content-disposition", temp);
    Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
    Response.Write(strdata);
    Response.End();
    ToExcel(this.dtgDetail);
    public static  void ToExcel(System.Web.UI.Control ctl)   

    HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=Excel.xls"); 
    HttpContext.Current.Response.Charset ="UTF-8";     
    HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.Default; 
    HttpContext.Current.Response.ContentType ="application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword 
    ctl.Page.EnableViewState =false;    
    System.IO.StringWriter  tw = new System.IO.StringWriter() ; 
    System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (tw); 
    ctl.RenderControl(hw); 
    HttpContext.Current.Response.Write(tw.ToString()); 
    HttpContext.Current.Response.End(); 

    private string GetData()
    {
    TempTable =(DataTable)Session["Table"];
    StringBuilder strdata=new StringBuilder(String.Empty) ;
    string strtemp = String.Empty; strdata.Append(this.lblCaption.Text);
    strdata.Append("\n");
    //strdata.Append(this.lblCutOff.Text);
    //strdata.Append("\n");

    for(int k=0;k<TempTable.Columns.Count;k++)
    {
    strdata.Append("\"");
    strdata.Append(TempTable.Columns[k].ColumnName.ToString());
    strdata.Append("\",");
    }
    strdata.Append("\n"); for(int i=0;i<TempTable.Rows.Count;i++)
    {
    for(int j=0;j<TempTable.Columns.Count;j++)
    {
    strtemp=TempTable.Rows[i][j].ToString();
    if (! strtemp.Equals("&nbsp;"))
    {
    // if(strtemp.IndexOf("<")>0)
    // {
    // strdata.Append("\"");
    // strdata.Append(strtemp.Substring(strtemp.IndexOf("<")+1,strtemp.LastIndexOf(">")-strtemp.IndexOf("<")-1));
    // strdata.Append("\",");
    // }
    // else
    // {
    strdata.Append("\"");
    strdata.Append(strtemp);
    strdata.Append("\",");
    //}
    }
    else
    strdata.Append("\"\",");
    }
    strdata.Append("\n"); } return strdata.ToString(); }
    我这个导页面上DataGrid的,你试看看
      

  2.   

    'Response.Clear()
            'Response.Charset = "GB2312"
            'Response.Buffer = True
            'EnableViewState = False
            'Response.ContentEncoding = System.Text.Encoding.Default
            Response.ContentType = "application/octet-stream"
            'Response.AppendHeader("Content-Disposition", "attachment;filename=" & HttpUtility.UrlEncode(saveFileName))
            Response.WriteFile(filename)
            Response.Flush()
            Response.Close()
            Response.End()
      

  3.   

    Response.Clear();
            Response.ClearHeaders();
            Response.Charset = "GB2312";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
    Response.ContentType = "application/octet-stream"
    试试
      

  4.   

    Response.Clear();
            Response.Buffer = true;
            Response.Charset = "GB2312";
            Response.AppendHeader("Content-Disposition","attachment;filename=work.xls");
            Response.ContentEncoding = System.Text.Encoding.UTF7;
            Response.ContentType = "application/ms-excel";
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
            HtmlTextWriter oHtmlTextWriter = new HtmlTextWriter(oStringWriter);
            GridView1.RenderControl(oHtmlTextWriter);
            Response.Output.Write(oStringWriter.ToString());
            Response.Flush();
            Response.End();
      

  5.   

    :stephenlll(Stephen谢谢
    搞定了
    Response.Output.Write(oStringWriter.ToString());
    不同的是这个地方