How to open CSV files in flutter?

Viewed 349

I am creating a CSV file and I want to open the CSV file in Excel. How to do this in Flutter? I tried using open_file but I don't think it supports .csv files.

String dir = (await getExternalStorageDirectory()).absolute.path + "/";
String file = "$dir";
File f = new File(file + name + ".csv");
String csv = const ListToCsvConverter().convert(rows);
await f.writeAsString(csv);
Scaffold.of(context).showSnackBar(SnackBar(
  content: Text(name + ".csv created at " + file),
  action: SnackBarAction(
    label: "Open",
    onPressed: () {
      OpenFile.open(
        file + name + ".csv",
        type: "application/vnd.ms-excel",
        uti: "com.microsoft.excel.xls",
      );
    },
  ),
));

Here rows is of type List<List<dynamic>> and that contains the data for CSV file. The file gets created and can be opened with Google Sheets from File Explorer.

0 Answers
Related