[英]How to read data from docx file in Flutter
我有一个本地资产、一个 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
文件添加到assets
文件夹中,与lib
文件夹处于同一级别。
然后像这样将您的assets
文件夹添加到pubspec.yaml
assets:
- assets/
在你的main.dart
之后添加这个 function 即readFileFromAssets()
在initState()
到它会在应用程序启动时开始从.docx
加载数据。
并导入这个 package 太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");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.