2012-08-02 3 views
3

내 asp.net 응용 프로그램은 gridview 데이터를 Excel 파일로 내 보냅니다. 그러나 gridview 값이 표시되기 전에 고객 정보를 입력해야합니다.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

질문처럼 될 것 파일처럼 영문 파일을하자 .. Databound 항목을 변경할 때 BindingList를 만들 때 보았습니다 이유는 DataBind()가 BindingList를 으로 만들고 ViewState에서 값을 할당하려고 할 때 아마도 변경할 수 없기 때문입니다. [ "data"]를 BindingList에 추가합니다. 및 .DataSource = BindingList를 만듭니다. MethodMan

+2

HtmWriter를 사용하여 컨트롤을 렌더링 한 후에 셀 값을 변경하고 있습니다. RenderControl을 호출하기 전에이 작업을 수행해야합니다. – Icarus

+0

@Icarus RenderControl 전에 셀 값을 변경하면 gridview 내의 값을 덮어 쓰는 것으로 나타납니다. – DotNetRookie

답변

0

두 가지 방법이 있다고 생각합니다.

  1. 매크로 코드와 함께 Excel 템플릿을 사용할 수 있습니다. 하지만이 방법은 성가신 일입니다.

  2. NPOI 라이브러리를 사용하여 지정된 셀에 값을 쓸 수 있습니다. NPOI는 매우 사용하기 쉽습니다. http://npoi.codeplex.com/

0

모든 세부 사항을 내보낼 사업부의 컨트롤을 넣어 : 다음은 링크입니다. div를 내보내 Excel로 내 보냅니다.

당신이 자동 필드 이름을 false로 설정 생성해야합니까 ... .cs에서이

<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> 

수출의 코드는이

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(); 
} 
관련 문제