日期:2014-05-18  浏览次数:20433 次

gridview导出excel问题相关。
导出方法如下: 
  Response.Clear();
  Response.Buffer = false;
  Response.Charset = "utf-8";
  Response.AppendHeader("Content-Disposition", "attachment;filename=自检统计报表.xls");
  Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
  Response.ContentType = "application/ms-excel";
  Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">");
  this.EnableViewState = false;
  System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
  HtmlTextWriter oHtmlTextWriter = new HtmlTextWriter(oStringWriter);
  GridView1.Columns[6].Visible = false;
  GridView1.Columns[7].Visible = false;
  GridView1.RenderControl(oHtmlTextWriter);
  Response.Write(oStringWriter);
  Response.End();
  public override void VerifyRenderingInServerForm(Control control)
  {
  //base.VerifyRenderingInServerForm(control);
  }
  页面上:首行加了 EnableEventValidation = "false" 出现问题了
  1.导出的文件名都是乱码,但是内容确实正确的
  2.导出的文件有分页、、、还有后面我写的【详细】等等。这个怎么弄呀?

------解决方案--------------------

C# code

// <summary>
        /// 导出Excel
        /// </summary>
        /// <param name="dtTemp"></param>
        /// <param name="ServerPath"></param>
        /// <param name="TextName"></param>
        /// <param name="fc">将number格式化成string</param>
        public static void ExportTasks(System.Data.DataSet ds, string ServerPath, string TextName, int fc, string [] sName)
        {
            // 定义要使用的Excel 组件接口
            // 定义Application 对象,此对象表示整个Excel 程序
            Application excelApp = null;
            // 定义Workbook对象,此对象代表工作薄
            Workbook workBook;
            // 定义Worksheet 对象,此对象表示Execel 中的一张工作表
            Worksheet ws = null;
            //定义Range对象,此对象代表单元格区域
            Range range;
            
            try
            {
                excelApp = new Application();
                workBook = excelApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
                for (int d = 0; d<ds.Tables.Count; d++)
                {                   
                    System.Data.DataTable dtTemp = ds.Tables[d];
                    int dcell = 1;
                    int rowindex = 0; int colindex = 0;

                    int rowcount = dtTemp.Rows.Count;
                    int colcount = dtTemp.Columns.Count;
                    //初始化 Application 对象 excelApp
                    
                    //在工作薄的第一个工作表上创建任务列表
                   
                    //workBook.Worksheets.Count
                    ws = (Worksheet)workBook.Worksheets[d+1];

                    // 命名工作表的名称为
                    if (sName.Length >= d)
                    {
                        ws.Name = !string.IsNullOrEmpty(sName[d]) ? sName[d] : "Sheet"+(d+1).ToString();
                    }
                    else
                        ws.Name = "Sheet" + (d + 1).ToString();
                    //创建缓存
                    Object[,] objdata = new object[rowcount + 1, colcount];

                    //创建标题
                    foreach (System.Data.DataColumn dc in dtTemp.Columns)
                    {
                        objdata[rowindex, colindex++] = dc.ColumnName;
                    }
                    //获取数据
                    for (int i = 0; i < rowcount; i++)
                    {
                        dcell = 0;