思路:实际上是读取页面上某个控件下的内容再导出
protected void btnExcel_Click(object sender, EventArgs e) { string bgType = ""; string fileName = "预算汇总申请查询"; ContTaskList1.Visible = false; ContApproveOpinion1.Visible = false; trCondition.Visible = false; btnQuery.Visible = false; btnExcel.Visible = false; UcAssetBgSum1.isSHOW = false; trE.Visible = true; switch (dpQuarter.SelectedValue.Trim()) { case "0": bgType = "年度预算"; break; case "1": bgType = "一季度预算"; break; case "2": bgType = "二季度预算"; break; case "3": bgType = "三季度预算"; break; case "4": bgType = "四季度预算"; break; } this.lblYear.Text = dpYear.SelectedValue.Trim() + "年"; this.lblQuarter.Text = bgType; using (DataReimbursementDataContext db = new DataReimbursementDataContext()) { int intYear = int.Parse(this.dpYear.SelectedValue.Trim()); int intQuarter = int.Parse(this.dpQuarter.SelectedValue.Trim()); OA_TAB_FA_DOCSUM doc = db.OA_TAB_FA_DOCSUM.SingleOrDefault(p => p.DOC_YEAR == intYear && p.DOC_QUARTER == intQuarter); if (doc == null) { TextHelper.ShowMessage(tdMsg, "没有记录!", false); } else { TextHelper.ShowMessage(tdMsg, "", false); this.UcAssetBgSum1.Visible = true; this.UcAssetBgSum1.DocNo = doc.DOC_ID.Trim(); this.UcAssetBgSum1.BindData(); } } StringBuilder sb = new StringBuilder(); StringWriter sw = new StringWriter(sb); HtmlTextWriter htw = new HtmlTextWriter(sw); Page page = new Page(); HtmlForm form = new HtmlForm(); TABLE2.EnableViewState = false; page.EnableEventValidation = false; page.DesignerInitialize(); page.Controls.Add(form); form.Controls.Add(TABLE2); page.RenderControl(htw); Response.Clear(); Response.Buffer = true; Response.ContentType = "application/vnd.ms-excel"; Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpContext.Current.Server.UrlEncode(fileName) + ".xls"); Response.Charset = "UTF-8"; Response.ContentEncoding = Encoding.Default; Response.Write(sb.ToString()); Response.End(); }
<table id="TABLE3" width="100%" cellspacing="0" border="0" cellpadding="0" runat="server"> <tr> <td colspan="2" valign="top"> <table width="100%" cellspacing="0" cellpadding="0" runat="server"> <tr> <td height="20" > <table width="100%" cellspacing="0" cellpadding="0" runat="server"> <tr> <td width="30" bgcolor="#F2F9FD" class="p9"> <div align="center"> <img src="../images/bon_8.png" width="18" height="18"></div> </td> <td height="30" bgcolor="#F2F9FD" class="p9"> 您当前的位置:固定资产 > 预算汇总申请查询</td> <td class="p9" bgcolor="#F2F9FD" align="right" width="200" >登录用户: <uc1:UcUserInfo ID="UcUserInfo1" runat="server" /> </td> </tr> </table> </td> </tr> <tr> <td height="14" background="../images/right_jian.jpg"> </td> </tr> </table> </td> </tr> <tr> <td width="3%"> </td> <td> <table border="0" cellpadding="1" cellspacing="1" class="Table1" id="TABLE2" width="100%" runat="server"> <tr id="trCondition" runat="server"> <td class="tdleft">年度:</td> <td> <asp:DropDownList ID="dpYear" runat="server"> </asp:DropDownList> </td> <td class="tdleft">预算类型:</td> <td> <asp:DropDownList ID="dpQuarter" runat="server"> <asp:ListItem Value="0">年度预算</asp:ListItem> <asp:ListItem Value="1">一季度预算</asp:ListItem> <asp:ListItem Value="2">二季度预算</asp:ListItem> <asp:ListItem Value="3">三季度预算</asp:ListItem> <asp:ListItem Value="4">四季度预算</asp:ListItem> </asp:DropDownList> </td> <td> <asp:Button ID="btnQuery" runat="server" Text="查 询" onclick="btnQuery_Click" /> <asp:Button ID="btnExcel" runat="server" Text="导出excel" onclick="btnExcel_Click" /> </td> </tr> <tr> <td id="tdMsg" runat="server" colspan=5> </td> </tr> <tr id="trE" runat="server" visible="false"> <td colspan="5"> 年度:<asp:Label ID="lblYear" runat="server" Text=""></asp:Label> 预算类型:<asp:Label ID="lblQuarter" runat="server" Text=""></asp:Label> </td> </tr> <tr> <td colspan=5> <uc2:UcAssetBgSum ID="UcAssetBgSum1" runat="server" /> <uc3:ContTaskList ID="ContTaskList1" Visible=false runat="server" /> <uc4:ContApproveOpinion ID="ContApproveOpinion1" Visible=false runat="server" /> </td> </tr> </table> </td> </tr> </table>
先存个档吧,省的以后还要导出找。