2015-05-20 138 views
1

我在我的網站中有6個gridviews,我需要導出到excel,但每個都在一張不同的工作表中。將多個gridviews導出到多個excel選項卡(工作表)

此鏈接Export GridView to multiple Excel sheet使用的東西非常相似,但他使用的是數據集,而我不是。我是C#的新手,所以我無法改變它以適應我的解決方案。

我的代碼將所有的gridviews導出到同一張表。我創建了一個循環來運行我的GridView,現在我需要將代碼生成到每個Excel表單中。

protected void btExcel_Click(object sender, EventArgs e) 
{ 
    Response.Clear(); 
    Response.Buffer = true; 
    Response.AddHeader("content-disposition", "attachment;filename=FARPOP_Mensal_" + txDt.Text + ".xls"); 
    Response.ContentEncoding = System.Text.Encoding.GetEncoding("Windows-1252"); 
    Response.Charset = "ISO-8859-1"; 
    Response.ContentType = "application/vnd.ms-excel"; 

    using (StringWriter sw = new StringWriter()) 
    { 
     HtmlTextWriter hw = new HtmlTextWriter(sw); 
     GridView[] gvExcel = new GridView[] { gvAnual, gvPorUF, gvPorFarmacia, gvPorMunicipio, gvPorPV, gvPorCNPJ }; 
     for (int i = 0; i < gvExcel.Length; i++) 
     { 
      GridView gv = gvExcel[i]; 

      // 
      // Need to redirect to each sheet here? 
      // 

      gvExcel[i].HeaderRow.BackColor = Color.White; 
      gvExcel[i].UseAccessibleHeader = false; 
      gvExcel[i].AllowPaging = false; 
      for (int x = 0; x < gvExcel[i].Columns.Count; x++) 
      { 
       gvExcel[i].Columns[x].Visible = true; 
      } 

      gvExcel[i].DataBind(); 
      foreach (TableCell cell in gvExcel[i].HeaderRow.Cells) 
      { 
       cell.BackColor = Color.CadetBlue; 
       cell.Enabled = false; 
      } 

      foreach (GridViewRow row in gvExcel[i].Rows) 
      { 
       row.BackColor = Color.White; 
       foreach (TableCell cell in row.Cells) 
       { 

        cell.Controls.Clear(); 
        if (row.RowIndex % 2 == 0) 
        { 
         cell.BackColor = Color.White; 
        } 
        else 
        { 
         cell.BackColor = Color.LightBlue; 
        } 
        cell.CssClass = "textmode"; 
       } 
      } 

      gvExcel[i].RenderControl(hw); 
     } 

     //style to format numbers to string 
     string style = @"<style> .textmode { } </style>"; 
     Response.Write(style); 
     Response.Output.Write(sw.ToString()); 
     Response.Flush(); 
     Response.End(); 
    } 
} 
+1

在你進一步瞭解這個兔子洞之前,我想告訴你微軟建議不要在服務器上使用Office Automation:https://support.microsoft.com/en-us/kb/257757? wa = wsignin1.0 您最好在尋找第三方工具來執行Excel導出,或者您可以將Excel導出作爲計劃作業運行,以便稍後(根據您的需要)發送給用戶。 – maniak1982

+0

@ maniak1982:發佈的代碼不使用Interop。 –

+0

我編輯了你的標題。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 –

回答

5

我跟着maniak1982的意見,找到了很好的解決辦法。因此,如果你需要多個gridviews來多工作表,使用.NET,你將需要下載ClosedXMLDocumentFormat.OpenXml.dll

引用的DLL後,使用下面的代碼:

protected void btExcel_Click(object sender, EventArgs e) 
{ 
    XLWorkbook wb = new XLWorkbook(); 
    GridView[] gvExcel = new GridView[] { GridView1, GridView2, GridView3, GridView4, GridView5, GridView6}; 
    string[] name = new string[] { "Name1", "Name2", "Name3", "Name4", "Name5", "Name6" }; 
    for (int i = 0; i < gvExcel.Length; i++) 
    { 
     if (gvExcel[i].Visible) 
     { 
      gvExcel[i].AllowPaging = false; 
      gvExcel[i].DataBind(); 
      DataTable dt = new DataTable(name[i].ToString()); 
      for (int z = 0; z < gvExcel[i].Columns.Count; z++) 
      { 
       dt.Columns.Add(gvExcel[i].Columns[z].HeaderText); 
      } 

      foreach (GridViewRow row in gvExcel[i].Rows) 
      { 
       dt.Rows.Add(); 
       for (int c = 0; c < row.Cells.Count; c++) 
       { 
        dt.Rows[dt.Rows.Count - 1][c] = row.Cells[c].Text; 
       } 
      } 

      wb.Worksheets.Add(dt); 
      gvExcel[i].AllowPaging = true; 

     } 

    } 
    Response.Clear(); 
    Response.Buffer = true; 
    Response.Charset = ""; 
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 
    Response.AddHeader("content-disposition", "attachment;filename=Workbook_Name.xlsx"); 
    using (MemoryStream MyMemoryStream = new MemoryStream()) 
    { 
     wb.SaveAs(MyMemoryStream); 
     MyMemoryStream.WriteTo(Response.OutputStream); 
     Response.Flush(); 
     Response.End(); 
    } 
} 

我用了幾天的搜索...但它可以正常使用。 很開心

+0

ClosedXML是一個DLL? – Si8

相關問題