2012-08-02 58 views
3

我的asp.net應用程序將gridview數據導出到excel文件。但是,我還需要在顯示gridview值之前輸入Customer信息。從asp.net導出數據到excel時添加自定義文本

gvCustomerPayment.DataSource = ViewState["data"]; 
gvCustomerPayment.AllowPaging = false; 
gvCustomerPayment.DataBind(); 

gvCustomerPayment.RenderControl(hw); 
gvCustomerPayment.Rows[2].Cells[0].Text = Convert.ToString("Customer Name: " + dr["FirstName"] + dr["LastName"]); 

gridview數據顯示的非常好。我的代碼塊中的最後一行Convert.ToString(「Customer Name:」);需要位於單元格A2中(請參閱下圖中的紅色部分)。

enter image description here

我已搜查S/O &谷歌,但沒有什麼幫助。有什麼建議麼?

+0

問題......你有沒有自動生成字段名稱設置爲false ..也改變Databound項目時,你看看創建BindingList 我問的原因是因爲一旦DataBind()我非常確定你不能改變也許你可能想創建一個BindingList 並分配ViewState [「數據」]到BindingList 並使.DataSource = BindingList MethodMan 2012-08-02 21:29:50

+2

您正在更改單元格值後,您已使用HtmWriter呈現控件。如果我在RenderControl之前更改單元格值,則需要在調用RenderControl – Icarus 2012-08-02 21:33:10

+0

@Icarus之前執行此操作,它顯示爲覆蓋gridview中的值。 – DotNetRookie 2012-08-02 21:36:20

回答

0

我覺得有兩種方法。

  1. 您可以使用帶有宏代碼的Excel模板。 但這種方式很煩人。

  2. 您可以使用NPOI庫將某些值寫入指定的單元格。 NPOI非常易於使用。 這裏是鏈接:http://npoi.codeplex.com/

0

把控制在所有出口的細節一個div。將div導出爲ex​​cel。

讓aspx文件是這樣

<div id="divExport" runat="server"> 
    <div id="dvheads" runat="server"> 
     <b> 
      <label id="lblCustomerName" runat="server"></label><br /><br /> 
      <label id="lblAddress" runat="server"></label><br /><br /> 
      <label id="lblPin" runat="server"></label> 
      </b> 
    </div> 
     <br /> 

     <asp:GridView ID="GVLoans" runat="server" CellPadding="4" ForeColor="#333333" AutoGenerateColumns="False" > 
      <AlternatingRowStyle BackColor="White" /> 
      <EditRowStyle BackColor="#2461BF" /> 
      <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 
      <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 
      <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> 
      <RowStyle BackColor="#EFF3FB" HorizontalAlign="left" /> 
      <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> 
      <SortedAscendingCellStyle BackColor="#F5F7FB" /> 
      <SortedAscendingHeaderStyle BackColor="#6D95E1" /> 
      <SortedDescendingCellStyle BackColor="#E9EBEF" /> 
      <SortedDescendingHeaderStyle BackColor="#4870BE" /> 
      <Columns> 
       <asp:TemplateField> 
      <HeaderTemplate>Sl No</HeaderTemplate> 
      <ItemTemplate> 
      <asp:Label ID="lblSRNO" runat="server" 
       Text='<%#Container.DataItemIndex+1 %>'></asp:Label> 
      </ItemTemplate> 
        <ItemStyle HorizontalAlign="Center" /> 
      </asp:TemplateField> 
      </Columns> 
     </asp:GridView> 

     <br /> 
     </div> 

在.cs文件用於出口的代碼會是這樣

protected void btnSave_Click(object sender, EventArgs e) 
{ 
    Response.Clear(); 
    Response.AddHeader("content-isposition",attachment;filename=testExport.xls"); 
    Response.Charset = ""; 
    Response.ContentType = "application/vnd.xls"; 
    System.IO.StringWriter stringWrite = new System.IO.StringWriter(); 
    System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); 
    divExport.RenderControl(htmlWrite); 
    Response.Write(stringWrite.ToString()); 
    Response.End(); 
}