使用ClosedXML C#DataTable到Excel

时间:2017-05-15 18:50:00

标签: c# excel closedxml

我在帖子后查看了帖子,无法想办法让这个工作。

我有一个c#控制器,我想从数据库返回数据查询的excel文件。我从firebug捕获的回复似乎是二进制的(见图片),但是如何让弹出窗口让我保存或打开?

感谢enter image description here

public void getData()
        {
            var sql = "select top 10 * from z_main";

            System.Data.DataTable dt = runSQLParamsReturnDT(sql, null, db);
            dt.TableName = "Detail Data";
            using (XLWorkbook wb = new XLWorkbook())
            {
                wb.Worksheets.Add(dt);
                wb.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                wb.Style.Font.Bold = true;

                Response.Clear();
                Response.Buffer = true;
                Response.Charset = "";
                Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                Response.AddHeader("content-disposition", "attachment;filename= EmployeeReport.xlsx");

                using (MemoryStream MyMemoryStream = new MemoryStream())
                {
                    wb.SaveAs(MyMemoryStream);
                    MyMemoryStream.WriteTo(Response.OutputStream);
                    Response.Flush();
                    Response.End();
                }
            }
        }

0 个答案:

没有答案