Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to create a excel file using EPPlus library in C#.
However, I'm unable to open the created excel file with error message as following:
"Unreadable content found".

EPPlus library link: EPPlus-Create advanced Excel spreadsheets on the server - Download: EPPlus 4.1[^]

Please check my code and let me know what I am missing.

What I have tried:

Following below mentioned is the code I'm using:

C#
using (ExcelPackage pck = new ExcelPackage())
            {
                //Create the worksheet
                ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Demo");

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

                //Format the header for column 1-3
                using (ExcelRange rng = ws.Cells["A1:E1"])
                {
                    rng.Style.Font.Bold = true;
                    rng.Style.Fill.PatternType = ExcelFillStyle.Solid;      
                    rng.Style.Fill.BackgroundColor.SetColor(Color.BurlyWood);  
                    rng.Style.Font.Color.SetColor(Color.White);
                }
                
                //Write it back to the client
                Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";            
                Response.AddHeader("content-disposition", string.Format("attachment;  filename={0}", "ExcellData.xlsx"));
                Response.BinaryWrite(pck.GetAsByteArray());
            }

Below mentioned is dummy data for DataTable which I'm currently trying to show in excel:
            DataTable dt = new DataTable();
            dt.Columns.Add("SkillID");
            dt.Columns.Add("SkillName");
            dt.Columns.Add("BehaviorID");
            dt.Columns.Add("BehaviorName");
            dt.Columns.Add("ObjectiveDesc");

            DataRow dr1 = dt.NewRow();
            dr1[0] = "s11";
            dr1[1] = "s1";
            dr1[2] = "b11";
            dr1[3] = "b1";
            dr1[4] = "obj1";
            dt.Rows.Add(dr1);


            DataRow dr2 = dt.NewRow();
            dr2[0] = "s11";
            dr2[1] = "s1";
            dr2[2] = "b22";
            dr2[3] = "b2";
            dr2[4] = "obj2";
            dt.Rows.Add(dr2);


            DataRow dr3 = dt.NewRow();
            dr3[0] = "s11";
            dr3[1] = "s1";
            dr3[2] = "b33";
            dr3[3] = "b3";
            dr3[4] = "obj3";
            dt.Rows.Add(dr3);
Posted
Updated 15-Jul-17 14:03pm
v2

1 solution

Add Response.End(); at the end of the write to flush all the contents out.

C#
Response.BinaryWrite(pck.GetAsByteArray());
Response.End();


[Properly generating Excel files in .NET]
 
Share this answer
 
Comments
knackCoder 15-Jul-17 23:59pm    
Thank you. It worked for me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900