Search code examples
c#exceldatareader

0002 string value getting truncated to 2 while converting to DataTable using ExcelDataReader


private DataTable ExcelToDataTable(IFormFile file)
    {
        using (var stream = file.OpenReadStream())
        {
            using (var reader = ExcelReaderFactory.CreateReader(stream))
            {
                var result = reader.AsDataSet(new ExcelDataSetConfiguration()
                {
                    ConfigureDataTable = (data) => new ExcelDataTableConfiguration()
                    {
                        UseHeaderRow = true,
                    }
                });

                DataTableCollection table = result.Tables;
                DataTable resultTable = table["Employee Information"];

                return resultTable;
            }
        }
    }

Hi All, There is a column named uniquenumber in excel which contains the value 0002, when the data is converted to DataSet, the column value is changed to 2.

Can anyone help me fixing it ?


Solution

  • Make sure the cell is formatted as 'Text' when the cell is formatted as 'General' and begins with a zero the value is considered a number and all leading zeros are scrapped.