Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExcelPackage: Office Open XML

Tags:

c#

I am new to this Open Office XML and I was wondering what file extension the XLPackage takes.

For example I assumed I just needed to input the file location of a CSV file I am using, but it does not work, do I have to convert the file to .xlsx or is there something other then the XLPackage that I should use?

The problem is that once it gets to the using a new OpenDialog is initiated and I cant find my file. I am probably just missing something obvious. File Contains Corrupt data, FileFormatException, I assume I need to convert the file before use?

I appreciate any feedback.

Some code:

    FileInfo existingFile = new FileInfo(eFilePath);
        using (ExcelPackage xlPackage = new ExcelPackage(existingFile)) // I think the                                       issue is here.
        {
            ExcelWorksheet exeedSheet = xlPackage.Workbook.Worksheets[1];
            //Total rows
            for (int row = 1; row > 0; )
like image 572
Vasa Serafin Avatar asked Jan 28 '26 11:01

Vasa Serafin


1 Answers

If you are working with a CSV the ExcelPackage is overkill for what you are doing.

CSV:

using (var Sr = new StreamReader("\\SomeCoolFile.CSV"))
{
    var text = Sr.ReadToEnd();
    Sr.Close();
    text = text.Replace("\n", string.Empty);
    var lines = text.Split('\r');
    var info = lines.Select(line => line.Split(',')).ToList();
    ......
}

ExcelPackage:

using (var fs = new FileStream("\\SomeCoolFile.xlsx", FileMode.Open))
{
    using (var package = new ExcelPackage(fs))
    {
        var workBook = package.Workbook;
        .....
     }
 }      
like image 122
NitroxDM Avatar answered Jan 31 '26 00:01

NitroxDM



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!