2012-02-22 43 views
0

我導出一個GridView用下面的代碼到Excel中:追加信息到Excel中的ASP.NET C#中的GridView導出文件

protected void export_OnClick(object sender, EventArgs e) 
{ 

    string style = @"<style> .text { mso-number-format:\@; } </style> "; 
    // Let's hide all unwanted stuffing 
    GridView1.AllowPaging = false; 
    GridView1.AllowSorting = false; 

    // Let's bind data to GridView 
    BindGrid(); 

    //Change the color back to white 
    GridView1.HeaderRow.Style.Add("background-color", "#ffffff"); 


    //Apply color to the header 
    for (int i = 0; i < GridView1.HeaderRow.Cells.Count; i++) 
    { 
     GridView1.HeaderRow.Cells[i].Style.Add("background-color", "#e0e0e0"); 
    } 

    // Let's output the Gridview 
    Response.Clear(); 
    Response.ContentType = "application/vnd.xls"; 
    Response.AddHeader("content-disposition", "attachment;filename=" + reportid + ".xls"); 

    StringWriter swriter = new StringWriter(); 
    HtmlTextWriter hwriter = new HtmlTextWriter(swriter); 

    GridView1.RenderControl(hwriter); 

    Response.Write(style); 
    Response.Write(swriter.ToString()); 
    Response.End(); 

} 

現在我需要追加的第一行上的名稱,日期&第二行是時間,第三行可能還有其他信息。我也想在實際的GridView數據和這些信息之間留下一行。這可能嗎?如果是這樣,你會如此善良,並提供一些例子,我怎麼能做到這一點。謝謝。

回答

0

只要繼續並添加它即可。在每個後面添加回車,以使Excel前進到下一行(使用製表符前進到下一個單元格)。

Response.Write(style); 
Response.Write("Pretty Excel Export File\n"); 
Response.Write("Generated on " + DateTime.Now.ToShortDateString() + "\n\n"); 
Response.Write(swriter.ToString()); 
Response.End(); 
+0

對不起,我應該用什麼標籤? – jorame 2012-02-22 01:31:21

+0

「\ t」會給你一個標籤。 – Malk 2012-02-22 01:32:53

+0

\ r或\ t都不起作用。當我使用\ r時,它並沒有更多的到下一行,當我使用\ t它不會移動到下一個單元格 – jorame 2012-02-22 01:35:52