xlsx包:将excel文件写入自定义路径
我正在尝试使用 xlsx
包编写一组对象以执行 Excel 操作。
我想将文件写入路径而不是当前目录
const fileName ='ouptputs/test.xlsx'
const workSheet = XLSX.utils.json_to_sheet(data);
const wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, workSheet,fileName);
const s = XLSX.writeFile(wb, fileName,{
type:"file"
});
但是执行此命令时出现错误
工作表名称不能包含:\ / ? *
那么我如何指定路径而不是将文件生成到
我在他们的文档中搜索的当前目录 https ://github.com/SheetJS/sheetjs#writing-options 但无法找到有关自定义路径的文档
I am trying to write an array of objects to excel using xlsx
package.
I want to write the file to a path instead of current directory
const fileName ='ouptputs/test.xlsx'
const workSheet = XLSX.utils.json_to_sheet(data);
const wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, workSheet,fileName);
const s = XLSX.writeFile(wb, fileName,{
type:"file"
});
But executing this i m getting the error
Sheet name cannot contain : \ / ? *
So how do i specify the path instead of generating the file to current dir
I searched in their doc https://github.com/SheetJS/sheetjs#writing-options but couldn't able to find the documentation about the custom path
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是,当您将字符串作为 fileName 传递给 writeFile 函数时,它会尝试在当前目录中查找具有您提供的名称的文件,但它包含“/”,该文件不能出现在文件名中,因此会导致错误。
所以解决方案是使用“path”库创建路径,请参阅下面的代码示例
希望它会对您或其他人有所帮助。谢谢!
快乐编码:-)
The problem is when you pass the string as fileName to the writeFile function it tries to find the file in the current directory with the name you provided but it contains "/" which can not be present in the file name because of which it causing the error.
So the solution is to create the path by using the "path" library, See below code for example
Hope it will help you or somebody else. Thanks!
Happy Coding :-)