Reputation: 117
I need to read a excel file of .xls format from external storage. Using file picker i have got its path. But now the problem is i have to read the excel file row wise and save it in an array.
I could not find any library like poi similar to java.
The expected result should be like Workbook=>sheet1=>row(i)
Upvotes: 0
Views: 4733
Reputation: 21
OutlinedButton(
onPressed: () async {
ByteData data = await rootBundle.load("assets/mydata.xlsx");
_upload(data);
}
)
static Future<void> _upload(var data) async {
var bytes = data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
var excel = Excel.decodeBytes(bytes);
List<dynamic> excelList = [];
for (var table in excel.tables.keys)
{
for(int rowIndex= 1 ;rowIndex <=excel.tables[table].maxRows; rowIndex++)
{
Sheet sheetObject = excel['Sheet1'];
var excelfileDetails = new MyExcelTable();
excelfileDetails.name = sheetObject.cell(CellIndex.indexByColumnRow(columnIndex:0,rowIndex: rowIndex)).value.toString();
excelfileDetails.age = sheetObject.cell(CellIndex.indexByColumnRow(columnIndex:1,rowIndex: rowIndex)).value;
excelfileDetails.state = sheetObject.cell(CellIndex.indexByColumnRow(columnIndex:2,rowIndex: rowIndex)).value.toString();
excelfileDetails.country = sheetObject.cell(CellIndex.indexByColumnRow(columnIndex:3,rowIndex: rowIndex)).value.toString();
excelfileDetails.occupation = sheetObject.cell(CellIndex.indexByColumnRow(columnIndex:4,rowIndex: rowIndex)).value.toString();
excelList.add(excelfileDetails);
}
}
}
class MyExcelTable
{
var name;
var age;
var state;
var country;
var occupation;
MyExcelTable({this.name, this.age, this.state, this.country, this.occupation});
}
Upvotes: 1
Reputation: 1
have a try : https://pub.dev/packages/spreadsheet_decoder
import 'dart:io';
import 'package:spreadsheet_decoder/spreadsheet_decoder.dart';
main() {
var bytes = new File.fromUri(fullUri).readAsBytesSync();
var decoder = new SpreadsheetDecoder.decodeBytes(bytes);
var table = decoder.tables['Sheet1'];
var values = table.rows[0];
...
decoder.updateCell('Sheet1', 0, 0, 1337);
new File(join(fullUri).writeAsBytesSync(decoder.encode());
...
}
Upvotes: 0
Reputation: 789
Does it have to be an Excel file or could you also save it as .csv data? If you can save it as .csv, you can simply read it as a normal Text.
Upvotes: 0