Wednesday, August 24, 2016

Convert Excel file Stream to DataTable in C# .NET

Using ExcelDataReader library we can convert a file stream excel file to a DataTable. The excel file no need to be saved to the local drive. The sample code is given below.

    byte[] buffer = Convert.FromBase64String(fileData.Replace(' ', '+'));

    Stream stream = new MemoryStream(buffer);

    var result = this.ExcelStreamToDataSet(stream);
public DataSet ExcelStreamToDataSet(Stream stream)
{
    IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);

    excelReader.IsFirstRowAsColumnNames = true;

    var ds = excelReader.AsDataSet();

    excelReader.Close();

    return ds;
}

No comments:

Post a Comment