因此,我正在生成一个发票,并使用react包将我的graphql数据隐藏到一个csv文件中。问题是,我不希望用户在本地选择要下载到的位置(开箱即用)。它需要自动保存在服务器中的硬编码目录中。有人吗?
<CSVLink onClick={generateInvoice} className="mt-1 inline-block align-middle w-full inline-flex justify-center py-2 border border-transparent shadow-sm text-sm font-medium rounded-md text-white bg-blue-400 hover:bg-blue-500 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500" filename={csvfilename} data={csvinvoiceForDownload} headers={headers}>
Create Invoice
</CSVLink>发布于 2022-05-08 18:24:04
你不需要反应-csv,它是用来渲染数据的.
您可以使用例如PapaParse https://www.papaparse.com/docs#json-to-csv将您的graphQL对象转换为csv,您可以将其与fetch或axios一起发送到您的服务器。
// With implicit header row
// (keys of first object populate header row)
var csv = Papa.unparse([
{
"Column 1": "foo",
"Column 2": "bar"
},
{
"Column 1": "abc",
"Column 2": "def"
}
]);此外,您也可以使用js:
const items = json3.items
const replacer = (key, value) => value === null ? '' : value // specify how you want to handle null values here
const header = Object.keys(items[0])
const csv = [
header.join(','), // header row first
...items.map(row => header.map(fieldName => JSON.stringify(row[fieldName], replacer)).join(','))
].join('\r\n')
console.log(csv)(示例在SO:How to convert JSON to CSV format and store in a variable上找到)
你可以找到更多的选择。
https://stackoverflow.com/questions/72062775
复制相似问题