how to display excel file in flutter

1k Views Asked by At

i am using excel package to view excel file but confuse how to display file in whole body can someone help me with that

var file = "HERE IS THE FILE PATH";
var bytes = File(file).readAsBytesSync();
var excel = Excel.decodeBytes(bytes);

for (var table in excel.tables.keys) {
  print(table); //sheet Name
  print(excel.tables[table].maxCols);
  print(excel.tables[table].maxRows);
  for (var row in excel.tables[table].rows) {
    print("$row");
  }
}

im pass file path but cofuse how to show it inside whole body

2

There are 2 best solutions below

2
krunal Gajera On

This should work:

List<String> rowdetail = [];

_importFromExcel() async {
  var file = "Your file path";
  var bytes = File(file).readAsBytesSync();
  var excel = Excel.decodeBytes(bytes);

  for (var table in excel.tables.keys) {
    for (var row in excel.tables[table]!.rows) {
      print("row: ${row.value}");
      rowdetail.add(row.value);
    }
  }
}
0
Yakup Okumuş On

You can view like this after parse Excel, but if there is complikated excel file, you can't parse it

Widget _excelViewer() {
var table = _excel.tables.keys.first; 
var rows = _excel.tables[table]!.rows;

return SingleChildScrollView(
  scrollDirection: Axis.horizontal,
  child: DataTable(
    columns: _createColumns(rows.first),
    rows: _createRows(rows),
  ),
);
}

 List<DataColumn> _createColumns(List<dynamic> headerRow) {
return headerRow.map((cell) => DataColumn(label: 
Text(cell.toString()))).toList();
}

List<DataRow> _createRows(List<List<dynamic>> rows) {
return rows.skip(1) //bacause first row is header
    .map((row) => DataRow(
          cells: row.map((cell) => 
DataCell(Text(cell.toString()))).toList(),
         ))
     .toList();
}