Cells.ImportData

ImportData(ICellsDataTable, int, int, ImportTableOptions)

Import data from custom data table.

public int ImportData(ICellsDataTable table, int firstRow, int firstColumn, 
    ImportTableOptions options)
ParameterTypeDescription
tableICellsDataTableThe custom data table.
firstRowInt32First row index.
firstColumnInt32First column index.
optionsImportTableOptionsThe import options

Examples

using System;
using System.Collections;
using Aspose.Cells;

namespace AsposeCellsExamples
{
    public class CellsMethodImportDataWithICellsDataTableInt32Int32ImporDemo
    {
        public static void Run()
        {
            Workbook wb = new Workbook();
            
            ArrayList dataLists = new ArrayList();
            dataLists.Add(new object[] { "Name", "Age", "Gender" });
            dataLists.Add(new object[] { "Alice", 30, "Female" });
            dataLists.Add(new object[] { "Bob", 25, "Male" });
            dataLists.Add(new object[] { "Charlie", 35, "Male" });

            ICellsDataTable dt = wb.CellsDataTableFactory.GetInstance(dataLists, true);
            
            wb.Worksheets[0].Cells.ImportData(dt, 0, 0, new ImportTableOptions());
            
            Console.WriteLine("Data imported to first worksheet:");
            Console.WriteLine(wb.Worksheets[0].Cells["A2"].StringValue);
            
            wb.Save("output.xlsx");
        }
    }
}

See Also


ImportData(DataTable, int, int, ImportTableOptions)

Import data from custom data table.

public int ImportData(DataTable table, int firstRow, int firstColumn, ImportTableOptions options)
ParameterTypeDescription
tableDataTableThe DataTable object to be imported.
firstRowInt32First row index.
firstColumnInt32First column index.
optionsImportTableOptionsThe import options

Return Value

Total number of rows imported.

Examples

using System;
using System.Data;
using Aspose.Cells;

namespace AsposeCellsExamples
{
    public class CellsMethodImportDataWithDataTableInt32Int32ImportTableDemo
    {
        public static void Run()
        {
            Workbook workbook = new Workbook();
            Cells cells = workbook.Worksheets[0].Cells;

            // Create sample DataTable
            DataTable dataTable = new DataTable("Products");
            dataTable.Columns.Add("Product_ID", typeof(int));
            dataTable.Columns.Add("Product_Name", typeof(string));
            dataTable.Columns.Add("Units_In_Stock", typeof(int));

            // Add sample data
            DataRow row = dataTable.NewRow();
            row[0] = 1;
            row[1] = "Aniseed Syrup";
            row[2] = 15;
            dataTable.Rows.Add(row);

            row = dataTable.NewRow();
            row[0] = 2;
            row[1] = "Boston Crab Meat";
            row[2] = 123;
            dataTable.Rows.Add(row);

            // Set import options
            ImportTableOptions importOptions = new ImportTableOptions();
            importOptions.IsFieldNameShown = true;

            // Import data starting at row 5, column 5
            cells.ImportData(dataTable, 5, 5, importOptions);

            // Save the workbook
            workbook.Save("ImportDataDemo.xlsx");
        }
    }
}

See Also


ImportData(DataView, int, int, ImportTableOptions)

Import data from data view.

public int ImportData(DataView dataView, int firstRow, int firstColumn, ImportTableOptions options)
ParameterTypeDescription
dataViewDataViewThe DataView object to be imported.
firstRowInt32First row index.
firstColumnInt32First column index.
optionsImportTableOptionsThe import options

Return Value

Total number of rows imported.

Examples

using System;
using System.Data;
using Aspose.Cells;

namespace AsposeCellsExamples
{
    public class CellsMethodImportDataWithDataViewInt32Int32ImportTableODemo
    {
        public static void Run()
        {
            // Create a new workbook
            Workbook workbook = new Workbook();
            Cells cells = workbook.Worksheets[0].Cells;

            // Create sample data table
            DataTable dataTable = new DataTable("TestTable");
            dataTable.Columns.Add("Column1", typeof(string));
            dataTable.Columns.Add("Column2", typeof(int));
            
            // Add sample data
            dataTable.Rows.Add("Data1", 100);
            dataTable.Rows.Add("Data2", 200);
            dataTable.Rows.Add("Data3", 300);

            // Create ImportTableOptions
            ImportTableOptions options = new ImportTableOptions();
            options.IsFieldNameShown = true;
            options.InsertRows = true;

            // Import data from DataView to cells starting at row 0, column 0
            cells.ImportData(dataTable.DefaultView, 0, 0, options);

            // Save the workbook
            workbook.Save("ImportDataOutput.xlsx");
        }
    }
}

See Also


ImportData(IDataReader, int, int)

Imports data from a IDataReader object.

public int ImportData(IDataReader reader, int firstRow, int firstColumn)
ParameterTypeDescription
readerIDataReaderThe IDataReader object which contains data.
firstRowInt32The row number of the first cell to import in.
firstColumnInt32The column number of the first cell to import in.

Return Value

Total number of rows imported.

Examples

namespace AsposeCellsExamples
{
    using Aspose.Cells;
    using System;
    using System.Data;

    public class CellsMethodImportDataWithIDataReaderInt32Int32Demo
    {
        public static void Run()
        {
            // Create a new workbook
            Workbook workbook = new Workbook();
            Worksheet worksheet = workbook.Worksheets[0];

            // Create sample data table
            DataTable dataTable = new DataTable("Employees");
            dataTable.Columns.Add("ID", typeof(int));
            dataTable.Columns.Add("Name", typeof(string));
            dataTable.Columns.Add("Department", typeof(string));
            
            dataTable.Rows.Add(1, "John Doe", "Engineering");
            dataTable.Rows.Add(2, "Jane Smith", "Marketing");
            dataTable.Rows.Add(3, "Mike Johnson", "Sales");

            try
            {
                // Create IDataReader from DataTable
                using (IDataReader dataReader = dataTable.CreateDataReader())
                {
                    // Call ImportData method starting at cell A1 (row 0, column 0)
                    int importedRows = worksheet.Cells.ImportData(dataReader, 0, 0);
                    
                    Console.WriteLine($"Imported {importedRows} rows successfully.");
                    Console.WriteLine($"Data starts at: A1 (Row 0, Column 0)");
                }

                // Auto-fit columns for better visibility
                worksheet.AutoFitColumns();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error executing ImportData method: {ex.Message}");
            }

            // Save the result
            workbook.Save("ImportDataWithIDataReader.xlsx");
        }
    }
}

See Also


ImportData(IDataReader, int, int, ImportTableOptions)

Imports data from a IDataReader object.

public int ImportData(IDataReader reader, int firstRow, int firstColumn, ImportTableOptions options)
ParameterTypeDescription
readerIDataReaderThe IDataReader object which contains data.
firstRowInt32The row number of the first cell to import in.
firstColumnInt32The column number of the first cell to import in.
optionsImportTableOptionsThe options of importing table.

Return Value

Total number of rows imported.

Examples

using System;
using System.Data;
using Aspose.Cells;

namespace AsposeCellsExamples
{
    public class CellsMethodImportDataWithIDataReaderInt32Int32ImportTabDemo
    {
        public static void Run()
        {
            // Create a sample DataTable
            DataTable dt = new DataTable("TestData");
            dt.Columns.Add("Column1", typeof(string));
            dt.Columns.Add("Column2", typeof(int));
            dt.Columns.Add("Column3", typeof(DateTime));
            
            // Add sample data
            dt.Rows.Add("Data1", 100, DateTime.Now);
            dt.Rows.Add("Data2", 200, DateTime.Now.AddDays(1));
            dt.Rows.Add("Data3", 300, DateTime.Now.AddDays(2));

            // Create DataTableReader
            DataTableReader reader = dt.CreateDataReader();

            // Create a new workbook
            Workbook workbook = new Workbook();
            Worksheet worksheet = workbook.Worksheets[0];

            // Set import options
            ImportTableOptions options = new ImportTableOptions();
            options.IsFieldNameShown = true;
            options.InsertRows = true;
            options.ConvertNumericData = true;
            options.DateFormat = "MM/dd/yyyy";

            // Import data starting at row 5, column 0
            int rowsImported = worksheet.Cells.ImportData(reader, 5, 0, options);

            // Save the workbook
            workbook.Save("ImportDataOutput.xlsx");
        }
    }
}

See Also