This project has moved. For the latest updates, please go here.

Issue in Mono with File Loading

Oct 24, 2014 at 2:45 PM
I am using the below code for reading excel file into datatable:
public static DataTable GetDataTableFromExcel(string path)
        {
            DataTable tbl = new DataTable();
            using (var pck = new ExcelPackage())
            {
                using (var stream = File.OpenRead(path))
                {
                    pck.Load(stream);                    
                }

                //GET FIRST SHEET IN EXCEL FILE
                var ws = pck.Workbook.Worksheets.First();
                
                bool hasheader = true; // adjust it accordingly( i've mentioned that this is a simple approach)
                 
                //ADD COLUMNS (SCHEMA) TO DATATABLE
                foreach (var firstrowcell in ws.Cells[1, 1, 1, ws.Dimension.End.Column])
                {
                    tbl.Columns.Add(hasheader ? firstrowcell.Text : string.Format("column {0}", firstrowcell.Start.Column));
                }

                //IF THE EXCEL SHEET HAS FIRST ROW AS COLUMN NAMES THAN START FROM 2nd ROW Else FROM FIRST ROW
                var startrow = hasheader ? 2 : 1;

                for (var rownum = startrow; rownum <= ws.Dimension.End.Row; rownum++)
                {
                    //ws.Cells[rownum, 1, rownum, ws.Dimension.End.Column].Style.Numberformat.Format = "General";
                    var wsrow = ws.Cells[rownum, 1, rownum, ws.Dimension.End.Column];
                    var row = tbl.NewRow();
                    foreach (var cell in wsrow)
                    {
                        if (cell.Start.Column > 20)
                            continue;
                        if (cell.Start.Column == 20)
                        {
                            DateTime dt = new DateTime(1947, 1, 1);
                            DateTime.TryParse(cell.Value.ToString(), out dt);

                            if (dt == new DateTime(0001, 1, 1))
                            {
                                long serialDate = long.Parse(cell.Value.ToString());
                                DateTime result = DateTime.FromOADate(serialDate);
                                row[cell.Start.Column - 1] = result.ToString();
                            }
                            else
                                row[cell.Start.Column - 1] = cell.Value;
                        }
                        else
                            row[cell.Start.Column - 1] = cell.Text;
                    }
                    tbl.Rows.Add(row);
                }
                
            }
            return tbl;
        } 
In my aplication i select an excel file and its loaded in GridView if i select a new file- the above method will be called and data will be loaded in GridView.
But This does not work on Mono. This works in .Net but with the same code when i select a different file from the browse button the first file gets load again and again.
to load a new file i have to close the application and run again.

is there anything wrong with the method code?
do i need to close/Unload the excel in EP code somewhere?


Regards,