.net 将分页展示的GridView的全部数据 导出excel

cs代码如下:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Web;
  4 using System.Web.UI;
  5 using System.Web.UI.WebControls;
  6 using System.Data;
  7 using System.Data.OracleClient;
  8 using System.Configuration;
  9 using System.IO;
 10 using System.Text;
 11 
 12 public partial class NewFrameWorkUI_Web_UI_Bad_Event_Drug_Bad_Reaction_Bad_Reaction_List : System.Web.UI.Page
 13 {
 14     OracleConnection StrConn = new OracleConnection(ConfigurationManager.ConnectionStrings["ConnectionStringOracle10"].ConnectionString);
 15     public string sqlstr; //查询条件sql语句
 16     public string flag ;//是否分页标志  0 :分页   1:不分页
 17     protected void Page_Load(object sender, EventArgs e)
 18     {
 19         if (!Page.IsPostBack)
 20         {
 21             ViewState["sqlstr"] = "";
 22             ViewState["flag"] = "0";//初始化为分页
 23             Get_Data();
 24             Show_Report_Type();
 25             this.DropDownList1.Items.Insert(0, "--请选择--");
 26         }
 27     }
 28     public void Show_Report_Type()
 29     {
 30         string Str = " select * from drug_bad_dict where status='2' order by id   ";
 31         OracleDataAdapter da = new OracleDataAdapter(Str, StrConn);
 32         DataSet ds = new DataSet();
 33         da.Fill(ds, "Bad");
 34         this.DropDownList1.DataSource = ds.Tables["Bad"];
 35         this.DropDownList1.DataTextField = "name";
 36         this.DropDownList1.DataValueField = "id";
 37         this.DropDownList1.DataBind();
 38     }
 39 
 40     public void Get_Data()
 41     {
 42         StrConn.Open();
 43         string str = "select a.*,b.*,d.*,e.*,c.Name as report_name from DRUG_BAD_MASTER a inner join pat_master_index b on a.patient_id=b.patient_id left join DRUG_BAD_dict c on a.report_type=c.id  inner join DRUG_BAD_GUOCHENG d on a.bid = d.bid inner join DRUG_BAD_DETAIL e on a.bid=e.bid   where 1=1 " + (string )ViewState["sqlstr"] + "  order by a.REPORT_DATE desc";
 44 
 45         OracleDataAdapter da = new OracleDataAdapter(str, StrConn);
 46         DataSet ds = new DataSet();
 47         da.Fill(ds, "Bad");
 48         this.SearchGrird.DataSource = ds.Tables["Bad"];
 49         this.SearchGrird.DataBind();
 50         StrConn.Close();
 51         AspNetPager1.RecordCount = ds.Tables["Bad"].Rows.Count;
 52         PagedDataSource pds = new PagedDataSource();
 53         pds.DataSource = ds.Tables["Bad"].DefaultView;
 54         if ((string)ViewState["flag"] == "0")
 55         {
 56             pds.AllowPaging = true;
 57         }
 58         else
 59         {
 60             pds.AllowPaging = false;
 61         }
 62         pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
 63         pds.PageSize = AspNetPager1.PageSize;
 64         SearchGrird.DataSource = pds;
 65         SearchGrird.DataBind();
 66 
 67     }
 68     protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
 69     {
 70         int i;
 71         //执行循环,保证每条数据都可以更新
 72         for (i = 0; i < SearchGrird.Rows.Count; i++)
 73         {
 74             //首先判断是否是数据行
 75             if (e.Row.RowType == DataControlRowType.DataRow)
 76             {
 77                 //当鼠标停留时更改背景色
 78                 e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#EDF4FC'");
 79                 //当鼠标移开时还原背景色
 80                 e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
 81             }
 82         }
 83     }
 84     protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
 85     {
 86         this.SearchGrird.PageIndex = e.NewPageIndex;
 87         Get_Data();
 88 
 89     }
 90     protected void gv_RowCommand(object sender, GridViewCommandEventArgs e)
 91     {
 92         if (e.CommandName == "del")
 93         {
 94             string str_del1 = "delete from DRUG_BAD_Detail where bid=" + e.CommandArgument.ToString() + "";
 95 
 96             string str_del2 = "delete from DRUG_BAD_MASTER where bid=" + e.CommandArgument.ToString() + "";
 97             DbHelperOralce.ExecuteSql(str_del1);
 98             DbHelperOralce.ExecuteSql(str_del2);
 99             Jscript.Alert("删除成功!");
100         }
101         Get_Data();
102     }
103     protected void SearchGrird_RowDataBound(object sender, GridViewRowEventArgs e)
104     {
105         Sys_Commn.ShowRow(this.SearchGrird, e, "Default");//绑定行样式
106     }
107     protected void AspNetPager1_PageChanged(object sender, EventArgs e)
108     {
109         Get_Data();
110     }
111 
112 
113     protected void btn_Search_Click(object sender, EventArgs e)
114     {
115         string strsqling = "";
116         if (this.TB_Name.Text.Trim() != "")
117         {
118             strsqling += " and b.name like '%" + this.TB_Name.Text.Trim() + "%'";
119         }
120         if (this.TB_Start.Text.Trim() != "" && this.TB_End.Text.Trim() != "")
121         {
122             strsqling += " and ( a.REPORT_DATE  between to_date('" + this.TB_Start.Text.Trim() + "','yyyy-mm-dd') and to_date('" + this.TB_End.Text.Trim() + "','yyyy-mm-dd'))";
123         }
124         if (this.DropDownList1.SelectedValue != "--请选择--")
125         {
126             strsqling += " and a.REPORT_TYPE='" + this.DropDownList1.SelectedValue + "'";
127         }
128         ViewState["sqlstr"] = strsqling;
129         Get_Data();
130     }
131 
132     protected void CreateExcel(object sender, EventArgs e)
133     {
134         ViewState["flag"] = "1";//不分页
135         Get_Data();
136         Export("application/ms-excel", "不良反应报告登记表.xls");
137         ViewState["flag"] = "0";//分页
138     }
139     private void Export(string FileType, string FileName)
140     {
141         Response.Charset = "GB2312";
142         Response.ContentEncoding = System.Text.Encoding.UTF8;
143         Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
144         Response.ContentType = FileType;
145         this.EnableViewState = false;
146         StringWriter tw = new StringWriter();
147         HtmlTextWriter hw = new HtmlTextWriter(tw);
148         SearchGrird.RenderControl(hw);
149         Response.Write(tw.ToString());
150         Response.End();
151     }
152 
153     ////<summary>
154     ////这个方法必须重写,否则出错
155     ////</summary>
156     ////<param name="control"></param>
157     public override void VerifyRenderingInServerForm(Control control)
158     {
159     }
160 }

 

posted @ 2021-02-25 09:59  小小枝子  阅读(139)  评论(0编辑  收藏  举报