c# - 使用 EPPlus,我正在尝试将 gridview 导出到 Excel 工作表

标签 c# asp.net gridview epplus

Excel 告诉我有不可读的数据,所以在我说尝试恢复信息后,它显示了正确的数据。但是,当我打开 xlsx 的文本文件时,我得到了整个页面的所有 html,而不仅仅是 gridview(这可能是 Excel 正在谈论的不可读内容)。

这是我的代码:

public void ExcelDownload(object sender, EventArgs e)
    {
        DataSet _MailingListUsers = db.GetMailingList();
        DataTable mailTable = _MailingListUsers.Tables[0];

        DumpExcel(mailTable);

    }

    private void DumpExcel(DataTable tbl)
    {
        using (ExcelPackage pck = new ExcelPackage())
        {
            //Create the worksheet
            ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Mailing List");

            //Load the datatable into the sheet, starting from cell A1. Print the column names on row 1
            ws.Cells["A2"].LoadFromDataTable(tbl, false);

            //Header Titles
            ws.Cells["A1"].Value = "Employee Name";
            ws.Cells["B1"].Value = "Email Address";
            ws.Cells["C1"].Value = "Phone";
            ws.Cells["D1"].Value = "Business Unit";
            ws.Cells["E1"].Value = "Site";

            ws.Cells["A1"].AutoFitColumns();

            //Format the header for column 1-3
            using (ExcelRange rng = ws.Cells["A1:E1"])
            {
                rng.Style.Font.Bold = true;
                //Set Pattern for the background to Solid
                rng.Style.Fill.PatternType = ExcelFillStyle.Solid;    
                //Set color to dark blue
                rng.Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.FromArgb(79, 129, 189));  
                rng.Style.Font.Color.SetColor(System.Drawing.Color.White);
            }


            //Write it back to the client
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            Response.AddHeader("content-disposition", "attachment;  filename=MailingList.xlsx");
            Response.BinaryWrite(pck.GetAsByteArray());
        }
    }

对可能发生的事情有什么想法吗?在我尝试将 HTML 数据发送到 Excel 时遇到同样的问题后,有人建议使用 EPPlus,而且它发送的是整个页面,而不仅仅是 gridView。

谢谢

最佳答案

我错过了 Response.Clear()Response.End() :

try {
    var pck = new OfficeOpenXml.ExcelPackage();
    var ws = pck.Workbook.Worksheets.Add("Mailing List");
    ws.Cells["A2"].LoadFromDataTable(tbl, false);
    Response.Clear();
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    Response.AddHeader("content-disposition", "attachment;  filename=MailingList.xlsx");
    Response.BinaryWrite(pck.GetAsByteArray());
} catch (Exception ex) {
    //log error
}
Response.End();

关于c# - 使用 EPPlus,我正在尝试将 gridview 导出到 Excel 工作表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9574279/

相关文章:

android - GIF 图片加载缓慢问题

c# - 无法从 xtragridview 获取点击行的数据

c# - LINQ 到 SQL : To Attach or Not To Attach

c# - 使用通过 Aspx 读取的 Jquery colorbox 和动态图像时出现问题

c# - Active Directory 检查用户是否登录

javascript - ASP.NET 中的自定义复选框设计(ICheck JS)

c# - 生成程序网格时出现旋转问题

c# - 如何将预先格式化的 HTML 元素从 WebMethod 发送到 jQuery

c# - 通过单击按钮将焦点设置到另一个控件

android - 如何为gridview android提供单线边框