This project has moved and is read-only. For the latest updates, please go here.

MVC 5 Epplus Excel Found Unreadable Content

Sep 3, 2014 at 12:46 PM
Edited Sep 3, 2014 at 12:48 PM
I am trying to write some information to an excel file. I can write in it with using EPPlus. And download the excel file. But when I run and the download this file I see this error.

"Excel found unreadable content in filename.xls. Do you want to recover the contents of this workbook? If you trust the source of this workbook, click Yes."

Even if I did not write anything to file, I get this error. My code in Excel class is:
using MaasRaporlari.Models;
using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;

namespace MaasRaporlari.Controllers
{
    class AyrintiliHarcamaRapor
    {
        public List<AyrintiliHarcamaProgramiVM2> Ayrintili;

        private static int RaporaEklenebilecekIcerikSayısı = 33;

        public string ExcelTemplatePath { get; set; } 
        public string ExcelResultPath { get; set; }
        public FileStream Save()
        {
            try
            {
                ExcelTemplatePath = HttpContext.Current.Server.MapPath("~/Images/AyrintiliHarcamaProgrami.xlsx");
                ExcelResultPath = HttpContext.Current.Server.MapPath("~/Images/Harcama.xls");
                var File = new FileInfo(ExcelTemplatePath);
                using (ExcelPackage package = new ExcelPackage(File))
                {
                    package.Load(new FileStream(ExcelTemplatePath, FileMode.Open));
                    int sheetCount = (int)Math.Ceiling((double)Ayrintili.Count / RaporaEklenebilecekIcerikSayısı);
                    ExcelWorksheet workSheet = package.Workbook.Worksheets["Sheet1"];
                    List<ExcelWorksheet> workSheets = new List<ExcelWorksheet>();
                    workSheets.Add(workSheet);
                    package.Stream.Position = 0;
                    if (sheetCount >= 2)
                    {
                        for (int i = 0; i < sheetCount - 1; i++)
                        {
                            ExcelWorksheet tempSheet = package.Workbook.Worksheets.Add(string.Format("Sheet{0}", i + 2), workSheet);
                            workSheets.Add(tempSheet);
                        }
                    }

                    int icerikSayac = 0;
                    foreach (ExcelWorksheet workSheetItem in workSheets)
                    {
                        package.Stream.Position = 0;
                        decimal MiktarToplam1 = 0;
                        decimal MiktarToplam2 = 0;
                        decimal MiktarToplam3 = 0;
                        decimal MiktarToplam4 = 0;
                        decimal MiktarToplam5 = 0;
                        decimal OranToplam1 = 0;
                        decimal OranToplam2 = 0;
                        decimal OranToplam3 = 0;
                        decimal OranToplam4 = 0;
                        decimal OranToplam5 = 0;

                        MiktarToplam Toplam = new MiktarToplam();

                        int rowPointer = 10;

                        for (int i = 0; i < RaporaEklenebilecekIcerikSayısı; i++)
                        {
                            if (icerikSayac < Ayrintili.Count)
                            {
                                var item = Ayrintili[icerikSayac];

                                package.Stream.Position = 0;

                                MiktarToplam5 = item.Miktar1 + item.Miktar2 + item.Miktar3 + item.Miktar4;
                                OranToplam5 = item.Oran1 + item.Oran2 + item.Oran3 + item.Oran4;

                                MiktarToplam1 += item.Miktar1;
                                MiktarToplam2 += item.Miktar2;
                                MiktarToplam3 += item.Miktar3;
                                MiktarToplam4 += item.Miktar4;

                                OranToplam1 += item.Oran1;
                                OranToplam2 += item.Oran2;
                                OranToplam3 += item.Oran3;
                                OranToplam4 += item.Oran4;


                                workSheetItem.Cells["A" + rowPointer.ToString()].Value = item.One;
                                workSheetItem.Cells["B" + rowPointer.ToString()].Value = item.Two;
                                workSheetItem.Cells["C" + rowPointer.ToString()].Value = item.Aciklama;
                                workSheetItem.Cells["D" + rowPointer.ToString()].Value = item.Miktar1;
                                workSheetItem.Cells["F" + rowPointer.ToString()].Value = item.Miktar2;
                                workSheetItem.Cells["H" + rowPointer.ToString()].Value = item.Miktar3;
                                workSheetItem.Cells["J" + rowPointer.ToString()].Value = item.Miktar4;
                                workSheetItem.Cells["E" + rowPointer.ToString()].Value = item.Oran1;
                                workSheetItem.Cells["G" + rowPointer.ToString()].Value = item.Oran2;
                                workSheetItem.Cells["I" + rowPointer.ToString()].Value = item.Oran3;
                                workSheetItem.Cells["K" + rowPointer.ToString()].Value = item.Oran4;
                                workSheetItem.Cells["AC" + rowPointer.ToString()].Value = MiktarToplam5;
                                workSheetItem.Cells["AD" + rowPointer.ToString()].Value = OranToplam5;

                                rowPointer++;
                                icerikSayac++;

                            }
                        }


                        workSheetItem.Cells["D33"].Value = MiktarToplam1;
                        workSheetItem.Cells["E33"].Value = OranToplam1;
                        workSheetItem.Cells["F33"].Value = MiktarToplam2;
                        workSheetItem.Cells["G33"].Value = OranToplam2;
                        workSheetItem.Cells["H33"].Value = MiktarToplam3;
                        workSheetItem.Cells["I33"].Value = OranToplam3;
                        workSheetItem.Cells["J33"].Value = MiktarToplam4;
                        workSheetItem.Cells["K33"].Value = OranToplam4;

                    }
                    using (FileStream outStream = new FileStream(ExcelResultPath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                    {
                        package.Stream.Position = 0;

                        package.SaveAs(outStream);
                        outStream.Position = 0;
                        package.Stream.Dispose();


                        return (outStream);
                    }

                }

            }


            catch (Exception)
            {
                throw;

            }
        }
        public class MiktarToplam
        {
            public decimal Toplam { get; set; }

            public MiktarToplam()
            {
                Toplam = 0;

            }
        }
    }
}
```
Sep 3, 2014 at 4:27 PM
I never had any issues with mvc, only with webforms, but give a try...take a look at this thread

https://epplus.codeplex.com/discussions/223843

Regards,
Thiago
Sep 3, 2014 at 6:44 PM
thanks but I have already seen it. And it could not help me