Excel工作表未导出

时间:2016-11-09 05:49:04

标签: javascript c# sql asp.net

我需要根据所选日期生成Excel报告。每件事都运行正常但我的excel表没有生成,但当我尝试导出Web表单时它的工作正常。但是如果我使用母版页和内容页面它不起作用..

这是我的代码

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. */
}

1 个答案:

答案 0 :(得分:0)

在updatepanel中创建按钮的触发器,这可以解决您的问题..

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