Flutter Flatter web-如何将excel文件保存到设备

Flutter Flatter web-如何将excel文件保存到设备,flutter,dart,Flutter,Dart,我在将数据保存到设备时遇到问题。 我有base64字符串,需要将其作为excel文件保存到设备中。 一切都发生在网络上。 到目前为止,我发现的都是不起作用的移动解决方案 你能帮忙吗:) 这是我目前拥有的代码。它保存了文件,但是我无法在以后打开它,所以途中一定有问题。由于这个解决方案写入了文件,我猜我是在错误地转换某些内容 //base64StringFile is the file I'm getting from the web. It's a String Uint8List

我在将数据保存到设备时遇到问题。 我有base64字符串,需要将其作为excel文件保存到设备中。 一切都发生在网络上。 到目前为止,我发现的都是不起作用的移动解决方案

你能帮忙吗:)

这是我目前拥有的代码。它保存了文件,但是我无法在以后打开它,所以途中一定有问题。由于这个解决方案写入了文件,我猜我是在错误地转换某些内容

    //base64StringFile is the file I'm getting from the web. It's a String
    Uint8List bytes = base64.decode(base64StringFile);
    File file = File.fromRawPath(bytes);

    final blob = html.Blob([file]);
    final url = html.Url.createObjectUrlFromBlob(blob);
    final anchor = html.document.createElement('a') as html.AnchorElement
      ..href = url
      ..style.display = 'none'
      ..download = 'myFileName';
    html.document.body.children.add(anchor);

    anchor.click();

    html.document.body.children.remove(anchor);
    html.Url.revokeObjectUrl(url);

File.fromRawPath
将给定的字节解释为与文件系统相关的路径信息-在这里不相关

相反,像这样直接用字节创建blob,不要使用
文件
对象

请注意,字节列表包含在另一个列表中

此外,在执行此操作时,还应指定

final blob=html.blob([bytes],'application/vnd.openxmlformats officedocument.spreadsheetml.sheet');

虽然这段代码可以解决这个问题,但如何以及为什么解决这个问题将真正有助于提高您的帖子质量,并可能导致更多的投票。请记住,你是在将来回答读者的问题,而不仅仅是现在提问的人。请编辑您的答案,添加解释,并说明适用的限制和假设。
   //Method 2:
  import 'package:universal_html/html.dart' as html1;
   //Method 1: 
  import 'package:url_launcher/url_launcher.dart';

     
    exportExcelForWeb(String base64) {
    //Method 1: 
      // launch("data:application/octet-stream;base64,${base64}");
    
    //Method 2:
    
      final anchor = html1.AnchorElement(href: 'data:application/octet-stream;base64,$base64')..target = 'blank';
      // add the name
      anchor.download = 'Export_file.xlsx';
      // trigger download
      html1.document.body.append(anchor);
      anchor.click();
      anchor.remove();
    }