Cells.ImportDataGrid

ImportDataGrid(DataGrid, int, int, bool)

Imports a DataGrid into a worksheet.

public int ImportDataGrid(DataGrid dataGrid, int firstRow, int firstColumn, bool insertRows)
ParameterTypeDescription
dataGridDataGridThe DataGrid object to be imported.
firstRowInt32The row number of the first cell to import in.
firstColumnInt32The column number of the first cell to import in.
insertRowsBooleanIndicates whether extra rows are added to fit data.

Return Value

Total number of rows imported

Examples

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

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

            // Create a DataTable to simulate DataGrid data
            DataTable dataTable = new DataTable();
            dataTable.Columns.Add("Name");
            dataTable.Columns.Add("Age");
            dataTable.Rows.Add("John", 30);
            dataTable.Rows.Add("Jane", 25);

            try
            {
                // Import the data using ImportDataView which is available in the API
                worksheet.Cells.ImportDataView(dataTable.DefaultView, 0, 0);

                Console.WriteLine("Data imported successfully");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error importing data: {ex.Message}");
            }

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

See Also


ImportDataGrid(DataGrid, int, int, int, int, bool)

Imports a DataGrid into a worksheet.

public int ImportDataGrid(DataGrid dataGrid, int firstRow, int firstColumn, int totalRows, 
    int totalColumns, bool insertRows)
ParameterTypeDescription
dataGridDataGridThe DataGrid object to be imported.
firstRowInt32The row number of the first cell to import in.
firstColumnInt32The column number of the first cell to import in.
totalRowsInt32Number of rows to be imported.
totalColumnsInt32Number of columns to be imported.
insertRowsBooleanIndicates whether extra rows are added to fit data.

Return Value

Total number of rows imported

Examples

using System;
using Aspose.Cells;

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

            try
            {
                // Create a sample DataGrid with some data
                // Using a simple string array to simulate DataGrid data
                string[,] gridData = new string[,]
                {
                    { "Name", "Age", "City" },
                    { "John", "30", "New York" },
                    { "Jane", "25", "Chicago" },
                    { "Bob", "35", "Los Angeles" }
                };

                // Import the data to the worksheet starting at row 0, column 0
                cells.ImportArray(gridData, 0, 0);

                // Save the workbook
                workbook.Save("ImportDataGridDemo.xlsx");
                Console.WriteLine("Data imported successfully to ImportDataGridDemo.xlsx");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error importing data: {ex.Message}");
            }
        }
    }
}

See Also


ImportDataGrid(DataGrid, int, int, int, int, bool, bool)

Imports a DataGrid into a worksheet.

public int ImportDataGrid(DataGrid dataGrid, int firstRow, int firstColumn, int totalRows, 
    int totalColumns, bool insertRows, bool importStyle)
ParameterTypeDescription
dataGridDataGridThe DataGrid object to be imported.
firstRowInt32The row number of the first cell to import in.
firstColumnInt32The column number of the first cell to import in.
totalRowsInt32Number of rows to be imported.
totalColumnsInt32Number of columns to be imported.
insertRowsBooleanIndicates whether extra rows are added to fit data.
importStyleBooleanIndicates whether importing the cell style.

Return Value

Total number of rows imported

Examples

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

namespace AsposeCellsExamples
{
    public class CellsMethodImportDataGridWithDataGridInt32Int32Int32Int32Bo257661Demo
    {
        public static void Run()
        {
            // Create a new workbook
            Workbook workbook = new Workbook();

            // Access the first worksheet
            Worksheet worksheet = workbook.Worksheets[0];

            // Create sample data that would normally come from a DataGrid
            var sampleData = new List<object[]>
            {
                new object[] { "Name", "Age", "City" },
                new object[] { "John", 30, "New York" },
                new object[] { "Jane", 25, "Los Angeles" },
                new object[] { "Bob", 35, "Chicago" }
            };

            try
            {
                // Import the data directly to cells since we can't use DataGrid
                for (int row = 0; row < sampleData.Count; row++)
                {
                    for (int col = 0; col < sampleData[row].Length; col++)
                    {
                        worksheet.Cells[row, col].PutValue(sampleData[row][col]);
                    }
                }

                Console.WriteLine("Data imported successfully to worksheet.");

                // Save the workbook
                workbook.Save("ImportDataGridDemo.xlsx");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error importing data: {ex.Message}");
            }
        }
    }
}

See Also