How to read a spreadsheet with formulas

Asked

Viewed 51 times

-2

I wonder if anyone has ever faced that question. I have a code that normally reads data from a spreadsheet, but the tab I need to read has formulas and presents an error. I am using Epplus when I try to read the spreadsheet presents the error:

Officeopenxml.Packaging.Ionic.zip.Badreadexception: 'Could not read block - no data! (position 0x00090AAD)'

Follows the code :

public ActionResult Index(FileUploadViewModel model)
  {
      string rootfolder = _hostingEnvironment.WebRootPath;
      string filename = Guid.NewGuid().ToString() + model.Planilha.FileName;

      FileInfo file = new FileInfo(Path.Combine(rootfolder, filename));
      using (var stream = new MemoryStream())
      {
          ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
          model.Planilha.CopyToAsync(stream);
          using (var package = new ExcelPackage(stream))
          {
              package.SaveAs(file);
          }

      }

      using (ExcelPackage package = new ExcelPackage(file))
      {
          //ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
          ExcelWorksheet worksheet = package.Workbook.Worksheets.FirstOrDefault();
          if (worksheet == null)
          {
              ModelState.AddModelError("", "Planilha está vazia");
          }
          else
          {
              var countrow = worksheet.Dimension.Rows;
              for (int row = 2; row < countrow; row++)
              {
                  model.StaffInfoViewModel.StaffList.Add(new StaffInfoViewModel
                  {
                      Scripts = (worksheet.Cells[row, 1].Value ?? string.Empty).ToString().Trim(),

                  });
              }
          }
      }

      return View(model);
  }
  • Where exactly does the error occur? submit a [mcve]

  • Good morning Leandro, I am using a spreadsheet that generates a script to run in the database, it has a formula, if I take these lines and put in another spreadsheet the system normally reads the same problem is when it reads the spreadsheet with formulas, it follows the code where it presents the error: using (var stream = new Memorystream()) { Excelpackage.Licensecontext = Licensecontext.Noncommercial; model.Planilha.Copytoasync(stream); using (var package = new Excelpackage(stream))-error is here { package.Saveas(file); } }

  • Error points to this passage: using (var stream = new MemoryStream()) { ExcelPackage.LicenseContext = LicenseContext.NonCommercial; model.Planilha.CopyToAsync(stream); using (var package = new ExcelPackage(stream))--erro é aqui { package.SaveAs(file); } }

  • No one to help?

1 answer

-1

Instead of Epplus, use SpreadsheetlightBaixar pacotes do Nuget

Add the packages by Nuget: Documentformat.Openxml Open-XML-SDK Spreadsheetlight

using Documentformat.OpenXml.Spreadsheet; using Spreadsheetlight;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.