Roberto Mindoli
Roberto Mindoli

Reputation: 15

C# Epplus Save Byte file as XLS in a Desktop Application

I need to save an ExcelPackage as XLS file. I try to do this and i know how to change content type in a web application but i can't find any solution in a Desktop environment.

Now i am using this code:

    using (ExcelPackage p = new ExcelPackage())
    {
       string filename = "OCL_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
       string PathToCSV = "App_Data/OCL/";
       string OCLfolder = AppDomain.CurrentDomain.BaseDirectory + PathToCSV;
       string savepath = OCLfolder + "/" + filename;
       System.IO.FileInfo strfilePath = new System.IO.FileInfo(savepath);
       //create a new workbook
       p.Workbook.Properties.Created = DateTime.Now;
       p.Workbook.Properties.Title = filename;
       //create a new worksheet
       p.Workbook.Worksheets.Add(filename);
       ExcelWorksheet ws = p.Workbook.Worksheets[1];
       createExcelData(ws, idCliente, idDestinatario, idSpedDati);
       ws.Cells[ws.Dimension.Address].AutoFitColumns();
       for (int col = 1; col <= ws.Dimension.End.Column; col++)
       {
          ws.Column(col).Width = ws.Column(col).Width + 1;
       }
       byte[] bin = p.GetAsByteArray();
       strfilePath.Directory.Create();
       File.WriteAllBytes(savepath, bin);
       return savepath;
   }

If i save the file as .xlsx i have no problem but if i save as .xls i have an error before opening the file:

"The file format differs from the format that the file name extension specifies"

Upvotes: 1

Views: 6337

Answers (2)

Pete
Pete

Reputation: 1867

If your client insists on .XLS format and you can't convince them to update to something that's actually supported by MS then I'd suggest you take a look at using NPOI, an open-source project for reading/writing files in the older MS Office formats

Upvotes: 2

suraj mahajan
suraj mahajan

Reputation: 870

EPPlus does not work with the XLS format. Only XLSX.

Upvotes: 2

Related Questions