0

I am using EPPlus control in project. EPPLUS is not returning a file (download functionality not working, nothing happens). No errors on build or console errors. Here is ExportToExcel method:

[HttpPost]
    public void ExportUExcel(List<int> napuniNiz)
    {
        try
        {
            var lista = new List<Models.OsnovnoSredstvoExportUExcelModel>();

            foreach (var osnovnoSredstvoId in napuniNiz)
            {
                var objecat = new Models.OsnovnoSredstvoExportUExcelModel();
                var OsnovnoSredstvo = _db.OsnovnaSredstva.Include("Grupa").Include("KorisnikOsnovnogSredstva").Include("StanjeOS").Include("StatusOS").Include("Lokacija").Include("MjestoTroska").Include("AmortizacionaGrupa").Include("Dobavljac").Include("Proizvodjac").FirstOrDefault(x => x.Id.Equals(osnovnoSredstvoId));
                objecat.Id = OsnovnoSredstvo.Id;
                objecat.InventurniBroj = OsnovnoSredstvo.InventurniBroj;
                objecat.Sifra = OsnovnoSredstvo.Sifra;
                objecat.Naziv = OsnovnoSredstvo.Naziv;
                objecat.DatumPostavljanja = OsnovnoSredstvo.DatumPostavljanja;
                objecat.DobavljacNaziv = OsnovnoSredstvo.Dobavljac != null ? OsnovnoSredstvo.Dobavljac.Naziv : "";
                objecat.MjestoTroska = OsnovnoSredstvo.MjestoTroska.Konto;
                objecat.Lokacija = OsnovnoSredstvo.Lokacija != null ? OsnovnoSredstvo.Lokacija.Naziv : "";
                objecat.ProizvodjacNaziv = OsnovnoSredstvo.Proizvodjac != null ? OsnovnoSredstvo.Proizvodjac.Naziv : "";
                objecat.NabavnaCijena = OsnovnoSredstvo.NabavnaCijena.ToString();
                objecat.AkumuliranaAmortizacija = OsnovnoSredstvo.AkumuliranaAmortizacija.ToString();
                objecat.KnjiznaVrijednost = OsnovnoSredstvo.KnjiznaVrijednost.ToString();
                objecat.Korisnik = OsnovnoSredstvo.KorisnikOsnovnogSredstva != null ? (OsnovnoSredstvo.KorisnikOsnovnogSredstva.Ime + ' ' + OsnovnoSredstvo.KorisnikOsnovnogSredstva.Prezime) : "";
                lista.Add(objecat);
            }

            var Model = lista.ToList();

            using (var package = new OfficeOpenXml.ExcelPackage())
            {
                ExcelWorksheet ws = package.Workbook.Worksheets.Add("Sheet1");


                using (var range = ws.Cells[1, 1, 1, 12])
                {
                    range.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    range.Style.Fill.BackgroundColor.SetColor(Color.Coral);
                }

                using (var range = ws.Cells[2, 1, Model.Count + 1, 12])
                {
                    range.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    range.Style.Fill.BackgroundColor.SetColor(Color.BlanchedAlmond);
                }

                ws.Cells["A1"].LoadFromCollection(Collection: Model, PrintHeaders: true);

                Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                Response.AddHeader("content-disposition", "attachment;filename=PregledOsnovnihSredstava" + DateTime.Now.ToString("dd.MM.yyyy") + ".xlsx");
                Response.BinaryWrite(package.GetAsByteArray());

                Response.End();
            }

        }
        catch (Exception ex)
        {
            var err = ex.Message;
        }
    }

I don't know why download functionality does not work. My model is properly filled with data. Any help will be appreciated.

oknevermind
  • 101
  • 3
  • 17
  • It should be an `ActionResult`, and it should be called from your front end without using ajax. – Hackerman Jul 05 '18 at 14:30
  • Hi @Hackerman, I am try it (using ActionResult method), but nothing happens. I not use ajax. In frontend I use angularjs service `$http({ url: '/OsnovnoSredstvoExportUExcelMVC/ExportUExcel', method: "POST", data: { napuniNiz: listaId } }).then(function (result) { deferred.resolve(result.data); }, function () { deferred.reject(); }); ` I get data in the parameter `List napuniNiz`. what I noticed is (when hit break point on ws) that I have an exception on: ws.Cells.Current property. – oknevermind Jul 06 '18 at 07:47
  • Exception is: `'ws.Cells.Current' threw an exception of type 'System.NullReferenceException'`. I do not know what else I can do about this. – oknevermind Jul 06 '18 at 07:49
  • Try downloading by removing your "using (var range==...){ } using (var range==...){ }" lines.Do you still get the exception? – Sanjay Jul 06 '18 at 11:40
  • I've comment this part of the code (about var using range ==), but still get the exception. Thanks for the comment. – oknevermind Jul 06 '18 at 12:01
  • @oknevermind 1) `I not use ajax. In frontend I use angularjs service` that angularjs service uses ajax, so you use ajax 2) `Exception is: ... NullReferenceException` here is [what is a NullReferenceException, and how do I fix it](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it). – Vanity Slug - codidact.com Jul 18 '18 at 17:13

0 Answers0