【问题标题】:React Native: How to create excel file from code?React Native:如何从代码创建 excel 文件?
【发布时间】:2018-12-20 21:22:25
【问题描述】:

有没有办法在 React native 中从 js 代码创建一个 excel 文件?可以分享一下这个库,包括如何使用的例子吗?

我需要导出并下载一些数据到excel文件并下载到手机。

谢谢!

【问题讨论】:

  • 对图书馆的请求不在此处讨论。

标签: android excel react-native


【解决方案1】:

如果使用 Expo,以下代码应该适合您。它创建一个工作表,然后创建一个共享对话框,供用户在他们喜欢的任何应用程序中打开它,例如电子邮件、Office 等:

import XLSX from 'xlsx';
import * as FileSystem from 'expo-file-system';
import * as Sharing from 'expo-sharing';

var data = [{
    "name": "John",
    "city": "Seattle"
  },
  {
    "name": "Mike",
    "city": "Los Angeles"
  },
  {
    "name": "Zach",
    "city": "New York"
  }
];
var ws = XLSX.utils.json_to_sheet(data);
var wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, "Cities");
const wbout = XLSX.write(wb, {
  type: 'base64',
  bookType: "xlsx"
});
const uri = FileSystem.cacheDirectory + 'cities.xlsx';
console.log(`Writing to ${JSON.stringify(uri)} with text: ${wbout}`);
await FileSystem.writeAsStringAsync(uri, wbout, {
  encoding: FileSystem.EncodingType.Base64
});

await Sharing.shareAsync(uri, {
  mimeType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  dialogTitle: 'MyWater data',
  UTI: 'com.microsoft.excel.xlsx'
});

【讨论】:

    【解决方案2】:

    您可以将 JSON 转换为 excel 文件

    你必须使用两个包装

    1. react-native-fs
    2. XLSX(用于将json转换为excel文件)

    这是一个例子

    如果你想写 Excel 文件

    import { writeFile, readFile } from 'react-native-fs';
    import XLSX from 'xlsx';
    
    var data = [
    {"name":"John", "city": "Seattle"},
    {"name":"Mike", "city": "Los Angeles"},
    {"name":"Zach", "city": "New York"}
    ];
    
     var ws = XLSX.utils.json_to_sheet(data);
    
      var wb = XLSX.utils.book_new();
      XLSX.utils.book_append_sheet(wb,ws,"Prova");
    
      const wbout = XLSX.write(wb, {type:'binary', bookType:"xlsx"});
      var RNFS = require('react-native-fs');
      var file = RNFS.ExternalStorageDirectoryPath + '/test.xlsx';
      writeFile(file, wbout, 'ascii').then((r)=>{/* :) */}).catch((e)=>{/* :( */});
    

    如果你想读取 Excel 文件

    import { writeFile, readFile } from 'react-native-fs';
    import XLSX from 'xlsx';
    
      const filePath="/Users/copoo/Downloads/Data.xlsx";
      const excelFile=await RNFS.readFile(filePath,'ascii');
      const workbook = XLSX.read(excelFile, {type:'binary'});
      console.log(workbook,"excelFile")
    
    

    【讨论】:

    • 没有错误,它完成了......但我无法找到写入的文件。这是否适用于嵌套的 JsonObject
    • github.com/itinance/react-native-fs/issues/540 你可以看到 RNFS.ExternalStorageDirectoryPath 它实际上显示了存储根目录。您应该在 catch 块中进行控制台并调试它现在不写入文件的原因。当我发布这个答案时它正在工作
    【解决方案3】:

    我会使用 JavaScript 原生的 JSON,可以是 imported directly into Excel

    如果你还不知道怎么做,这里有一篇解释如何从 JS 对象创建 JSON 文件的帖子:

    write/add data in JSON file using node.js

    【讨论】:

      猜你喜欢
      • 2021-11-08
      • 1970-01-01
      • 2015-07-10
      • 1970-01-01
      • 1970-01-01
      • 2017-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多