2016-11-09 189 views
0

我需要根據所選日期生成excel報表。每件事情都可以正常工作,但是我的Excel表格不會生成,但是當我嘗試導出網頁表單時,它的工作正常。但是,如果使用母版頁和它不工作的內容頁..Excel工作表不導出

這裏是我的代碼

ASPX

<center> 
<table> 
    <tr> 
     <td> 
      <asp:Label ID="Label1" runat="server" Text="Start Date:" style="color:white"></asp:Label> 
     </td> 
     <td> 
      <asp:TextBox ID="datepicker" runat="server"></asp:TextBox> 
     </td> 
     <td> 
      <img src="../Images/calendar.gif" id ="datepic"/> 
      <cc1:calendarextender ID="ceFromDate" runat="server" TargetControlID="datepicker" 
                 Format="dd-MMM-yyyy" Enabled="True" PopupButtonID="datepic" CssClass="black"> 
                </cc1:calendarextender> 
     </td> 
    </tr> 
    <br /> 
    <br /> 
    <tr> 
     <td> 
      <asp:Label ID="Label2" runat="server" Text="End Date:" style="color:white"></asp:Label> 
     </td> 
     <td> 
      <asp:TextBox ID="datepicker1" runat="server"></asp:TextBox> 
     </td> 
     <td> 
      <img src="../Images/calendar.gif" id="datepic1" /> 
      <cc1:calendarextender ID="Calendarextender1" runat="server" TargetControlID="datepicker1" 
                 Format="dd-MMM-yyyy" Enabled="True" PopupButtonID="datepic1" CssClass="black"> 
                </cc1:calendarextender> 
     </td> 
    </tr> 
    <tr> 
     <td> 
      <asp:Button ID="Button1" runat="server" Text="Get Report" OnClick="Button1_Click" /> 
     </td> 
    </tr> 
</table> 
    <asp:GridView ID="GridView1" runat="server" ></asp:GridView> 
    </center> 

CS代碼:

protected void Button1_Click(object sender, EventArgs e) 
    { 
    using (OracleConnection con = new OracleConnection(constr)) 
    { 
     //for getting id 
     con.Open(); 


     OracleCommand cmd = new OracleCommand("select adcomplain_no as \"AD COMPLAIN NO\",to_char(COMPLAINT_DATE,'DD/MM/YY') as \"COMPLAINT DATE\",nature_of_complaint as \"NATURE OF COMPLAINT\",(select categoryname from crs_categorynew where crsid=complaint_categoryID)as \"CATEGORY NAME\",(SELECT subcategoryname FROM CRS_SUDCATEGORYNEW WHERE CRSSUBID=complaint_subcategory) as \"SUB CATEGORY NAME\",seat_location AS \"SEAT LOCATION\",seatno AS \"SEAT NO\",extensionno AS \"EXTENSION NO\",to_char(entry_date,'DD/MM/YY')as \"ENTRY DATE\" ,(select staffno||'-'||NAME as name from employee where staffno=entry_by) as \"ENTRY BY\" ,(CASE STATUSFLAG WHEN 'U' THEN 'Un Allotted' WHEN 'AN' THEN 'Allotted' WHEN 'B' THEN 'Being Completed' WHEN 'C' THEN 'Completed' WHEN 'UA' THEN 'Un Attented' ELSE '' END)as \"STATUS\",(select staffno||'-'||NAME as name from employee where staffno=alloted_person) as \"ALLOTED PERSON\",to_char(target_date,'DD/MM/YY') as \"TARGET DATE\",(select staffno||'-'||NAME as name from employee where staffno=alloted_by) as \"ALLOTED BY\",to_char(alloted_date,'DD/MM/YY') as \" ALLOTED DATE\",complain_reason as SOLUTION,to_char(complain_statusdate, 'DD/MM/YY')as \"COMPLAIN STATUS DATE\",(select staffno||'-'||NAME as name from employee where staffno=complain_statusby)AS \"COMPLAIN STATUS BY\" from crs_complaint where complaint_date between (TO_DATE('" + datepicker.Text + "', 'dd/mm/yyyy')) and (TO_DATE('" + datepicker1.Text + "', 'dd/mm/yyyy'))", con); 
     OracleDataAdapter oda = new OracleDataAdapter(cmd); 
     DataTable dt = new DataTable(); 
     oda.Fill(dt); 
     if (dt.Rows.Count > 0) 
     { 
      GridView1.DataSource = dt; 
      GridView1.DataBind(); 
     } 
     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); 

     for (int i = 0; i < GridView1.Rows.Count; i++) 
     { 
      //Apply text style to each Row 
      GridView1.Rows[i].Attributes.Add("class", "textmode"); 
     } 
     GridView1.RenderControl(hw); 


     string headerTable = @"<h1>Service Request List from '" + datepicker.Text + "' to '" + datepicker1.Text + "'</h1>"; 
     //style to format numbers to string 
     string style = @"<style> .textmode { mso-number-format:\@; } </style>"; 
     //Range rg = (Excel.Range)worksheetobject.Cells[1, 1]; 
     //rg.EntireColumn.NumberFormat = "MM/DD/YYYY"; 
     Response.Write(headerTable); 
     Response.Output.Write(sw.ToString()); 
     Response.Flush(); 
     Response.End(); 

     con.Close(); 




    } 
} 
public override void VerifyRenderingInServerForm(Control control) 
{ 
    /* Confirms that an HtmlForm control is rendered for the specified ASP.NET 
     server control at run time. */ 
} 
+0

定義'不working'。你會得到一個錯誤,一張空白表,錯誤的數據?沒有更詳細的信息,你的問題還不夠清楚。 – VDWWD

回答

0

cretae在UpdatePanel的按鈕觸發,這可能會解決你的問題..

</ContentTemplate> 
     <Triggers> 
      <asp:PostBackTrigger ControlID="Button1" /> 
     </Triggers> 
    </asp:UpdatePanel> 
+0

我試過,儘管不工作.... – hyugaazia

+0

只是爲了測試,你可以嘗試saprate這兩個功能.....採取兩個按鈕,第一個按鈕點擊綁定gridview並將其導出到excel第二個按鈕點擊.. 。我使用相同的主頁和更新面板 –

+0

感謝他的工作.. – hyugaazia

相關問題