printExcel static method

dynamic printExcel(
  1. String title,
  2. List columns,
  3. List<List> rows
)

Implementation

static printExcel(
    String title, List<dynamic> columns, List<List<dynamic>> rows) async {
  var workbook = Workbook();

  final Worksheet productionSheet = workbook.worksheets[0];
  productionSheet.name = title;
  productionSheet.showGridlines = false;
  productionSheet.enableSheetCalculations();

  var productionRowNumber = 1;
  var endColumn = columns.length;

  final Range productionRange6 = productionSheet.getRangeByIndex(
      productionRowNumber, 1, productionRowNumber, endColumn);
  productionRange6.cellStyle.fontSize = PlexFontSize.smallest;
  productionRange6.cellStyle.bold = true;
  productionRange6.cellStyle.backColorRgb = Colors.grey;

  var currentColumn = 1;
  for (var column in columns) {
    productionSheet
        .getRangeByIndex(productionRowNumber, currentColumn++)
        .setText(column.toString());
  }

  productionRowNumber++;
  for (var row in rows) {
    currentColumn = 1;
    for (var data in row) {
      productionSheet
          .getRangeByIndex(productionRowNumber, currentColumn++)
          .setText(data.toString());
    }
    productionRowNumber++;
  }

  var productionLastRow = productionRowNumber - 1;

  productionSheet
      .getRangeByIndex(1, 1, productionLastRow, endColumn)
      .autoFitColumns();
  productionSheet
      .getRangeByIndex(1, 1, productionLastRow, endColumn)
      .cellStyle
      .borders
      .bottom
      .lineStyle = LineStyle.dashed;
  productionSheet
      .getRangeByIndex(1, 1, productionLastRow, endColumn)
      .cellStyle
      .borders
      .bottom
      .colorRgb = Colors.grey;

  final List<int> bytes = workbook.saveAsStream();
  workbook.dispose();

  return await saveExcelFile(title, bytes);
}