Syncfusion.XlsIO.Base
Example 


Syncfusion.XlsIO Namespace > IWorksheet Interface > ImportDataReader Method : ImportDataReader(IDataReader,Boolean,Int32,Int32,Boolean) Method
The System.Data.SqlClient.SqlDataReader or System.Data.OleDb.OleDbDataReader object which contains data.
TRUE if column names must be imported. FALSE otherwise.
First row from where the data should be imported.
First column from where the data should be imported.
Indicates whether XlsIO should preserve column types from DataReader. By default, preserve type is FALSE. i.e., Setting it to True will import data based on column type, otherwise will import based on value type.

ImportDataReader(IDataReader,Boolean,Int32,Int32,Boolean) Method

Imports data from System.Data.IDataReader into worksheet from the specified row and column along with the preserve type.
Syntax
'Declaration
 
Overloads Function ImportDataReader( _
   ByVal dataReader As IDataReader, _
   ByVal isFieldNameShown As Boolean, _
   ByVal firstRow As Integer, _
   ByVal firstColumn As Integer, _
   ByVal preserveTypes As Boolean _
) As Integer
'Usage
 
Dim instance As IWorksheet
Dim dataReader As IDataReader
Dim isFieldNameShown As Boolean
Dim firstRow As Integer
Dim firstColumn As Integer
Dim preserveTypes As Boolean
Dim value As Integer
 
value = instance.ImportDataReader(dataReader, isFieldNameShown, firstRow, firstColumn, preserveTypes)

Parameters

dataReader
The System.Data.SqlClient.SqlDataReader or System.Data.OleDb.OleDbDataReader object which contains data.
isFieldNameShown
TRUE if column names must be imported. FALSE otherwise.
firstRow
First row from where the data should be imported.
firstColumn
First column from where the data should be imported.
preserveTypes
Indicates whether XlsIO should preserve column types from DataReader. By default, preserve type is FALSE. i.e., Setting it to True will import data based on column type, otherwise will import based on value type.

Return Value

Number of imported rows.
Remarks
This method is supported in ASP.NET Core, Xamarin, and UWP platforms from .NET Standard 2.0 onward. To know more about ImportDataReader refer this link
Example
The following code illustrates how to Imports data from System.Data.IDataReader into worksheet from the specified row and column.
using Syncfusion.XlsIO;
using System.Data;
             
class Example
{
  static void Main()
  {
     ExcelEngine excelEngine = new ExcelEngine();
     IApplication application = excelEngine.Excel;
     application.DefaultVersion = ExcelVersion.Excel2013;
     IWorkbook workbook = application.Workbooks.Create(1);
     IWorksheet worksheet = workbook.Worksheets[0];
        
     //Initialize DataTable and get data from SampleDataTable method
     DataTable table = SampleDataTable();
     
     //Create IDataReader using Datatable 
     IDataReader reader = table.CreateDataReader();
  
     //Import data from DataReader
     worksheet.ImportDataReader(reader,true, 1, 1,true);
     
     workbook.SaveAs("ImportDataReader.xlsx");
     workbook.Close();
     excelEngine.Dispose();
  } 
  
  static DataTable SampleDataTable()
  {
    // Here we create a three columns
    DataTable table = new DataTable();
    table.Columns.Add("ID", typeof(int));
    table.Columns.Add("Item", typeof(string));
    table.Columns.Add("Name", typeof(string));
            
    // Here we add Ten DataRows
    table.Rows.Add(1, "Soap", "David");
    table.Rows.Add(2, "Paste", "Sam");
    table.Rows.Add(3, "Cream", "Christoff");
    table.Rows.Add(4, "Powder", "Janet");
    table.Rows.Add(5, "Snacks", "Andrew");
    table.Rows.Add(6, "Perfume", "Thomos");
    table.Rows.Add(7, "Biscuit", "Stephen");
    table.Rows.Add(8, "Cake", "Jones");
    table.Rows.Add(9, "Fruit", "Yabes");
    table.Rows.Add(10, "vegetable", "Marsion");
    return table;
  }
}
AssemblyVersion

Syncfusion.XlsIO.Base: 16.4460.0.40

See Also