當我將datagrid中的數據轉為excel的時候,如果數據大與3條,轉出來的就很正常,
如果數據小與3條的話,轉出來的就是亂碼,有哪位兄弟姐妹能不能解釋一下啊
代碼如下:(我用繁體操作系統)
System.Web.HttpResponse httpResponse = Page.Response;
System.Web.UI.WebControls.DataGrid dataGrid=new System.Web.UI.WebControls.DataGrid();

dataGrid.DataSource=dt.DefaultView;
dataGrid.DataBind();httpResponse.AppendHeader("Content-Disposition","attachment;filename="+HttpUtility.UrlEncode(p_DecDocName,System.Text.Encoding.Unicode)); //filename="*.xls";httpResponse.ContentEncoding=System.Text.Encoding.GetEncoding("BIG5");
httpResponse.ContentType ="application/vnd.ms-excel";
System.IO.StringWriter  tw = new System.IO.StringWriter() ;
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
dataGrid.RenderControl(hw);  
string filePath = Page.Server.MapPath("..")+"\\MatCode\\FilePack\\" +p_DecDocName;
System.IO.StreamWriter sw = System.IO.File.CreateText(filePath);
sw.Write(tw.ToString());
sw.Close();

解决方案 »

  1.   

    StreamWriter swFromFileTrueUTF8 = 
            new StreamWriter(fileName, true,
            System.Text.Encoding.UTF8);
      

  2.   

    你可以dataset 导入 excel参考
    http://community.csdn.net/Expert/topic/3077/3077526.xml?temp=.8746912
    http://www.dev-club.com/club/bbs/showEssence.asp?id=26350http://dev.csdn.net/Develop/article/18/18623.shtm
    http://community.csdn.net/Expert/topic/3112/3112296.xml?temp=.926861
    http://dotnet.aspx.cc/ShowDetail.aspx?id=BF0A54F9-C7C7-4200-BD9A-802AC1F5DE50
    http://expert.csdn.net/Expert/TopicView1.asp?id=2928057www.foxhis.com/powermjtest/
    原文代码:
    private void Button1_Click(object sender, System.EventArgs e)
    {
      //写入Excel的方法:
      //定义需要参数。
      string SourceFile="Data.XLS";                                //源文件名称。
      string TemplatePath=Server.MapPath("ExcelTemplate");    //存放源文件的文件夹路径。
      string DownloadPath=Server.MapPath("ExcelDownload");    //副本的文件夹路径。
      //副本的文件名。
      string TempFileName = DateTime.Now.ToString("yyyyMMdd") + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + ".XLS";  
      object missing = System.Reflection.Missing.Value;
      Excel.Application myExcel=new Excel.Application();
      //打开新文件
      myExcel.Application.Workbooks.Open(TemplatePath+"\\"+SourceFile,missing,missing,missing,missing,
    missing,missing,missing,missing,missing,missing, missing,missing); 
      Excel.Workbook myBook=myExcel.Workbooks[1];
      Excel.Worksheet curSheet = (Excel.Worksheet)myBook.Sheets[2];

      string DownloadFilePath=DownloadPath+"\\"+TempFileName;

      int i=0;
      while (i<=10)
      {
        myExcel.Cells[4+i,2]=i.ToString();
        myExcel.Cells[4+i,3]=i.ToString();
        myExcel.Cells[4+i,4]=i.ToString();
        myExcel.Cells[4+i,5]=i.ToString();
        myExcel.Cells[4+i,6]=i.ToString();
        i++;
      }   

      myBook.Saved=true;
      //myBook.SaveAs(DownloadFilePath,missing,"","",false,false,Excel.XlSaveAsAccessMode.xlNoChange,1,false,missing,missing);

      myBook.PrintPreview(0);
      //myBook.PrintOut(missing,missing,missing,missing,missing,missing,missing,missing);
      myBook.Close(false, null,null);
      myExcel.Quit();
      System.Runtime.InteropServices.Marshal.ReleaseComObject(myBook);
      System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcel);
      myBook = null;
      myExcel = null;
      GC.Collect();
      //Response.Redirect("ExcelDownload//"+TempFileName); //下载文件
    }
    string path = Server.MapPath(this.xlfile.Text+".xls"); System.IO.FileInfo file = new System.IO.FileInfo(path);
    Response.Clear();
    Response.Charset="GB2312";
    Response.ContentEncoding=System.Text.Encoding.UTF8;
    // 添加头信息,为"文件下载/另存为"对话框指定默认文件名
    Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
    // 添加头信息,指定文件大小,让浏览器能够显示下载进度
    Response.AddHeader("Content-Length", file.Length.ToString());

    // 指定返回的是一个不能被客户端读取的流,必须被下载
    Response.ContentType = "application/ms-excel";

    // 把文件流发送到客户端
    Response.WriteFile(file.FullName);
    // 停止页面的执行

    Response.End();