1
votes

I'm using ExcelPackage throughout my wpf application for generating csvs and xlsxs, whenever I open a xlsx that has been generated by epplus in excel it opens without an error, but when I open a generated csv in excel it shows me

"The file format and extension bonus of "filename.csv" don't match. The file could be corrupted or unsafe. Unless you trust its source, don't open it. Do you want to open it anyway?"

Pressing yes opens the file with no problems, but this error is showing to users and is quite annoying so I'd like to fix it..

The code that is generating the csv is nothing fancy.

using (ExcelPackage p = new ExcelPackage(new FileInfo(path + filename)))
{
    //Create a sheet
    p.Workbook.Worksheets.Add("User Ids");
    ExcelWorksheet ws = p.Workbook.Worksheets[1];

    for (int i = 0; i < lstCsvUsers.Items.Count; i++)
    {
        ws.Cells[i + 1, 1].Value = lstCsvUsers.Items[i].ToString();
    }

    if (!Directory.Exists(path))
        Directory.CreateDirectory(path);

    p.Save();
}

But is it missing something to prevent this error from showing to the user when they open the generated file in excel/ other software?

1

1 Answers

2
votes

EPPLus does not generate CSV files - only xlsx format. If you give it a .csv extension in the path it will save the file with the extension but it is still an .xlsx. It opens because Excel is able to determine that - hence the warning.

If you need to generate a CSV then there are plenty of examples on how to do that without EPPlus.

Writing a CSV file in .net