2013-07-12 45 views
0

我有一個問題,將我所有的數據導出到excel文件。我只設法從gridview的第一頁導出數據。我可否知道導出數據的正確方法,包括其他頁面中的數據?謝謝一堆。無法將多頁gridview數據導出爲ex​​cel

protected void bn_export_Click(object sender, EventArgs e) 
{ 

    Response.Clear(); 
    Response.Buffer = true; 

    Response.AddHeader("content-disposition","attachment;filename=GridViewExport.xls"); 
    Response.Charset = ""; 
    Response.Cache.SetCacheability(HttpCacheability.NoCache); 
    Response.ContentType = "application/vnd.ms-excel"; 

    StringWriter sw = new StringWriter(); 
    HtmlTextWriter hw = new HtmlTextWriter(sw); 

    gv_getGameDetails.AllowPaging = false; 


    //Change the Header Row back to white color 
    gv_getGameDetails.HeaderRow.Style.Add("background-color", "#FFFFFF"); 
    gv_getGameDetails.HeaderRow.Cells[0].Style.Add("background-color", " #262626"); 
    gv_getGameDetails.HeaderRow.Cells[1].Style.Add("background-color", " #262626"); 
    gv_getGameDetails.HeaderRow.Cells[2].Style.Add("background-color", " #262626"); 
    gv_getGameDetails.HeaderRow.Cells[3].Style.Add("background-color", " #262626"); 



    this.RemoveControls(gv_getGameDetails); 
    gv_getGameDetails.RenderControl(hw); 


    Response.Output.Write(sw.ToString()); 
    Response.Flush(); 
    Response.End(); 

回答

0

這可能有所幫助。它重申所有列和行並導出到Excel工作表。您需要添加Microsoft.Office.Interop.Excel

private void button2_Click(object sender, EventArgs e) 
    { 
     // creating Excel Application 
     Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application(); 
     // creating new WorkBook within Excel application 
     Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing); 
     // creating new Excelsheet in workbook 
     Microsoft.Office.Interop.Excel._Worksheet worksheet = null; 
     // see the excel sheet behind the program 
     app.Visible = true; 
     // get the reference of first sheet. By default its name is Sheet1. 
     // store its reference to worksheet 
     try 
     { 
      //Fixed:(Microsoft.Office.Interop.Excel.Worksheet) 
      worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets["Sheet1"]; 
      worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.ActiveSheet; 
      // changing the name of active sheet 
      worksheet.Name = "YourChosenSheetName"; 
      // storing header part in Excel 
      for (int i = 1; i < dataGridView1.Columns.Count + 1; i++) 
      { 
       worksheet.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText; 
      } 
      // storing Each row and column value to excel sheet 
       for (int i = 0; i < dataGridView1.Rows.Count - 1; i++) 
      { 
       for (int j = 0; j < dataGridView1.Columns.Count; j++) 
       { 
        worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString(); 
       } 
      } 

      // save the application 
      string fileName = String.Empty; 
      SaveFileDialog saveFileDialog1 = new SaveFileDialog(); 

      saveFileDialog1.Filter = "Excel files |*.xls|All files (*.*)|*.*"; 
      saveFileDialog1.FilterIndex = 2; 
      saveFileDialog1.RestoreDirectory = true; 

      if (saveFileDialog1.ShowDialog() == DialogResult.OK) 
      { 
       fileName = saveFileDialog1.FileName; 
       //Fixed-old code :11 para->add 1:Type.Missing 
       workbook.SaveAs(fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); 

      } 
      else 
       return;    

      // Exit from the application 
      //app.Quit(); 
     } 
     catch (System.Exception ex) 
     { 

     } 
     finally 
     { 
      app.Quit(); 
      workbook = null; 
      app = null; 
     } 
    }