importData method
Imports collection of ExcelDataRows into a worksheet.
//Create a new Excel Document.
final Workbook workbook = Workbook();
// Accessing sheet via index.
final Worksheet sheet = workbook.worksheets[0];
// Create Data Rows for importing.
final List<ExcelDataRow> dataRows = _buildReportDataRows();
// Import the Data Rows in Worksheet.
sheet.importData(dataRows, 1, 1);
//Save and Dispose.
final List<int> bytes = workbook.saveAsStream();
saveAsExcel(bytes, 'ExcelworksheetProtectionAllOption1.xlsx');
workbook.dispose();
// Custom Report class.
class _Report {
_Report(String name, int juneToJuly, int julyToDec) {
salesPerson = name;
salesJanJune = juneToJuly;
salesJulyDec = julyToDec;
}
late String salesPerson;
late int salesJanJune;
late int salesJulyDec;
}
// Create collection objects for Report class.
List<_Report> _getSalesReports() {
final List<_Report> reports = List.empty(growable: true);
reports.add(_Report("Andy Bernard", 45000, 58000));
reports.add(_Report("Jim Halpert", 34000, 65000));
reports.add(_Report("Karen Fillippelli", 75000, 64000));
reports.add(_Report("Phyllis Lapin", 56500, 33600));
reports.add(_Report("Stanley Hudson", 46500, 52000));
return reports;
}
// Create Data Rows with Collection objects of Report class.
List<ExcelDataRow> _buildReportDataRows() {
List<ExcelDataRow> excelDataRows = [];
final List<_Report> reports = _getSalesReports();
excelDataRows = reports.map<ExcelDataRow>((dataRow) {
return ExcelDataRow(cells: [
ExcelDataCell(columnName: 'Sales Person', value: dataRow.salesPerson),
ExcelDataCell(
columnName: 'Sales Jan to June', value: dataRow.salesJanJune),
ExcelDataCell(
columnName: 'Sales July to Dec', value: dataRow.salesJulyDec),
]);
}).toList();
return excelDataRows;
}
Implementation
void importData(
List<ExcelDataRow> excelDataRows, int rowIndex, int colIndex) {
for (int i = 0; i <= excelDataRows.length; i++) {
final ExcelDataRow dataRow =
i > 0 ? excelDataRows[i - 1] : excelDataRows[i];
for (int j = 0; j < dataRow.cells.length; j++) {
final ExcelDataCell dataCell = dataRow.cells[j];
final Range range = getRangeByIndex(rowIndex + i, colIndex + j);
if (i == 0) {
range.value = dataCell.columnHeader;
} else if (dataCell.value != null) {
if (dataCell.value is Hyperlink) {
final Hyperlink link = dataCell.value! as Hyperlink;
hyperlinks.add(range, link.type, link.address, link.screenTip,
link.textToDisplay);
} else if (dataCell.value is Picture) {
final Picture picture = dataCell.value! as Picture;
picture.row = range.row;
picture.column = range.column;
pictures.innerList.add(picture);
final Hyperlink? link = picture.hyperlink;
if (link != null) {
hyperlinks.addImage(
picture, link.type, link.address, link.screenTip);
}
final int width = picture.width;
final int height = picture.height;
if (_innerGetColumnWidth(range.column) <
_pixelsToColumnWidth(width)) {
setColumnWidthInPixels(range.column, width);
}
if (range.rowHeight < height) {
range.setRowHeight(
book.convertFromPixel(height.toDouble(), 6), true);
}
} else {
range.value = dataCell.value;
}
}
}
}
}