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:

  1. Find the correct local path.
    • include path_provider in pubspec.yaml
    • compute a path to destination parent directory, for example documents dir:
Future<String> get _localPath async {
  final directory = await getApplicationDocumentsDirectory();
  return directory.path;
}
  1. Create a reference to the file location.
Future<File> get _localFile async {
  final path = await _localPath;
  return File('$path/filename.xlsx');
}
  1. Write data to the file.
Future<File> writeCounter(Excel excel) async {
  final file = await _localFile;
  return file.writeAsBytes(excel.encode());
}
Share:
4,532
Rick
Author by

Rick

Updated on December 30, 2022

Comments

  • Rick
    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
      Rick almost 3 years
      I 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
      Arijeet over 1 year
      open_file package does not support null safety.
  • Rick
    Rick almost 3 years
    Do 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
    eeqk almost 3 years
    the 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 and dataDirectory (getDataDirectory) on Android: pub.dev/documentation/path_provider/latest/path_provider/…
  • Rick
    Rick almost 3 years
    I 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
    eeqk almost 3 years
    how big are these files?
  • Rick
    Rick almost 3 years
    Not 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
    eeqk almost 3 years
    perhaps 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
    Rick almost 3 years
    Docs say it shouldn't be used for critical data... bummer :(
  • Rick
    Rick almost 3 years
    Hi 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
    Rick almost 3 years
    I'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
    Kumar vishnu about 2 years
    file.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 ?