Я использую следующий код для преобразования Excel в datatable с помощью 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;
}
Он создает Excel, однако, когда я пытаюсь его открыть, он сообщает мне, что он заблокирован для редактирования другим пользователем и что я могу открыть его только в режиме "Только для чтения".
Я думал, используя:
pck.Dispose();
решит проблему, однако я все равно получаю ту же ошибку.
Кроме того, когда я пытаюсь удалить файл, я получаю сообщение: действие не может быть выполнено, потому что файл открыт в WebDev.WebServer40.EXE.
Есть идеи, как это решить? Заранее спасибо.:)