Import a CSV or XLSX Into A DataTable Using EPPLus

1

I found a thread on SO and am attempting to import a csv or xls or xlsx into a C# DataTable - but I am getting the below stack trace error:

    System.Runtime.InteropServices.COMException
  HResult=0x8003001D
  Message=A disk error occurred during a write operation. (Exception from HRESULT: 0x8003001D (STG_E_WRITEFAULT))
  Source=EPPlus

This is the code that I am trying to use to import the file - what do I need to change so this executes as it should?

        private OpenFileDialog openFileDialog1 = new OpenFileDialog();
    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;
        }
    }

    private void btnImportSpreadsheet_Click(object sender, EventArgs e)
    {
        DataTable spreadsheetImport = new DataTable();
        openFileDialog1.Filter = "Excel Files|*.xls;*.xlsx;*.csv;";
        DialogResult result = openFileDialog1.ShowDialog();

        if (result == DialogResult.OK)
        {
            string file = openFileDialog1.FileName;
            spreadsheetImport = GetDataTableFromExcel(file);
        }
    }
c#
datatable
epplus
epplus-4
asked on Stack Overflow Aug 1, 2018 by user2676140

1 Answer

1

For work with csv, you must use LoadFromText. Also note that EPPlus can not work with xls, it can read xlsx and xlsm and write xlsx.

char csvDelimiter = ';';
using(var pck = new ExcelPackage())
{
    ExcelWorksheet ws = null;
    if(path.EndsWith(".csv"))
    {
        ws = pck.Workbook.Worksheets.Add("Sheet1");
        ExcelTextFormat format = new ExcelTextFormat()
        {
            Delimiter = csvDelimiter 
        };
        ws.Cells[1, 1].LoadFromText(File.ReadAllText(path), format);
    }
    else
    {
        using (var stream = File.OpenRead(path))
        {
            pck.Load(stream);
        }
        ws = pck.Workbook.Worksheets.First();
    }

    //The rest of your code

}

Another option is to use ExcelDataReader, as it can read xls, xlsx and csv, but cannot write. It has an extension that converts a file to a DataSet

answered on Stack Overflow Aug 1, 2018 by Magnetron • edited Aug 1, 2018 by Magnetron

User contributions licensed under CC BY-SA 3.0