Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
513 views
in Technique[技术] by (71.8m points)

c# - Using EPPlus, I am trying to export a gridview to an Excel Sheet

Excel is telling me that there is unreadable data, and so after I say to try to recover the information, it shows the correct data. When I open the text file of the xlsx, though, I get all of the html for the entire page, instead of just the gridview (which is probably the unreadable content Excel is talking about).

Here is my code:

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());
        }
    }

Any ideas about what might be happening? Someone suggested using EPPlus after I had the same problems trying to send HTML data to Excel, and it was sending the entire page, instead of just the gridView.

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'm missing the Response.Clear() and the 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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...