2013-04-05 4 views
7

私はグリッドビューのデータをExcelに転送しようとしました....しかし、出力は空白のExcelシートです。どのようにこの問題を解決するには?グリッドビューの値を転送するコードはありますか?データベースを持つシートを秀でる?グリッドビューのデータエクスポートasp.netでExcelに

protected void btnexcel_Click1(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); 

    gvdetails.AllowPaging = false; 
    gvdetails.DataBind(); 
    gvdetails.HeaderRow.Style.Add("background-color", "#FFFFFF"); 
    gvdetails.HeaderRow.Cells[0].Style.Add("background-color", "green"); 
    gvdetails.HeaderRow.Cells[1].Style.Add("background-color", "green"); 
    gvdetails.HeaderRow.Cells[2].Style.Add("background-color", "green"); 
    for (int i = 0; i < gvdetails.Rows.Count;i++) 
    { 
     GridViewRow row = gvdetails.Rows[i]; 
     row.BackColor = System.Drawing.Color.White; 
     row.Attributes.Add("class", "textmode"); 
     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"); 
     } 
    } 

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

    Response.Output.Write(sw.ToString()); 
    Response.End(); 
} 
+1

? – Ratna

+1

@Ratna:生成中のExcelシートは空白です。要件を正しく読んでください。 – pordi

+0

あなたはこれがexcel exportではないことを知っていることを願っています。あなたは、HTMLテーブルとトリックブラウザに間違ったコンテンツタイプを提供しています。あなたがこのアプローチで得ることができる多くの問題があります。 http://stackoverflow.com/a/10245406/351383 –

答えて

12

あなたのシートがnullであなたの文字列ライターので空白であるとして、あなたのコード内でDataBindを持っていますがgridview
のための任意のDataSourceがあります。ここ はここ

System.Web.UI.HtmlTextWriter htmlWrite = 
    new HtmlTextWriter(stringWrite); 

    GridView1.RenderControl(htmlWrite); 

を助けるかもしれないものである完全なコード

protected void Button1_Click(object sender, EventArgs e) 
{ 
    Response.Clear(); 

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


    Response.ContentType = "application/vnd.xls"; 

    System.IO.StringWriter stringWrite = new System.IO.StringWriter(); 

    System.Web.UI.HtmlTextWriter htmlWrite = 
    new HtmlTextWriter(stringWrite); 

    GridView1.RenderControl(htmlWrite); 

    Response.Write(stringWrite.ToString()); 

    Response.End(); 

} 
+1

ビジュアルなWebパーツで、ページに 'form'コントロールがない場合はどうなりますか? – SearchForKnowledge

-1

私はそこにない

gvdetails.DataBind(); 
+0

私はDataBindがAllowPagingのすぐ下にあると思います – pordi

+0

私は 'datasource'について話していますか? –

+0

正当なものと思われます.... – pordi

0

の代わりに...以下に示すように、あなたが単純なアプローチを使用するカントこれらすべてをやっています。

Response.ClearContent(); 
      Response.AddHeader("content-disposition", "attachment; filename=" + strFileName); 
      Response.ContentType = "application/excel"; 
      System.IO.StringWriter sw = new System.IO.StringWriter(); 
      HtmlTextWriter htw = new HtmlTextWriter(sw); 
      gv.RenderControl(htw); 
      Response.Write(sw.ToString()); 
      Response.End(); 

あなたが何か他のものをチェックするために全体のウォークスルーにhere

0

を得ることができますが、ビューステートが(私はちょうど昨日、これを解決)であることを確認しています。ビューステートをオンにしていない場合は、再度ロードするまではグリッドビューは空白になります。

3

は、エクスポートのデータバインディングに問題がある可能性があります。そのデータがグリッドビューに正しくビンされているかどうかを確認してください。

excelシートのエクスポートグリッドビューにこのコードを使用し、iTextSharp dllをプロジェクトに追加する必要があることに注意してください。

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); 
     GridView1.AllowPaging = false; 

     // Re-Bind data to GridView 

     using (CompMSEntities1 CompObj = new CompMSEntities1()) 
     { 
      Start = Convert.ToDateTime(txtStart.Text); 
      End = Convert.ToDateTime(txtEnd.Text); 

      GridViewSummaryReportCategory.DataSource = CompObj.SP_Category_Summary(Start, End); 
      SP_Category_Summary_Result obj1 = new SP_Category_Summary_Result(); 
      GridView1.DataBind(); 
      GridView1.Visible = true; 
      ExportTable.Visible = true; 
     } 

     //Change the Header Row back to white color 

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


     GridView1.Style.Add(" font-size", "10px"); 




     //Apply style to Individual Cells 

     GridView1.HeaderRow.Cells[0].Style.Add("background-color", "green"); 
     GGridView1.HeaderRow.Cells[1].Style.Add("background-color", "green"); 
     GridView1.HeaderRow.Cells[2].Style.Add("background-color", "green"); 
     GridView1.HeaderRow.Cells[3].Style.Add("background-color", "green"); 
     GridView1.HeaderRow.Cells[4].Style.Add("background-color", "green"); 

     for (int i = 1; i < GridView1.Rows.Count; i++) 
     { 
      GridViewRow row = GridView1.Rows[i]; 

      //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"); 
      } 
     } 
     GridView1.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(); 
    } 
3

私はそれが最良の方法はclosedxmlを使用することですあなた

string filename = String.Format("Results_{0}_{1}.xls", DateTime.Today.Month.ToString(), DateTime.Today.Year.ToString()); 
     if (!string.IsNullOrEmpty(GRIDVIEWNAME.Page.Title)) 
      filename = GRIDVIEWNAME.Page.Title + ".xls"; 

     HttpContext.Current.Response.Clear(); 

     HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + filename); 


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

     System.IO.StringWriter stringWriter = new System.IO.StringWriter(); 
     System.Web.UI.HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter); 



     System.Web.UI.HtmlControls.HtmlForm form = new System.Web.UI.HtmlControls.HtmlForm(); 
     GRIDVIEWNAME.Parent.Controls.Add(form); 
     form.Controls.Add(GRIDVIEWNAME); 
     form.RenderControl(htmlWriter); 

     HttpContext.Current.Response.Write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />"); 
     HttpContext.Current.Response.Write(stringWriter.ToString()); 
     HttpContext.Current.Response.End(); 
0

役立つと思います。以下は、あなたがここに取得しているエラーが何であるかを参照するためのリンク

https://closedxml.codeplex.com/wikipage?title=Adding%20DataTable%20as%20Worksheet&referringTitle=Documentation

であり、あなたが簡単な使用をすることができます

var wb = new ClosedXML.Excel.XLWorkbook(); 
DataTable dt = GeDataTable();//refer documentaion 

wb.Worksheets.Add(dt); 

Response.Clear(); 
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 
Response.AddHeader("content-disposition", "attachment;filename=\"FileName.xlsx\""); 

using (var ms = new System.IO.MemoryStream()) { 
    wb.SaveAs(ms); 
    ms.WriteTo(Response.OutputStream); 
    ms.Close(); 
} 

Response.End(); 
関連する問題