将数据导出到 React Native 中的 Excel(在网络上)

问题描述 投票:0回答:1

我在我的项目中使用react-native-fs & xlsx。

在网络浏览器(chorom、microsoft Edge、...)中打开应用程序时返回无法读取未定义的属性(读取“RNFSFileTypeRegular”)错误。

谢谢你带领我。

import React from "react";
import { } from "react-native";
import xlsx from 'xlsx';
var RNFS = require('react-native-fs');
export const exportDataToExcel = (fileName, data, headerTitle) => {
    let wb = xlsx.utils.book_new();
    let ws = xlsx.utils.json_to_sheet(data, { header: [headerTitle] });
    xlsx.utils.book_append_sheet(wb, ws, fileName)
    const wbout = xslx.write(wb, { type: 'binary', bookType: "xlsx" });
    RNFS.writeFile(RNFS.ExternalStorageDirectoryPath + `/${formName}.xlsx`, wbout, 'ascii').then((r) => { console.log(`create xlsx is success`) })
        .catch((e) => { console.error(`create xlsx is failed`) });

}
react-native xlsx react-native-fs
1个回答
0
投票

找到最佳答案:

import { Component } from "react";
import * as XLSX from "xlsx";
import * as FileSaver from "file-saver";
export function exportDataToExcel(fileName, headerTitle, data) {
    try {
        const fileType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset = UTF-8";
        const fileExtension = ".xlsx";
        var ws = XLSX.utils.json_to_sheet(data);
        var wb = { Sheets: { data: ws }, SheetNames: ["data"] };
        var excelBuffer = XLSX.write(wb, { bookType: 'xlsx', type: 'array' });
        var excelData = new Blob([excelBuffer], { type: fileType });
        FileSaver.saveAs(excelData, fileName + fileExtension);
    }
    catch (error) {
        console.error(`get error ${error} in ExportDataToExcel exportDataToExcel`);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.