日期:2014-05-18 浏览次数:20450 次
public double zhk = 1; List<Product> pros = new List<Product>(); protected void Page_Load(object sender, EventArgs e) { string vastr = Request["vastr"].ToString(); List<Product> prosd = new List<Product>(); if (vastr != "") { string[] ids = vastr.Split(','); for (int i = 0; i < ids.Length; i++) { List<Product> pros = ProductService.GetAllProductById(ids[i].ToString()); if (pros.Count > 0) { Product pro = pros[0]; prosd.Add(pro); } } ExportGridToExcel(prosd, "upxia.xls"); } else { Response.Write("<script>alert('请选择您要导出的数据!');location='prozhan.aspx'</script>"); } } private void ExportGridToExcel(List<Product> pros, string filename) { // 检查是否有文件名 if (string.IsNullOrEmpty(filename)) { throw new ArgumentException("Export filename is required"); } // 检查是否是excel格式 if (!filename.EndsWith(".xls")) { filename += ".xls"; } StringWriter tw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(tw); Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312"); //Response.Clear(); //Response.ContentType = "application/vnd.ms-excel"; Response.AddHeader("content-disposition", "attachment;filename=" + filename); //Response.Charset = string.Empty; //Page.EnableViewState = false; //grid.RenderControl(hw); writeEc ec = new writeEc(); hw.Write(ec.getEXCEL(pros, zhk)); Response.Write(tw.ToString()); Response.End(); }