Xceed Workbooks for .NET v3.0 Documentation
Xceed.Workbooks.NET Assembly / Xceed.Workbooks.NET Namespace / Worksheet Class / ImportData Method
The data to import. 
The data can be of any of the supported types:

The options when importing the data. 
These can be any of the following: 

Example


In This Topic
    ImportData Method
    In This Topic

    Imports data of type Array, 2D-Array, ArrayList, ICollection, IDictionary, DataTable, DataView or CSV path/stream in a Worksheet.

    Syntax
    'Declaration
     
    
    Public Function ImportData( _
       ByVal data As Object, _
       Optional ByVal options As ImportOptions _
    ) As Integer
    'Usage
     
    
    Dim instance As Worksheet
    Dim data As Object
    Dim options As ImportOptions
    Dim value As Integer
     
    value = instance.ImportData(data, options)
    public int ImportData( 
       object data,
       ImportOptions options
    )

    Parameters

    data
    The data to import. 
    The data can be of any of the supported types:
    • Array (like string[], int[], MyProduct[], object[]...)
    • 2D-Array (like double[,], string[,], MyProduct[,], object[,])
    • ArrayList
    • ICollection (like List<string>, List<MyProduct>, ObservableCollection<MyProduct>, Array<int>, ...)
    • IDictionary (like Dictionary<int, string>, Dictionary<int, MyProduct>, ...)
    • DataTable (with DataColumns and DataRows)
    • DataView
    • CSV path/stream (a path to a CSV document or a stream based on a CSV document).
    options

    The options when importing the data. 
    These can be any of the following: 

    Return Value

    The number of items imported in the Worksheet.
    Remarks
    This method offers more flexibility when importing data. For example, it allows to determine where the data will be imported in the Worksheet, the data's direction and whether the Columns' names should be shown or not.
    Example
    var workbook = Workbook.Create("test.xlsx" );
    
    var worksheet = workbook.Worksheets[ 0 ];
    
    // Define a dataTable, the import options(show specific ColumnNames) and call the ImportData function.
    
      var dataTable = new DataTable( "Employees" );
      dataTable.Columns.Add( "Name", typeof( string ) );
      dataTable.Columns.Add( "Position", typeof( string ) );
      dataTable.Columns.Add( "Experience", typeof( double ) );
      dataTable.Columns.Add( "Salary", typeof( int ) );
      dataTable.Rows.Add( "Jenny Melchuck", "Project Manager", 11.5d, 77000 );
      dataTable.Rows.Add( "Cindy Gartner", "Medical Assistant", 1.3d, 56000 );
      dataTable.Rows.Add( "Carl Jones", "Web Designer", 4d, 66000 );
      dataTable.Rows.Add( "Anna Karlweiss", "Account Executive", 7.8d, 51000 );
      dataTable.Rows.Add( "Julia Robertson", "Marketing Coordinator", 17.6d, 65000 );
      var dataTableImportOptions = new DataTableImportOptions() { DestinationTopLeftAddress = "B5", ColumnNames = new string[] { "Name", "Experience", "Position" }, IsColumnNamesShown = true };
      worksheet.ImportData( dataTable, dataTableImportOptions );
    
    workbook.Save();
    var workbook = Workbook.Create("test.xlsx" );
    
    var worksheet = workbook.Worksheets[ 0 ];
    
    // Define a list of user objects, the import options(vertical by default, specify PropertyNames and show propertyNames) and call the ImportData function.
    
      var userObjectData = new List<Player>()
      {
        new Player() { Name = "Tom Sawyer", Team = Team.Miami_Ducks, Number = 9 },
        new Player() { Name = "Mike Smith", Team = Team.Chicago_Hornets, Number = 18 },
        new Player() { Name = "Kelly Tomson", Team = Team.LosAngelese_Raiders, Number = 33 },
        new Player() { Name = "John Graham", Team = Team.NewYork_Bucs, Number = 7 },
      };
      
      var userObjectImportOptions = new UserObjectImportOptions() { DestinationTopLeftAddress = "H5", PropertyNames = new string[] { "Name", "Team" }, IsPropertyNamesShown = true };
      worksheet.ImportData( userObjectData, userObjectImportOptions );
    
    workbook.Save();
    
    private class Player
    
      {
        public string Name { get; set; }
    
        public int Number { get; set; }
    
        public Team Team { get; set; }
      }
    Requirements

    Target Platforms: Windows 11, Windows 10, Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

    See Also