EPPlus icon indicating copy to clipboard operation
EPPlus copied to clipboard

Xamarin forms UWP problem: excel cannot open the file because the file format or file extension is not valid.

Open pabloat81 opened this issue 11 months ago • 7 comments

I am testing the library and cant generate a valid output.

This library in version 7 works on UWP?

Tested: ExcelPackage.LicenseContext = LicenseContext.NonCommercial; using (var package = new ExcelPackage(rutaArchivo)) { var sheet = package.Workbook.Worksheets.Add("My Sheet"); sheet.Cells["A1"].Value = "Hello World!";

  // Save to file
  package.Save();

}

Opening the file i get this error: Excel cannot open the file because the file format or file extension is not valid. verify that the file has not been corrupted and that the file extension matches the format of the file.

pabloat81 avatar Mar 04 '24 12:03 pabloat81

What is the value of the variable rutaArchivo?

swmal avatar Mar 04 '24 14:03 swmal

What is the value of the variable rutaArchivo?

The file is created correctly, the path comes from a FileSavePicker: C:\Users\Pablo\Desktop\Export040320241343.xlsx

pabloat81 avatar Mar 04 '24 14:03 pabloat81

That shold be ok then. Does it work if you create a new file on UWP?

ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
using (var package = new ExcelPackage())
{
   var sheet = package.Workbook.Worksheets.Add("My Sheet");
   sheet.Cells["A1"].Value = "Hello World!";

   // Save to file
   package.SaveAs(@"C:\Users\Pablo\Desktop\EPPlusTest.xlsx");
}

If you can upload the corrupted file here we can have a look at it.

swmal avatar Mar 04 '24 15:03 swmal

That shold be ok then. Does it work if you create a new file on UWP?

ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
using (var package = new ExcelPackage())
{
   var sheet = package.Workbook.Worksheets.Add("My Sheet");
   sheet.Cells["A1"].Value = "Hello World!";

   // Save to file
   package.SaveAs(@"C:\Users\Pablo\Desktop\EPPlusTest.xlsx");
}

If you can upload the corrupted file here we can have a look at it.

It creates a new file ok always, but i cant open it.

Export040320241246.xlsx

pabloat81 avatar Mar 04 '24 15:03 pabloat81

That shold be ok then. Does it work if you create a new file on UWP?

ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
using (var package = new ExcelPackage())
{
   var sheet = package.Workbook.Worksheets.Add("My Sheet");
   sheet.Cells["A1"].Value = "Hello World!";

   // Save to file
   package.SaveAs(@"C:\Users\Pablo\Desktop\EPPlusTest.xlsx");
}

If you can upload the corrupted file here we can have a look at it.

I have to use the FileSavePicker to have permision to save the file, if i try the path directly i have an access denied exception.

pabloat81 avatar Mar 04 '24 15:03 pabloat81

The file you sent is an empty file (0 Kb), so nothing has been written to it.

I would suggest that you don't let EPPlus save the file directly via a file path, I assume that it has to be done via the UWP storage API:s somehow. You can get Excel-file as a byte-array by calling:

var bytes = package.GetAsByteArray();

instead of using package.Save() or package.SaveAs(...).

swmal avatar Mar 05 '24 12:03 swmal

The file you sent is an empty file (0 Kb), so nothing has been written to it.

I would suggest that you don't let EPPlus save the file directly via a file path, I assume that it has to be done via the UWP storage API:s somehow. You can get Excel-file as a byte-array by calling:

var bytes = package.GetAsByteArray();

instead of using package.Save() or package.SaveAs(...).

Thanks, i will try that

pabloat81 avatar Mar 05 '24 12:03 pabloat81

Closed due to inactivity

swmal avatar Mar 12 '24 08:03 swmal