我看了你網站上的哪個 datagrid導出excle的例子。
可是當我的datagrid中有一個按鈕列的時候導出就會出錯。請問怎麼解決啊?Response.Clear(); 
Response.Buffer= true; 
Response.Charset="GB2312";    
Response.AppendHeader("Content-Disposition","attachment;filename=FileName.xls"); 
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
Response.ContentType = "application/ms-excel";
this.EnableViewState = false;    
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN",true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad); 
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.DataGrid1.RenderControl(oHtmlTextWriter); 
Response.Write(oStringWriter.ToString());
Response.End();

解决方案 »

  1.   

    按钮连接到另外一个页面重新绑同样一个没有按钮的DataGrid在PageLoad内导出//年报
    if((null == Request.QueryString["year"] ||Request.QueryString["year"].ToString().Trim().Length <=0) )
    {
    this.AlertAndCloseWin("非法调用!");
    }
    int year = int.Parse(Request.QueryString["year"].Trim());
    oc = BizObjects.Operation.ReportByYear(year);
    //绑定DataList
    this.dl_operation_year.DataSource = oc;
    this.dl_operation_year.DataBind(); //导出DataList到Excel
    Response.Clear();
    Response.Buffer=true;
    Response.ContentType="application/vnd.ms-excel";
    Response.Charset="";
    this.EnableViewState=false; StringWriter sw=new StringWriter();
    HtmlTextWriter htw=new HtmlTextWriter(sw);
    this.dl_operation_year.RenderControl(htw);
    Response.Write(sw.ToString());
    Response.End();
      

  2.   

    xiahouwen(活靶子.NET) 那請問怎麼樣將一個DataGrid的內容傳到下一個頁面呢?如果重新查詢數據生成DataGrid的話太慢了啊
      

  3.   

    你也可以直接从datatable或是datareader导出,不过导出的时候需要同时导出一些html无素,像<table>
    <tr><td>这里是要出的一个数据对应一个单元格</td><td>另一个数据</td>
    </tr>
    </table>你可以在<td>里加样式,设置颜色,高度,宽度等。就像编辑html表格一样。
    这样导出到Excel里,与孟子那个将DataGrid里的数据导出原理是一样的。可以叁考
    http://aspalliance.com/articleViewer.aspx?aId=1&pId=如果非要直接从DataGrid导出,也可以把<td></td>之间的内容换成对应的DataGrid格的内容。例:把一个DataReader(reader)里的数据导出到ExcelResponse.Clear()
            Response.Buffer = True
            Response.Charset = "GB2312"
            Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls")
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312")
            Response.ContentType = "application/ms-excel"
            Dim numbercols As Integer = reader.FieldCount
            Dim row As Integer = 2
            Dim i As Integer = 0
            ' 输出标题        Response.Write("<TABLE BORDER=1>")
            Response.Write("<tr>")
            For i = 0 To numbercols - 1
                Response.Write("<td>")
                Response.Write(reader.GetName(i).ToString())
                Response.Write("</td>")
            Next
            Response.Write("</tr>")
            ' 输出字段内容
            While (reader.Read())
                Response.Write("<tr>")
                For i = 0 To numbercols - 1
                    Response.Write("<td style='vnd.ms-excel.numberformat:@'>")
                    Response.Write(reader.GetValue(i).ToString() & vbTab)
                    Response.Write("</td>")
                Next
                Response.Write("</tr>")
            End While
            Response.End()
            reader.Close()
      

  4.   

    public string Export(System.Data.DataTable  dt)
    {
    Excel.Application excel=new Excel.Application();
    Excel.Workbooks workbooks = excel.Workbooks; 
    Excel._Workbook workbook1=excel.Workbooks.Add(XlWBATemplate.xlWBATWorksheet); 
    Sheets sheets = workbook1.Worksheets; 
    _Worksheet worksheet = (_Worksheet) sheets.get_Item(1);   int rowIndex=1; 
    int colIndex=0;  //循环读取生成新表格 
    excel.Application.Workbooks.Add(true);              //生成EXCEL的格式设定 
    excel.Cells.Font.Bold = false; 
    excel.Cells.FillLeft(); 
    excel.Cells.Font.Size = "9"; 

    //引用函数GetData中所取出的数据,得到数据源 
    System.Data.DataTable  table =dt; //将所得到的表的列名,赋值给单元格 
    foreach(DataColumn col in table.Columns) 

    colIndex++;  
    excel.Cells[1,colIndex]=col.ColumnName; 
    }
    //同样方法处理数据  foreach(DataRow row in table.Rows) 

    rowIndex++; 
    colIndex=0; 
    //处理各列的数据 
    foreach(DataColumn col in table.Columns) 

    colIndex++; 
    excel.Cells[rowIndex,colIndex]=row[col.ColumnName].ToString(); 
    }

    //不可见,即后台处理 
    excel.Visible=true;
    //友好界面提示  Excel.XlSaveAsAccessMode lhx=Excel.XlSaveAsAccessMode.xlShared;  
    object Nothing=System.Reflection.Missing.Value; 
    string FileNameString =System.DateTime.Now.ToString("yyyyMMddhhmmss");
    string url=Server.MapPath("..")+"\\ZLJDZFile" + "\\"+FileNameString+".xls";
    excel.ActiveWorkbook.SaveAs( url,Nothing,Nothing,Nothing,Nothing,Nothing,lhx,Nothing,Nothing,Nothing,Nothing,Nothing);      
    excel.Workbooks.Close();
    excel.Quit(); 
    excel=null;  Response.Buffer= true;
    Response.ContentType = "application/ms-excel";
    Response.Redirect("../ZLJDZFile/"+FileNameString+".xls"); return FileNameString;
    }
      

  5.   

    那請問怎麼樣將一個DataGrid的內容傳到下一個頁面呢?如果重新查詢數據生成DataGrid的話太慢了啊Session["dg"]=datagrid1;新页面DataGrid dg;
    dg=(DataGrid)Session["dg"];