【问题标题】:Excel sheet is not exportingExcel 工作表未导出
【发布时间】:2017-03-22 21:30:33
【问题描述】:

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

【问题讨论】:

  • 定义not working。您是否收到错误、空白工作表、错误数据?如果没有更详细的信息,您的问题还不够清楚。

标签: javascript c# sql asp.net


【解决方案1】:

为更新面板中的按钮创建一个触发器,这可能会解决您的问题..

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

【讨论】:

  • 只是为了测试你可以尝试saprate这两个功能.....拿两个按钮,在第一个按钮单击时绑定gridview并在第二个按钮单击时将其导出到excel中......我正在使用与母版页和更新面板相同
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多