【问题标题】:Export mysql (sequelize) table data to json by nodejsnodejs将mysql(sequelize)表数据导出为json
【发布时间】:2018-06-20 17:51:59
【问题描述】:

我有 json 数据,我想将其导出为 data.xlsx。例如

我有一个表有一个导出按钮,如果用户点击那个导出按钮,这个ng-click功能就会起作用。

controllers.js:

$scope.exportDataXlsx = function () {
        var json = $scope.contacts;
        console.log(json);
        $http.post('/api/exportcontactsxlsx', json).then(function (response) {
            $state.reload();
            toastr.success("exported successfully!");
        });
    };

我的api代码:

exports.exportContactsXlsx = function (req, res) {
    var data = req.body;
    var xls = json2xls(data);
    fs.writeFileSync('data.xlsx', xls, 'binary');
}

我正在使用名为 jsonexport 的 npm 包。

如果我点击导出,文件将下载到我的项目中。

但我需要输出,当用户单击导出按钮时,“data.xlsx”文件应下载到 chrome 左角,并在用户默认下载目录中。

【问题讨论】:

    标签: javascript mysql angularjs json node.js


    【解决方案1】:

    将文件保存到服务器后。您需要发送文件的名称,以便您可以从浏览器访问它:

    $scope.exportDataXlsx = function () {
        var json = $scope.contacts;
        console.log(json);
        $http.post('/api/exportcontactsxlsx', json).then(function (response) {
            downloadFile(response.fileName) // make sure response.fileName has the file name
            $state.reload();
            toastr.success("exported successfully!");
        });
    };
    function downloadFile(name) {
        var link = document.createElement('a');
        link.download = name;
        link.href = '/files/' + name;
        link.click();
    }
    

    服务器:

    // in app.js, to serve your files as static files
    app.use("/files", express.static(path.join(__dirname, 'files')));
    
    // send the the name of your file to the client
    exports.exportContactsXlsx = function (req, res) {
        var data = req.body;
        var xls = json2xls(data);
        fs.writeFileSync(path.join(__dirname,'../files/data.xlsx'), xls, 'binary');
        //changed the directory
        res.json({ fileName: 'data.xlsx' });
    }
    

    【讨论】:

    • data.xslx 已下载,但当我打开它时显示的是 UI 页面,而不是表格数据。
    • @MohamedSameer 确保文件保存在files 目录中
    • 在文件目录中我有data.xlsx,如果我打开这个,表格数据看起来很好,但是在chrome中下载的文件,当我打开它时不显示表格数据
    • @MohamedSameer 是files 目录,与node_modules 处于同一级别?
    • 路由目录中没有我有 files 目录
    【解决方案2】:

    您必须在 res 标头中设置参数。

    您可以尝试以下方法:

    var fields = ['firstName', 'email'];
                    var csv = json2csv({ data: resp, fields: fields });
                    res.set('Cache-Control', 'max-age=0, no-cache, must-revalidate, proxy-revalidate');
                    res.set('Content-Type','application/force-download');
                    res.set('Content-Type','application/octet-stream');
                    res.set('Content-Type','application/download');
                    res.set('Content-Disposition','attachment;filename=userList.csv');
                    res.set('Content-Transfer-Encoding','binary');
                    res.send(csv); 
    

    所以当你在浏览器中点击 API 时,它会询问 SaveFile 选项,如果用户点击 OK,它将被下载到 chrome 的默认下载目录。

    【讨论】:

    • 你能具体点吗?传入的文件是 xlsx 格式吗?我无法理解你的代码。
    猜你喜欢
    • 1970-01-01
    • 2019-12-14
    • 2011-06-29
    • 1970-01-01
    • 2020-10-06
    • 2020-12-17
    • 2019-05-08
    • 2019-11-30
    • 2011-01-30
    相关资源
    最近更新 更多