Excel工作表不导出

问题描述:

我需要根据所选日期生成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

cretae在UpdatePanel的按钮触发,这可能会解决你的问题..

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

我试过,尽管不工作.... – hyugaazia

+0

只是为了测试,你可以尝试saprate这两个功能.....采取两个按钮,第一个按钮点击绑定gridview并将其导出到excel第二个按钮点击.. 。我使用相同的主页和更新面板 –

+0

感谢他的工作.. – hyugaazia