【问题标题】:How to read data from docx file in Flutter如何在 Flutter 中从 docx 文件中读取数据
【发布时间】:2021-05-25 07:13:58
【问题描述】:

我有一个本地资产、一个 Docx 文件(或者在某些情况下只有 UInt8List 字节),如何从该文件(或字节)中读取数据?

这个数据只包含字符串,我怎么读?我可以阅读 .txt 文件,但没有 docx,为什么? 实际上里面没有特殊字符,所以问题不在于编码。 (查看下面的内容,HtmlCode)

我试过了: 这是字节

final data = await rootBundle.load('template.docx');
    final bytes = data.buffer.asUint8List();

和 这是(将是)字符串

String fileText = await rootBundle.loadString('template.docx');

错误:

Error: FormatException: Unexpected extension byte (at offset 16)
    at Object.throw_ [as throw] (http://localhost:54436/dart_sdk.js:5366:11)
    at convert._Utf8Decoder.new.convertGeneral (http://localhost:54436/dart_sdk.js:49632:19)
    at convert._Utf8Decoder.new.convertSingle (http://localhost:54436/dart_sdk.js:49604:19)
    at Utf8Decoder.convert (http://localhost:54436/dart_sdk.js:49463:67)
    at Utf8Codec.decode (http://localhost:54436/dart_sdk.js:49172:22)
    at asset_bundle.PlatformAssetBundle.new.loadString (http://localhost:54436/packages/flutter/src/services/system_channels.dart.lib.js:2289:31)
    at loadString.next (<anonymous>)
    at http://localhost:54436/dart_sdk.js:39272:33
    at _RootZone.runUnary (http://localhost:54436/dart_sdk.js:39129:58)
    at _FutureListener.thenAwait.handleValue (http://localhost:54436/dart_sdk.js:34091:29)
    at handleValueCallback (http://localhost:54436/dart_sdk.js:34651:49)
    at Function._propagateToListeners (http://localhost:54436/dart_sdk.js:34689:17)
    at _Future.new.[_completeWithValue] (http://localhost:54436/dart_sdk.js:34531:23)
    at async._AsyncCallbackEntry.new.callback (http://localhost:54436/dart_sdk.js:34554:35)
    at Object._microtaskLoop (http://localhost:54436/dart_sdk.js:39416:13)
    at _startMicrotaskLoop (http://localhost:54436/dart_sdk.js:39422:13)
    at http://localhost:54436/dart_sdk.js:34905:9

字节完全没有问题,但是字符串会抛出这个错误。 我该如何解决这个问题?

完整代码:

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Html(),
    );
  }
}

class Html extends StatefulWidget {
  LoadHtml createState() => LoadHtml();
}

class LoadHtml extends State<Html> {

  void initState() {
    readFilesFromAssets();
    super.initState();
  }

// Read .docx function not only .docx mostly any type .json .txt etc
  readFilesFromAssets() async {
    print("read now");
    String assetContent = await rootBundle.loadString('assets/template.docx');
    print("assetContent : $assetContent");
  }

//this is exaclty what would be in the template.doc
  var HtmlCode = r"""
<HTML>
<BODY LANG="ru-RU" DIR="LTR">
<P CLASS="western" ALIGN=CENTER STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif"><FONT SIZE=3><SPAN LANG="en-US">Example
Document</SPAN></FONT></FONT></P>
<P CLASS="western" ALIGN=CENTER STYLE="margin-bottom: 0in"> <FONT FACE="Times New Roman, serif"><FONT SIZE=5><SPAN LANG="en-US"><B>Document
name</B></SPAN></FONT></FONT></P>
<P LANG="en-US" CLASS="western" ALIGN=CENTER STYLE="margin-bottom: 0in">
<BR>
</P>
<P CLASS="western" ALIGN=CENTER STYLE="margin-bottom: 0in"><FONT FACE="Times New Roman, serif"><FONT SIZE=3><SPAN LANG="en-US">Szerzodes
tobbi resze:</SPAN></FONT></FONT></P>
</BODY>
</HTML>
    """;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('From docx')),
      body: WebViewX(
        initialContent: HtmlCode, //I should read this from the .docx
        initialSourceType: SourceType.HTML,
      ),
    );
  }
}

【问题讨论】:

  • 您是否在资产中添加了您的.docx 文件并在pubspec.yaml 中添加了一个文件路径,例如assets: – assets/myfile.docx
  • 是的,我只是尝试按 1:1 复制您的代码以查看它是否有效,但不,请在评论下方查看我的答案 我不明白为什么,使用 .txt 可以像一个魅力,但不是 docx。
  • WebViewX 是否在 webview_flutter 包内?
  • 我不知道,但你的代码运行良好,我已经复制了它并且运行顺利。
  • 好吧,就这样吧。删除您的 .docx 文件并创建一个新文件。而不是通过打开word来添加内容意味着不要打开MS word来添加内容。使用记事本或崇高文本在文件中添加内容并复制粘贴您的 HTML 代码即可。像这样试试。但它的加载数据但没有显示在WebViewX

标签: flutter file dart docx readfile


【解决方案1】:

首先将.docx 文件添加到与lib 文件夹相同级别的assets 文件夹中。

然后像这样将assets 文件夹添加到pubspec.yaml

  assets:
    - assets/

在您的main.dart 中添加此函数,即initState() 中的readFileFromAssets() 之后,它将在应用启动时开始从.docx 加载数据。

并且也导入这个包import 'package:flutter/services.dart';

void initState() {
  readFilesFromAssets();
  super.initState();
}

// Read .docx function not only .docx mostly any type .json .txt etc
readFilesFromAssets() async {
  String assetContent = await rootBundle.loadString('assets/mytext.docx');
  print("assetContent : $assetContent");
}

这是输出:

【讨论】:

  • 感谢您的详细解答。是的,我完全一样。在我的 docx 里面有 assets 文件夹,pubspec.yaml 也在那里,但仍然存在:下面是我的错误:
  • ``` [ERROR:flutter/lib/ui/ui_dart_state.cc(213)] 未处理的异常:FormatException:意外的扩展字节(偏移量 16)#0 _Utf8Decoder.convertSingle (dart:convert- patch/convert_patch.dart:1788:7) #1 Utf8Decoder.convert (dart:convert/utf.dart:318:42) E/flutter (17417): #2 Utf8Codec.decode (dart:convert/utf.dart:63 :20) E/flutter (17417): #3 AssetBundle.loadString (package:flutter/src/services/asset_bundle.dart:74:19) E/flutter (17417): E/flutter (17417): #4 LoadHtml.readFilesFromAssets (package:docxflutter/main.dart:373:27) ``
  • 更新了问题,感谢您指出这一点
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-02
  • 1970-01-01
  • 1970-01-01
  • 2023-01-23
  • 2020-04-19
  • 1970-01-01
相关资源
最近更新 更多