Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel to DataTable using EPPlus - excel locked for editing

Tags:

c#

excel

epplus

I'm using the following code to convert an Excel to a datatable using EPPlus:

public DataTable ExcelToDataTable(string path) {     var pck = new OfficeOpenXml.ExcelPackage();     pck.Load(File.OpenRead(path));     var ws = pck.Workbook.Worksheets.First();     DataTable tbl = new DataTable();     bool hasHeader = true;     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));     }     var startRow = hasHeader ? 2 : 1;     for (var rowNum = startRow; rowNum <= ws.Dimension.End.Row; rowNum++)     {         var wsRow = ws.Cells[rowNum, 1, rowNum, ws.Dimension.End.Column];         var row = tbl.NewRow();         foreach (var cell in wsRow)         {             row[cell.Start.Column - 1] = cell.Text;         }         tbl.Rows.Add(row);     }     pck.Dispose();     return tbl; } 

It creates the Excel, however, when I try to open it, it gives me the message that it is locked for editing by another user and that I can only open it in Read-Only mode.

I thought using:

pck.Dispose(); 

would solve the issue, however I'm still getting the same error.

Also, when I try to delete the file, I get the message: The action can't be completed because the file is open in WebDev.WebServer40.EXE.

Any ideas how to resolve this? Thanks in advance. :)

like image 726
Fahad Avatar asked Nov 15 '12 11:11

Fahad


People also ask

Does EPPlus require Excel?

No, it does not require Excel to be installed on the server, as you can read in the docs: EPPlus is a . NET library that reads and writes Excel files using the Office Open XML format (xlsx). EPPlus has no dependencies other than .

Does EPPlus work with XLS?

EPPlus does not work with the XLS format. Only XLSX. You'll need to find a new library.

What is the use of EPPlus?

EPPlus is a very helpful open-source 3rd party DLL for writing data to excel. EPPlus supports multiple properties of spreadsheets like cell ranges, cell styling, charts, pictures, shapes, comments, tables, protection, encryption, pivot tables, data validation, conditional formatting, formula calculation, etc.


2 Answers

I see, that's what i've posted recently here(now corrected). It can be improved since the ExcelPackage and the FileStream(from File.OpenRead) are not disposed after using.

public static DataTable GetDataTableFromExcel(string path, bool hasHeader = true) {     using (var pck = new OfficeOpenXml.ExcelPackage())     {         using (var stream = File.OpenRead(path))         {             pck.Load(stream);         }         var ws = pck.Workbook.Worksheets.First();           DataTable tbl = new 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));         }         var startRow = hasHeader ? 2 : 1;         for (int rowNum = startRow; rowNum <= ws.Dimension.End.Row; rowNum++)         {             var wsRow = ws.Cells[rowNum, 1, rowNum, ws.Dimension.End.Column];             DataRow row = tbl.Rows.Add();             foreach (var cell in wsRow)             {                 row[cell.Start.Column - 1] = cell.Text;             }         }         return tbl;     } } 
like image 99
Tim Schmelter Avatar answered Sep 19 '22 04:09

Tim Schmelter


A extension version of Tim Schmelter's answer.

public static DataTable ToDataTable(this ExcelWorksheet ws, bool hasHeaderRow = true) {     var tbl = new DataTable();     foreach (var firstRowCell in ws.Cells[1, 1, 1, ws.Dimension.End.Column])         tbl.Columns.Add(hasHeaderRow ?             firstRowCell.Text : string.Format("Column {0}", firstRowCell.Start.Column));     var startRow = hasHeaderRow ? 2 : 1;     for (var rowNum = startRow; rowNum <= ws.Dimension.End.Row; rowNum++)     {         var wsRow = ws.Cells[rowNum, 1, rowNum, ws.Dimension.End.Column];         var row = tbl.NewRow();         foreach (var cell in wsRow) row[cell.Start.Column - 1] = cell.Text;         tbl.Rows.Add(row);     }     return tbl; } 
like image 44
Jafin Avatar answered Sep 21 '22 04:09

Jafin