Saving an excel file to a device using Flutter
4,532
According to Read and write files
cookbook: https://flutter.dev/docs/cookbook/persistence/reading-writing-files
you need to follow these steps:
- Find the correct local path.
- include
path_provider
inpubspec.yaml
- compute a path to destination parent directory, for example documents dir:
- include
Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory();
return directory.path;
}
- Create a reference to the file location.
Future<File> get _localFile async {
final path = await _localPath;
return File('$path/filename.xlsx');
}
- Write data to the file.
Future<File> writeCounter(Excel excel) async {
final file = await _localFile;
return file.writeAsBytes(excel.encode());
}
Author by
Rick
Updated on December 30, 2022Comments
-
Rick over 1 year
I'm would like to save an excel file that I generate on the fly in a flutter app. I want to save the file on the users device, which is going to be on a mobile. However, I'm trying to figure out where and how to save it.
This is what I have so far using this excel package from flutter:
https://pub.dev/packages/excel
// Some function code... // Trying to save the file somewhere on a device excel.encode().then((onValue) { File("/some/filepath/but/not/sure/where/it/should/go") ..createSync(recursive: true) ..writeAsBytesSync(onValue); });
Anyone know the best way to do this?
-
Rick almost 3 yearsI ended up switching from excel and used instead syncfusion_flutter_xlsio coupled with path_provider and open_file packages. But I think the excel package would work too with open_file as it allows me to open up my already modified file and then save it and export it from there. The package: pub.dev/packages/open_file
-
Arijeet over 1 yearopen_file package does not support null safety.
-
-
Rick almost 3 yearsDo you know where I could access these files after creation if I create them using getApplicationDocumentsDirectory(), that is where on the device? I mean where does getApplicationDocumentsDirectory() really lead to?
-
eeqk almost 3 yearsthe easiest way to check it without reading docs/using search would be to print
file.path
. Also,path_provider
API mentions two places:NSDocumentDirectory
on iOS anddataDirectory
(getDataDirectory
) on Android: pub.dev/documentation/path_provider/latest/path_provider/… -
Rick almost 3 yearsI end up using this path but there are no excel files being saved there (using @mightybruno answer). I'm also looking for a much more user friendly way to save the excel files on user's device. I don't want to have to download the container from Xcode to view the files if they even would show up there... maybe there is some other flutter package that I need to use.
-
eeqk almost 3 yearshow big are these files?
-
Rick almost 3 yearsNot big at all. Excel files with 4 columns and 1-12 rows being used on each who only contain text. Where on my mobile device are they suppose to show up? Is it only possible to download them via Xcode?
-
eeqk almost 3 yearsperhaps you could store the files inside
shared_preferences
then? I don't have any iOS experience so I won't be able to help you with anything related to it or xcode pub.dev/packages/shared_preferences -
Rick almost 3 yearsDocs say it shouldn't be used for critical data... bummer :(
-
Rick almost 3 yearsHi again! I got it to work and I know for sure that I'm saving the files and where. But I have noooo idea where I can actually access them...
-
Rick almost 3 yearsI've got it to work using path_provider, among other things, with similar approach that would save files on a device. Although been finally able to save it exposed another tricky situation of actually fetching the saved file. But your comment was of help and actually answered the question so I'll give you solution for it. Thank you.
-
Kumar vishnu about 2 yearsfile.writeAsBytes(excel.encode()) is giving error as excel.encode() type is List<int>? whereas writeAsBytes() accepts "List<int>" parameter. Anyone know how to solve this ?