deleteRow method

void deleteRow(
  1. int rowIndex, [
  2. int? rowCount
])

Delete the Row in the Worksheet.

// Create a new Excel Document.
final Workbook workbook = Workbook();
// Accessing sheet via index.
final Worksheet sheet = workbook.worksheets[0];
Range range = sheet.getRangeByName('A2');
range.setText('Hello');
range = sheet.getRangeByName('C2');
range.setText('World');

// Delete a row
sheet.deleteRow(1, 1);

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

Implementation

void deleteRow(int rowIndex, [int? rowCount]) {
  if (rowIndex < 1 || rowIndex > workbook.maxRowCount) {
    throw Exception('rowIndex');
  }
  rowCount ??= 1;
  if (rowCount < 0) {
    throw Exception('count');
  }
  for (int count = 1; count <= rowCount; count++) {
    final int lastRow = getLastRow();
    for (int i = rowIndex; i <= lastRow; i++) {
      final Row? row = rows[i];
      if (row != null && i != lastRow && rows[i + 1] != null) {
        rows[i] = rows[i + 1];
        rows[i]!.index = rows[i]!.index - 1;
        for (int j = rows[i]!.ranges.innerList.length; j >= 1; j--) {
          final Range? range = rows[i]!.ranges[j];
          if (range != null) {
            rows[i]!.ranges[j]!.row = rows[i]!.ranges[j]!.row - 1;
            rows[i]!.ranges[j]!.lastRow = rows[i]!.ranges[j]!.lastRow - 1;
            rows[i]!.ranges[j]!.column = rows[i]!.ranges[j]!.column;
            rows[i]!.ranges[j]!.lastColumn = rows[i]!.ranges[j]!.lastColumn;
          }
        }
      } else if (row == null && i != lastRow && rows[i + 1] != null) {
        rows[i] = Row(this);
        rows[i] = rows[i + 1];
        rows[i]!.index = rows[i]!.index - 1;
        for (int j = rows[i]!.ranges.innerList.length; j >= 1; j--) {
          final Range? range = rows[i]!.ranges[j];
          if (range != null) {
            rows[i]!.ranges[j]!.row = rows[i]!.ranges[j]!.row - 1;
            rows[i]!.ranges[j]!.lastRow = rows[i]!.ranges[j]!.lastRow - 1;
            rows[i]!.ranges[j]!.column = rows[i]!.ranges[j]!.column;
            rows[i]!.ranges[j]!.lastColumn = rows[i]!.ranges[j]!.lastColumn;
          }
        }
      } else if (i == lastRow) {
        rows[i] = null;
        if (hyperlinks.count > 0) {
          for (int z = 0; z < hyperlinks.count; z++) {
            if (hyperlinks[z].attachedType ==
                    ExcelHyperlinkAttachedType.range &&
                hyperlinks[z].row > rowIndex) {
              hyperlinks[z].row = hyperlinks[z].row - 1;
            }
          }
        }
      } else {
        rows[i] = rows[i + 1];
      }
    }
  }
}