资产文件夹中的 Flutter Excel 工作表

mar*_*ong 1 dart flutter

我正在创建一个应用程序,我将在其中发送一封附有 Excel 工作表的电子邮件。
我已经有了 Excel 工作表,我的应用程序将在发送之前将一些数据添加到 Excel 工作表中。

但是,在尝试将 Excel 工作表添加到我的资产文件夹中,添加 pubspec.yaml 的路径后,File 类找不到该文件。

文件结构

pubspec.yaml:

 assets:
   - logo.png
   - Declaratieformulier.xlsx
Run Code Online (Sandbox Code Playgroud)

功能:

openFile() {
   var bytes = new File("assets/Declaratieformulier.xlsx").readAsBytesSync();
   var decoder = new SpreadsheetDecoder.decodeBytes(bytes);
   var table = decoder.tables['Blad1'];
Run Code Online (Sandbox Code Playgroud)

我做了一些挖掘,发现资产并没有作为文件放在设备上,而是包含在 APK 中。

所以我的问题是:如何在我的应用程序中包含 Excel 工作表,以便我可以读取它并将其另存为手机上的新 Excel 文件?

Lor*_*eto 5

您必须在pubspec.yamlso 中插入完整路径,而不是Declaratieformulier.xlsx添加assets/Declaratieformulier.xlsx.

assets:
  - logo.png
  - assets/Declaratieformulier.xlsx
Run Code Online (Sandbox Code Playgroud)

您还应该阅读根包的文件

import 'package:flutter/services.dart' show ByteData, rootBundle;

...

ByteData data = await rootBundle.load("assets/Declaratieformulier.xlsx");
List<int> bytes = data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
var decoder = SpreadsheetDecoder.decodeBytes(bytes);
Run Code Online (Sandbox Code Playgroud)