C# column length of dataset created by Excel with ExcelDataReader is always -1

127 Views Asked by At

I import a file into a dataset with this method using the ExcelDataReader library

using ExcelDataReader;

public DataSet EstraiContenuto(IFormFile file)
{
        using (var inputStream = new MemoryStream())
        {
            // read file to stream
            file.CopyTo(inputStream);
            //converto in dataset

            switch (file.ContentType)
            {
                case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
                    {
                        using var reader = ExcelReaderFactory.CreateReader(inputStream);

                        return reader.AsDataSet(new ExcelDataSetConfiguration()
                        {
                            ConfigureDataTable = (_) => new ExcelDataTableConfiguration()
                            {
                                UseHeaderRow = true
                            }
                        });
                    }               
            }                   
        }       
    }

Then I need to work on it to get length of every column (I need to create a SQL statement to create a table and all columns will be of datatype NVARCHAR).

DataTable dataTable = dati.Tables[0];

foreach (DataColumn column in table.Columns)
{
    column.MaxLength;
}

but MaxLength is never populated.

is there a way to avoid having to manually iterate all rows?

Thanks

0

There are 0 best solutions below