Other API Samples

Live SpreadsheetGear API Samples

Range Samples Values and Formulas Values from Array

Set cell values from a two-dimensional object array.

// Create a new workbook and some local variables.
SpreadsheetGear.IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook();
SpreadsheetGear.IWorksheet worksheet = workbook.ActiveWorksheet;
SpreadsheetGear.IRange cells = worksheet.Cells;

// Get a reference to a range of cells.
SpreadsheetGear.IRange range = cells["A1:J100"];

// Create an array of values matching the size of 
// the range which is 100 rows x 10 columns.
int rowCount = range.RowCount;
int colCount = range.ColumnCount;
object[,] values = new object[rowCount, colCount];
for (int row = 0; row < rowCount; row++)
{
    for (int col = 0; col < colCount; col++)
        values[row, col] = "Cell=" + range.WorkbookSet.GetAddress(row, col);
}

// Set the array values into the range.
range.Value = values;

// AutoFit the range.
range.Columns.AutoFit();
Run This Sample
Download File

Download an Excel Open XML Workbook (*.xlsx) file with the results of this sample.

Related Samples