xlsx包:将excel文件写入自定义路径

发布于 2025-01-13 22:35:19 字数 640 浏览 1 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不知所踪 2025-01-20 22:35:19

问题是,当您将字符串作为 fileName 传递给 writeFile 函数时,它会尝试在当前目录中查找具有您提供的名称的文件,但它包含“/”,该文件不能出现在文件名中,因此会导致错误。

所以解决方案是使用“path”库创建路径,请参阅下面的代码示例

const path = require('path');

// You may need to use relative path in join function depending upon the working file location
const filePath = path.join(__dirname, 'ouptputs/test.xlsx'); // __dirname -> returns the current path of the working file
const workSheet = XLSX.utils.json_to_sheet(data);
const wb = XLSX.utils.book_new();
                
XLSX.utils.book_append_sheet(wb, workSheet, 'Sheet1'); // Sheet1 is the name of sheet that is created inside workbook
const s = XLSX.writeFile(wb, filePath, {
        bookType: 'xlsx',
        type: 'file'
});

希望它会对您或其他人有所帮助。谢谢!

快乐编码:-)

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

const path = require('path');

// You may need to use relative path in join function depending upon the working file location
const filePath = path.join(__dirname, 'ouptputs/test.xlsx'); // __dirname -> returns the current path of the working file
const workSheet = XLSX.utils.json_to_sheet(data);
const wb = XLSX.utils.book_new();
                
XLSX.utils.book_append_sheet(wb, workSheet, 'Sheet1'); // Sheet1 is the name of sheet that is created inside workbook
const s = XLSX.writeFile(wb, filePath, {
        bookType: 'xlsx',
        type: 'file'
});

Hope it will help you or somebody else. Thanks!

Happy Coding :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文