importList method

int importList(
  1. List<Object?> arrObject,
  2. int firstRow,
  3. int firstColumn,
  4. bool isVertical,
)

Imports an array of objects into a worksheet with specified alignment.

// Create a new Excel Document.
final Workbook workbook = Workbook();

// Accessing sheet via index.
final Worksheet sheet = workbook.worksheets[0];

//Initialize the list
final List<Object> list = [
 'Toatal Income',
 20000,
 'On Date',
 DateTime(2021, 11, 11)
];

//Import the Object list to Sheet
sheet.importList(list, 1, 1, true);

// Save and dispose workbook.
final List<int> bytes = workbook.saveAsStream();
File('Importlist.xlsx').writeAsBytes(bytes);
workbook.dispose();

Implementation

int importList(
    List<Object?> arrObject, int firstRow, int firstColumn, bool isVertical) {
  if (firstRow < 1 || firstRow > _book._maxRowCount) {
    throw Exception('firstRow is not proper');
  }

  if (firstColumn < 1 || firstColumn > _book._maxColumnCount) {
    throw Exception('firstColumn is not proper');
  }

  int i = 0;
  int elementsToImport;

  if (isVertical) {
    elementsToImport =
        min(firstRow + arrObject.length - 1, _book._maxRowCount) -
            firstRow +
            1;
  } else {
    elementsToImport =
        min(firstColumn + arrObject.length - 1, _book._maxColumnCount) -
            firstColumn +
            1;
  }

  Range range;
  if (elementsToImport > 0) {
    range = getRangeByIndex(firstRow, firstColumn);
    if (arrObject[i] == null) {
      range.value = null;
    } else {
      range.value = arrObject[i];
    }
  }

  for (i = 1; i < elementsToImport; i++) {
    if (!isVertical) {
      range = getRangeByIndex(firstRow, firstColumn + i);
    } else {
      range = getRangeByIndex(firstRow + i, firstColumn);
    }
    range.value = arrObject[i];
  }

  return i;
}