2013-04-12 3 views
0

데이터 표보기를 엑셀 시트로 내보내고 작동합니다.데이터 내보내기 표보기가 우수합니다.

격자보기에는 10 개의 열이 있지만 Excel 시트에는 5 개의 열만 표시하려고합니다.

어떻게 해결할 수 있습니까?

protected void btnexcel_Click1(object sender, EventArgs e) 
    { 

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

    Response.AddHeader("content-disposition", 
    "attachment;filename=ActualsAndBudgets.xls"); 
    Response.Charset = ""; 
    Response.ContentType = "application/ms-excel"; 
    Response.Cache.SetCacheability(HttpCacheability.NoCache); 
    StringWriter sw = new StringWriter(); 
    HtmlTextWriter htw = new HtmlTextWriter(sw); 
    gvdetails.AllowPaging = false; 
    fillgrid(); 
    gvdetails.RenderControl(htw); 
    Response.Write(sw.ToString()); 
    Response.End(); 
    gvdetails.AllowPaging = true; 
    fillgrid(); 

    } 
    public override void VerifyRenderingInServerForm(Control control) 
    { 

    } 

답변

0

당신은 아래의 코드를 시도 할 수 있습니다 :

protected void ConvertToExcel_Click(object sender, System.EventArgs e) 
{ 
Response.ContentType = "application/vnd.ms-excel"; 
Response.AddHeader("content-disposition",  "attachment;filename=ContactMailingAddress.xls"); 
Response.Charset = ""; 
StringWriter sw = new StringWriter(); 
HtmlTextWriter hw = new HtmlTextWriter(sw); 
ContactMailingView.AllowPaging = false; 
ContactMailingView.DataBind(); 


    //Apply style to Individual Cells 

    ContactMailingView.HeaderRow.Cells(0).Style.Add("background-color", "yellow"); 
ContactMailingView.HeaderRow.Cells(1).Style.Add("background-color", "yellow"); 
ContactMailingView.HeaderRow.Cells(2).Style.Add("background-color", "yellow"); 
ContactMailingView.HeaderRow.Cells(3).Style.Add("background-color", "yellow"); 
ContactMailingView.HeaderRow.Cells(4).Style.Add("background-color", "yellow"); 


for (int i = 0; i <= ContactMailingView.Rows.Count - 1; i++) { 
    GridViewRow row = ContactMailingView.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"); 

    } 
} 

    ContactMailingView.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(); 

가}

내가

dotNetFromManila blog에 내 게시물에서했다은 당신을 도움이되기를 바랍니다.

0

이 코드를 사용하면 도움이됩니다. 프로젝트에 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(); 
    } 
+0

및 귀하의 데이터를 제대로 Excel에서 내보내기 기능에 gridview와 바인딩 할 수 없습니다, 당신은 제대로 gridview.at 바인딩의 시간에 데이터를 얻을 수 있습니다 –

관련 문제