2012-04-20 106 views
0

我正在研究一個具有gridview控件的網頁。我需要將數據轉換爲.xls格式。我可以創建.xls文件並傳輸數據,但問題是我需要將網格單元格顯示在Excel表格的背景中。現在,它只顯示沒有網格單元的空白背景。否則,gridview會正常傳輸。由於此問題,打印.xls文件是一個問題。具有更多列的表格不是壓縮,而是打印2-3頁。我的代碼如下:將gridview導出爲.xls

public static void ExportToXLS(string fileName, GridView gv,string companyName,string reportTitle , string period) 
    { 
     //For writing to XLS file 
     HttpContext.Current.Response.Clear(); 
     HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName)); 
     HttpContext.Current.Response.ContentType = "application/ms-excel"; 

     using (StringWriter sw = new StringWriter()) 
     { 
      using (HtmlTextWriter htw = new HtmlTextWriter(sw)) 
      { 

       Table tableReport = new Table(); 
       tableReport.GridLines = gv.GridLines; 

       // add the header row to the table 
       if (gv.HeaderRow != null) 
       { 
        ReportList.PrepareControlForExport(gv.HeaderRow); 
        tableReport.Rows.Add(gv.HeaderRow); 
       } 

       // add each of the data rows to the table 
       foreach (GridViewRow row in gv.Rows) 
       { 
        ReportList.PrepareControlForExport(row); 
        tableReport.Rows.Add(row); 
       } 

       // add the footer row to the table 
       if (gv.FooterRow != null) 
       { 
        ReportList.PrepareControlForExport(gv.FooterRow); 
        tableReport.Rows.Add(gv.FooterRow); 
       } 

       //Takes value of company name 
       System.Web.UI.WebControls.Label labelCompany = new System.Web.UI.WebControls.Label(); 
       labelCompany.Text = companyName; 
       labelCompany.Font.Bold = true; 

       //Takes value of report title 
       System.Web.UI.WebControls.Label labelReport = new System.Web.UI.WebControls.Label(); 
       labelReport.Text = reportTitle; 
       labelReport.Font.Bold = true; 

       //Takes value of report period 
       System.Web.UI.WebControls.Label labelPeriod = new System.Web.UI.WebControls.Label(); 
       labelPeriod.Text = period; 

       // render the htmlwriter into the response 
       htw.Write("<center>"); 
       labelCompany.RenderControl(htw); 
       htw.Write("<br/>"); 
       labelReport.RenderControl(htw); 
       htw.Write("<br/>"); 
       labelPeriod.RenderControl(htw); 
       htw.Write("</center>"); 
       htw.Write("<br/>"); 
       tableReport.RenderControl(htw); 
       HttpContext.Current.Response.Write(sw.ToString()); 
       HttpContext.Current.Response.End(); 
      } 
     } 
    } 

有什麼建議嗎?

+0

你想在xls中顯示網格線? – 2012-04-20 06:18:48

+0

請謹慎接受答案..它會讓你很快得到答案。 – 2012-04-20 07:39:14

+0

hello bhavna您是否得到了這項工作,因爲我也面臨同樣的問題 – rahul 2012-09-28 07:00:25

回答

0
Response.Clear(); 
    Response.Buffer = true; 
    Response.AddHeader("content-disposition",attachment;filename=GridViewExport.xls"); 
    Response.Charset = ""; 
    Response.ContentType = "application/vnd.ms-excel"; 

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

編號:Export grid

試試這個:

protected void btnExportExcel_Click(object sender, EventArgs e) 

    {  

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

     Response.AddHeader("content-disposition", 

     "attachment;filename=GridViewExport.xls"); 

     Response.Charset = ""; 

     Response.ContentType = "application/vnd.ms-excel"; 

     StringWriter sw = new StringWriter(); 

     HtmlTextWriter hw = new HtmlTextWriter(sw); 

     grdExport.AllowPaging = false; 

     oMailing.GetData(out ODs); 

     grdExport.DataSource = ODs; 

     grdExport.DataBind(); 

     //Change the Header Row back to white color 

     grdExport.HeaderRow.Style.Add("background-color", "#FFFFFF"); 

     //Apply style to Individual Cells 

     grdExport.HeaderRow.Cells[0].Style.Add("background-color", "green"); 

     grdExport.HeaderRow.Cells[1].Style.Add("background-color", "green"); 

     grdExport.HeaderRow.Cells[2].Style.Add("background-color", "green"); 

     grdExport.HeaderRow.Cells[3].Style.Add("background-color", "green"); 

     grdExport.HeaderRow.Cells[4].Style.Add("background-color", "green"); 

     grdExport.RenderControl(hw); 

     //style to format numbers to string 

     string style = @"<style> .textmode { mso-number-format:\@; } </style>"; 

     Response.Write(style); 

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

     Response.Flush(); 

     Response.End(); 

    } 
+0

嗨..有什麼辦法可以在Excel中包裝標題文本? – kk1076 2012-08-01 05:57:24

0

所有的答案會得到你想要的東西 - 你只是缺少格式化部分。

在一天結束時,您真正創建的是一個Excel可以讀取的HTML文件(帶有HTML表格)。 @BhaskarreddyMule中的RESPONSE標題是「強制」客戶端將文件視爲「xls」文件,並且如果它已經運行並打開它(但底線是其不是真正的「本地」Excel文件

現在已經不存在了,用HTML來思考,像在HTML中一樣對列,行和文本內容進行樣式設置,這就是你如何控制格式(即老派「nowrap」來防止包裝細胞內容,字體大小等等)

我一段時間沒有這樣做(當我需要這樣做時,我已經轉移到了Excel XML和VB.Net XML文字)我不知道你對RenderControl有多少控制權......或者如果你不得不走上更遠的「老學校」並建立HTML桌面e從頭開始的字符串.....

+0

我被困在這一點。你如何使用Excel XML?我可以在asp.net框架中使用它嗎? – bhavna 2012-04-20 12:17:13

+0

'RenderControl'將是您的首選 - 數據已存在於頁面中(不需要再進行其他數據調用)。我對此的評論是一個免責聲明,讓您瞭解您可以通過「RenderControl」獲得多少距離(因爲我個人無法提供更多有用的信息)。結賬@NiranjanKala的答案也是如此。是的,這些都是.Net,C#或VB.Net。這只是在VB.net中,它的[XML Literals goodness](http://msdn.microsoft.com/zh-cn/vstudio/Video/bb927708)其天堂(視頻中的魔術大約是5:00)。如果你這樣做,注意上面的警告,你幾乎希望每個人都有Excel。 – EdSF 2012-04-20 14:52:08

0

首先請參考:How to export nested gridview to excel/word with gridlines on the child grid,希望它能幫助您解決您的問題。

編號:How to export gridview to excel in a console type application?

您可以參考以下鏈接導出到Excel的控制。這是一個自定義控件。希望它能幫助你。

http://exporttoexcel.codeplex.com/

Styling exported grid

protected void btnExportExcel_Click(object sender, EventArgs e) 
     { 
      Response.Clear(); 
      Response.Buffer = true; 

      Response.AddHeader("content-disposition", 
      "attachment;filename=GridViewExport.xls"); 

      Response.Charset = ""; 
      Response.ContentType = "application/vnd.ms-excel"; 
      StringWriter sw = new StringWriter(); 
      HtmlTextWriter hw = new HtmlTextWriter(sw); 

      grdExport.AllowPaging = false; 
      oMailing.GetData(out ODs); 
      grdExport.DataSource = ODs; 
      grdExport.DataBind(); 

      //Change the Header Row back to white color 
      grdExport.HeaderRow.Style.Add("background-color", "#FFFFFF"); 

      //Apply style to Individual Cells 
      grdExport.HeaderRow.Cells[0].Style.Add("background-color", "green"); 

      grdExport.HeaderRow.Cells[1].Style.Add("background-color", "green"); 

      grdExport.HeaderRow.Cells[2].Style.Add("background-color", "green"); 

      grdExport.HeaderRow.Cells[3].Style.Add("background-color", "green"); 

      grdExport.HeaderRow.Cells[4].Style.Add("background-color", "green"); 

      for (int i = 0; i < grdExport.Rows.Count; i++) 
      { 
       GridViewRow row = grdExport.Rows; 

       //Change Color back to white 
       row.BackColor = System.Drawing.Color.White; 

       //Apply text style to each Row 
       row.Attributes.Add("class", "textmode"); 

       //Apply style to Individual Cells of Alternating Row 
       if (i % 2 != 0) 
       { 
        row.Cells[0].Style.Add("background-color", "#C2D69B"); 
        row.Cells[1].Style.Add("background-color", "#C2D69B"); 
        row.Cells[2].Style.Add("background-color", "#C2D69B"); 
        row.Cells[3].Style.Add("background-color", "#C2D69B"); 
        row.Cells[4].Style.Add("background-color", "#C2D69B"); 
       } 

      } 
      grdExport.RenderControl(hw); 
      //style to format numbers to string 
      string style = @"<style> .textmode { mso-number-format:\@; } </style>"; 
      Response.Write(style); 
      Response.Output.Write(sw.ToString()); 
      Response.Flush(); 
      Response.End(); 
     } 
0

我與導出到Excel試了,看來你首先需要創建一個使用部分Excel = using Microsoft.Office.Interop.Excel,當u點擊按鈕,只需要創建一個Excel類有工作簿和工作表屬性,然後使用Gridview.Row[].cell[].Text.的gridview屬性,你可以動態地將每個值在gridview中存儲到工作表中,然後將其寫入到FILE ....