使用EPPLus将Gridview导出到Excel [英] Export Gridview to Excel with EPPLus

查看:80
本文介绍了使用EPPLus将Gridview导出到Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用EPPlus将数据从数据表导出到Excel文件,但似乎无济于事。
这是代码-

I am trying to export data from datatable to Excel file using EPPlus, but nothing seems to work. This is the code-

using (ExcelPackage xp = new ExcelPackage())
{
    ExcelWorksheet ws = xp.Workbook.Worksheets.Add(dt.TableName);

    int rowstart = 2;
    int colstart = 2;
    int rowend = rowstart;
    int colend = colstart + dt.Columns.Count;

    ws.Cells[rowstart, colstart, rowend, colend].Merge = true;
    ws.Cells[rowstart, colstart, rowend, colend].Value = dt.TableName;
    ws.Cells[rowstart, colstart, rowend, colend].Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;
    ws.Cells[rowstart, colstart, rowend, colend].Style.Font.Bold = true;
    ws.Cells[rowstart, colstart, rowend, colend].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
    ws.Cells[rowstart, colstart, rowend, colend].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.LightGray);

    rowstart += 2;
    rowend = rowstart + dt.Rows.Count;
    ws.Cells[rowstart, colstart].LoadFromDataTable(dt, true);
    int i = 1;
    foreach (DataColumn dc in dt.Columns)
    {
        i++;
        if (dc.DataType == typeof(decimal))
        ws.Column(i).Style.Numberformat.Format = "#0.00";
    }
    ws.Cells[ws.Dimension.Address].AutoFitColumns();

    ws.Cells[rowstart, colstart, rowend, colend].Style.Border.Top.Style =
       ws.Cells[rowstart, colstart, rowend, colend].Style.Border.Bottom.Style =
       ws.Cells[rowstart, colstart, rowend, colend].Style.Border.Left.Style =
       ws.Cells[rowstart, colstart, rowend, colend].Style.Border.Right.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;


    Response.AddHeader("content-disposition", "attachment;filename=logs.xlsx");
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    Response.BinaryWrite(xp.GetAsByteArray());
    Response.Close();
    HttpContext.Current.ApplicationInstance.CompleteRequest();
}

未显示异常,代码运行平稳,但未创建文件。
我真的不知道如何了解这行代码背后的情况,因为它什么也没显示。但我可以看到工作表中的单元格已填充。
我尝试了此网站和其他网站上提供的所有其他问题和解决方案。但是似乎没有任何作用。因此,请不要仅提供其他问题和解决方案的链接。
任何其他类型的建议,将不胜感激。

no exception is shown, code runs smoothly but no file is created. I really dont know how to get what's happening behind this lines as its showing nothing. But I can see the cells in the sheet are filled. I tried every other question and solution available on this site and other sites also. But nothing seems to work. so please dont only provide link to other questions and solutions. Any other kind of advice will be appreciated.

推荐答案

我是在另一个工作的页面的帮助下完成此操作的仅取文件名和路径并发送给响应。
如果有人像我一样卡在你做正确的事的地方,那么其他页面上的事都会起作用,而不是你想要的地方。因此,您可以尝试一下。在这种情况下,将不会进行刷新或闪烁。用户不会觉得根本没有使用其他页面。

I have done this with help of another page who's job was just to take the file name and path and send to response. If anyone stucks like me where you are doing everything right, things working on every other page but not where you want. So you can try this. No refresh or blinking will be done in this case. User wont feel that there was other page used at all.

CardLogs.aspx.cs

CardLogs.aspx.cs

string filename = "logs_" + DateTime.Now.ToString("MMddyyyy") + ".xlsx";
                FileInfo excelFile = new FileInfo(Server.MapPath("~/Uploads/" + filename));
                excel.SaveAs(excelFile);
                Session["fileName"] = excelFile.FullName;
                Response.Redirect("exportdata.aspx"); 

exportdata.aspx.cs文件-

exportdata.aspx.cs file -

 protected void Page_Load(object sender, EventArgs e)
    {
        string filePath = HttpContext.Current.Session["fileName"].ToString();
        if (!string.IsNullOrEmpty(filePath))
        {
            Response.ContentType = ContentType;
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
            Response.WriteFile(filePath);
            Response.Flush();
            File.Delete(filePath);                
        }

    }

这篇关于使用EPPLus将Gridview导出到Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆