HarmonyOS excel文件如何打开

自己服务器的excel表格文档,有什么方式可以预览,或者下载下来后如何打开本地的excel文件?

HarmonyOS
3天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

目前暂不支持excel文件的预览,可以通过downloadFile下载excel文件后通过应用文件分享的方式通过其他有应用打开excel文件,具体使用还请参考文档:

downloadFile:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-request-V5#requestdownloadfile9

应用文件分享:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/share-app-file-V5

文件预览支持的文件类型:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/preview-arkts-V5

downloadSave() {
  let context = getContext(this) as common.UIAbilityContext;
  let filePath = context.filesDir + "/test.xlsx";
  let url = "excel文件的url"
  try {
    request.downloadFile(context, { url: url, filePath: filePath }).then((data: request.DownloadTask) => {
      let downloadTask: request.DownloadTask = data;
      downloadTask.on('complete', () => {
        console.info('download complete');
        let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
        let arrayBuffer = new ArrayBuffer(4096000);
        let readLen = fs.readSync(file.fd, arrayBuffer);
        let buf = buffer.from(arrayBuffer, 0, readLen);
        console.info(`content of File: ${buf.toString()}`);
        let writeLen = fs.writeSync(file.fd, arrayBuffer);
        fs.closeSync(file);
        const documentSaveOptions = new picker.DocumentSaveOptions(); // 创建文件管理器选项实例
        documentSaveOptions.newFileNames = ["1.xlsx"]; // 保存文件名(可选)
        documentSaveOptions.fileSuffixChoices = ['xlsx']; // 保存文件类型(可选)
        let uris: Array<string> = [];
        const documentViewPicker = new picker.DocumentViewPicker(); // 创建文件选择器实例
        documentViewPicker.save(documentSaveOptions).then((documentSaveResult: Array<string>) => {
          uris = documentSaveResult;
          console.info('documentViewPicker.save to file succeed and uris are:' + uris);
          let uri = uris[0];
          let file2 = fs.openSync(uri, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
          let writeLen2 = fs.writeSync(file2.fd, arrayBuffer);
          fs.closeSync(file2);

        }).catch((err: BusinessError) => {
          console.error(`Invoke documentViewPicker.save failed, code is ${err.code}, message is ${err.message}`);
        })
      })
      let failCallback = () => {
        console.info('Download task fail.');
      };
      downloadTask.on('fail', failCallback);
    }).catch((err: BusinessError) => {
      console.error(`downLoadFileTask failed, code is ${err.code}, message is ${err.message}`);
    });
  } catch (error) {
    console.error(`downLoadFileTask failed, code is ${error.code}, message is ${error.message}`);
  }
}
分享
微博
QQ
微信
回复
3天前
相关问题
HarmonyOS print组件 支持 excel 打印吗
67浏览 • 1回复 待解决
如何打开指定文件夹,选择文件返回
8751浏览 • 1回复 待解决
HarmonyOS 怎么打开PDF文件
218浏览 • 1回复 待解决
HarmonyOS 使用filePreview打开文件问题
35浏览 • 1回复 待解决
如何打开文件管理器有知道的吗?
2251浏览 • 1回复 待解决