将GridView数据导出到Excel时出现问题

时间:2019-11-06 10:28:26

标签: gridview export-to-excel

我正在尝试导出具有两个复选框列的Gridview,以使其表现出色。在Response.End()上获取“线程被中止”异常。评论Response.End()时,出现类似“发送HTTP标头后服务器无法设置内容类型”的问题。

删除了Responce.Flush()并尝试,没有错误,但无法导出。请帮忙

这是我的代码:

 DataSet ds = ProductMaster_ExportToExcel();
                        if (ds.Tables[0] != null && ds.Tables[0].Rows.Count != 0)
                        {
                            using (XLWorkbook wb = new XLWorkbook())
                            {
                                wb.Worksheets.Add(ds.Tables[0], "Sheet1");
                                Response.ClearHeaders();
                                Response.Buffer = true;
                                Response.Charset = "";
                                Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                                Response.AddHeader("content-disposition", @"attachment;filename=ProductMaster.xlsx");
                                using (MemoryStream MyMemoryStream = new MemoryStream())
                                {
                                    wb.SaveAs(MyMemoryStream);
                                    MyMemoryStream.WriteTo(Response.OutputStream);
                                    Response.Flush();
                                    Response.SuppressContent = true;
                                    HttpContext.Current.Response.SuppressContent = true;
                                    HttpContext.Current.ApplicationInstance.CompleteRequest();
                                    //Response.End();
                                }
                            }
                        }

0 个答案:

没有答案