2013-04-11 37 views
0

保存数据网格视图擅长asp点网。数据是当我在网格视图中使用的填充比全数据未在Excel工作表显示了如何通过下面的代码在网格视图使用填充导出DataGridView与填充优秀

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.RenderControl(htw); 
    Response.Write(sw.ToString()); 
    Response.End(); 
    } 
    public override void VerifyRenderingInServerForm(Control control) 
    { 
    } 
+0

使用填充或不示例代码,所有的数据必须得到保存在Excel中。你点击单元格进行验证了吗? – Ratna 2013-04-11 04:43:45

+0

而不是使用填充尝试将所有列设置为固定的宽度和相同的对齐方式(左,右)。一旦完成,他们应该全部导出到Excel对齐。 – Darshan 2013-04-11 04:43:50

回答

1

转到何时显示excel表网格视图的完整数据保存:

using System; 
using System.Data; 
using System.IO; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

namespace Whatever 
{ 
/// 

/// This class provides a method to write a dataset to the HttpResponse as 
/// an excel file. 
/// 

public class ExcelExport 
{ 
    public static void ExportDataSetToExcel(DataSet ds, string filename) 
    { 
    HttpResponse response = HttpContext.Current.Response; 

    // first let's clean up the response.object 
    response.Clear(); 
    response.Charset = ""; 

    // set the response mime type for excel 
    response.ContentType = "application/vnd.ms-excel"; 
    response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + "\""); 

    // create a string writer 
    using (StringWriter sw = new StringWriter()) 
    { 
    using (HtmlTextWriter htw = new HtmlTextWriter(sw)) 
    { 
    // instantiate a datagrid 
    DataGrid dg = new DataGrid(); 
    dg.DataSource = ds.Tables[0]; 
    dg.DataBind(); 
    dg.RenderControl(htw); 
    response.Write(sw.ToString()); 
    response.End(); 
    } 
    } 
    } 
} 
} 

NOTE:以上代码属于this的链接。

您可以关注that的相关链接,了解更多详情。

希望它有帮助。

2

您可以导出到Excel和应用样式和格式见下文

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

     // Re-Bind data to GridView 

     using (MSEntities1 CompObj = new MSEntities1()) 
     { 
      // code for bind grid view 
      GridView.DataBind(); 
     } 

     // Change the Header Row back to white color 

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

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


     //Apply style to Individual Cells 

     GridView.HeaderRow.Cells[0].Style.Add("background-color", "green"); 
     GridView.HeaderRow.Cells[1].Style.Add("background-color", "green"); 
     GridView.HeaderRow.Cells[2].Style.Add("background-color", "green"); 
     GridView.HeaderRow.Cells[3].Style.Add("background-color", "green"); 
     GridView.HeaderRow.Cells[4].Style.Add("background-color", "green"); 
     GridView.HeaderRow.Cells[5].Style.Add("background-color", "green"); 
     GridView.HeaderRow.Cells[6].Style.Add("background-color", "green"); 
     GridView.HeaderRow.Cells[7].Style.Add("background-color", "green"); 


     int k = GridView.Rows.Count; 

     for (int i = 1; i < GridView.Rows.Count; i++) 
     { 

      GridViewRow row = GridView.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"); 
       row.Cells[5].Style.Add("background-color", "#C2D69B"); 
       row.Cells[6].Style.Add("background-color", "#C2D69B"); 
       row.Cells[7].Style.Add("background-color", "#C2D69B"); 
      } 
     } 
     GridView.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(); 
    }