protected void btn_export_Click(object sender, EventArgs e)
{
int i_whlist = chk_whlist.SelectedIndex;
string s_whlist = "";
string _ws = "";
if (i_whlist >= 0)
{
foreach (ListItem li in chk_whlist.Items)
{
s_whlist += li.Selected ? li.Value + "," : "";
}
_ws = s_whlist.Substring(0, s_whlist.Length - 1);
}
string _txtkey = txt_key.Text.ToString() == "" ? "" : txt_key.Text.ToString();
string str_sql = "P_GET_IMSTOCKICInventory " + "'" + _ws + "'" + "," + "'" + _txtkey + "'";
string _wh = chk_whlist.SelectedValue;
string _dt=_wh+"-"+DateTime.Now.ToOADate().ToString();
MydB rb = new MydB();
DataTable dt = new DataTable();
dt = rb.ExecuteQueryERP(str_sql);
this.gv_export.DataSourceID = null;
this.gv_export.DataSource = dt;
this.gv_export.DataBind();
mysubmenu.DGToExcel(this.gv_export, _dt);
this.gv_export.Visible = false;
}
public override void VerifyRenderingInServerForm(Control control)
{
}//以上程序段必须添加否则会报错!
//以下是表格下载类
//excel表格下载
public void DGToExcel(System.Web.UI.Control ctl,string filename)
{
string _filename = filename.ToString();
string ny = DateTime.Now.Date.ToString("yyyyMMdd");
HttpContext.Current.Response.Clear();//清除缓冲区流中的所有的内容输出
HttpContext.Current.Response.Buffer = true;//获取一个值,该值指示是否缓冲输出,并在完成处理整个响应之后将其发送
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + _filename + ny + ".xls");
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
HttpContext.Current.Response.ContentType = "application/ms-excel";
ctl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}