public void ExportToExcel(string FileName, string filePath)
        {
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            this.gvOrder.RenderControl(htw);            string strHtml = sw.ToString().Trim();
            strHtml = strHtml.Substring(1, strHtml.Length - 1);
            string FilePhysicialPathName = Request.PhysicalApplicationPath + filePath + "\\";            //生成的Excel文件名
            string objectExcelFileName = Path.Combine(FilePhysicialPathName, FileName);
            if (File.Exists(objectExcelFileName))
            {
                File.Delete(objectExcelFileName);
            }
            FileStream fs = new FileStream(objectExcelFileName, FileMode.Create);
            BinaryWriter bw = new BinaryWriter(fs, Encoding.GetEncoding("GB2312"));
            bw.Write(strHtml);
            bw.Close();
            fs.Close();
            sw.Dispose();
            htw.Dispose();        }

解决方案 »

  1.   

    分页是自定义设置的,可以导出,不会报错的,就是数据超过10来条就成乱码了,只几条数据的时候是好好的
      

  2.   

    你导出的文件放在服务器端吗
      

  3.   

    换一种方式吧,我做过一个和你的不一样,回头我找找忘记代码放哪了
      

  4.   

    代码放服务器段的,就是保存好了可以下载的
      

  5.   

    是所有中文都是乱码 还是十几行后中文乱码 查看fs的编码方式
      

  6.   

    就是上面一段代码导出并保存的,数据超过十几条了全部中文都是乱码,