简体   繁体   中英

Load data from Excel to sql server table using node.js

I am trying to load an excel file to the sql server table, but I am getting error. I have shared the code and error for the same. Kindly point out what is wrong here.

Excel file consists of 4 columns Id, Date, Model and plan which I want to insert into mssql table, but unable to do it.

 //CODE
var express = require('express');
var http = require('http');
var oracledb = require('oracledb');
//var dbConfig = require('./dbconfig.js');
var app = express();
var bodyParser = require('body-parser');
var XLSX = require('xlsx');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const Cors = require("cors");
app.use(Cors());


jsonArrayToValueArray = (jsonArray) => {
    let valueArray = new Array();
    for (let index = 0; index < jsonArray.length; index++) {
        let object = jsonArray[index];
        let keys = Object.keys(object);
        valueArray.push([object[keys[0]], object[keys[1]], object[keys[2]], object[keys[3]]]);
        console.log(valueArray) ;  
    }
    return valueArray;
  }

app.get('/', function (req, res) {

    var sql = require("mssql");

    // Configuration object for your database
    var config = {
        user: 'sa',
    password: '******',
    database: '*****',
    server: '*******',
    port: 1433,
    //timezone: 'utc',
    //multipleStatements: true 
    };
    // connect to the database
    sql.connect(config, function (err) {
    var workbook = XLSX.readFile(__dirname + '/uploads/delivery_excel.xlsx');
    var sheet_name_list = workbook.SheetNames;

    var xlData = XLSX.utils.sheet_to_json(workbook.Sheets[sheet_name_list[0]]);
     console.log();
    let valueArray = jsonArrayToValueArray(xlData);

        if (err) console.log(err);
        // create Request object
        var request = new sql.Request();


        // query to the database and get the records
        request.query("INSERT INTO dbo.plan_excel Values(' + _Id + ', ' + _Date + ', ' + _Model + ', ' + _Plan + ')", [valueArray], function (err, recordset) {
       // request.query("INSERT INTO dbo.plan_excel Values(@Id, @Date, @Model, @Plan) ", [valueArray], function (err, recordset) {

            if (err) console.log(err)
            // send records as a response
            res.send(recordset);
            sql.close();
       });
    });
});
var server = app.listen(5050, function () {
    console.log('Server is running..');
});

Error I am getting is as follows, an array of 38 rows(excel data) followed by an error. Please help me with the changes required for this code to work, so that data will be inserted into the sql server. I am using that insert query with reference to few other links, please let me know if it is not the correct way.

I have achieved the same flow earlier, but it was for Mysql database, since I am new to sql server so not understanding much about it.

  [ 28, 43295, 'V45', 200 ],
  [ 29, 43296, 'V45', 200 ],
  [ 30, 43297, 'V45', 200 ],
  [ 31, 43298, 'V45', 200 ],
  [ 32, 43299, 'V45', 200 ],
  [ 33, 43299, 'V30', 213 ],
  [ 34, 43299, 'V26', 220 ],
  [ 35, 43299, 'V22', 200 ],
  [ 36, 43299, 'V34', 233 ],
  [ 37, 42895, 'v35', 123 ],
  [ 38, 42895, 'V32', 122 ] ]
(node:40820) UnhandledPromiseRejectionWarning: RequestError: Conversion failed when converting the varchar value ' + _Id + ' to data type int.
    at handleError (C:\Users\vishruti.pawar\.vscode\mssql_api\node_modules\mssql\lib\tedious.js:566:15)
    at emitOne (events.js:116:13)
    at Connection.emit (events.js:211:7)
    at Parser.tokenStreamParser.on.token (C:\Users\vishruti.pawar\.vscode\mssql_api\node_modules\tedious\lib\connection.js:716:12)
    at emitOne (events.js:116:13)
    at Parser.emit (events.js:211:7)
    at Parser.parser.on.token (C:\Users\vishruti.pawar\.vscode\mssql_api\node_modules\tedious\lib\token\token-stream-parser.js:27:14)
    at emitOne (events.js:116:13)
    at Parser.emit (events.js:211:7)
    at addChunk (C:\Users\vishruti.pawar\.vscode\mssql_api\node_modules\readable-stream\lib\_stream_readable.js:297:12)
(node:40820) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:40820) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate
the Node.js process with a non-zero exit code.

I have shared the alternate code, it is sending records to database, but some random records are being send there, can you please tell what might be the mistake. I am using for loop to add those values to table.

var express = require('express');
var http = require('http');
var oracledb = require('oracledb');
var dbConfig = require('./dbconfig.js');
var app = express();
var bodyParser = require('body-parser');
var XLSX = require('xlsx');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const Cors = require("cors");
app.use(Cors());

jsonArrayToValueArray = (jsonArray) => {
    let valueArrayN = new Array();
    let valueArray = new Array();
    let valueArray1 = new Array();
    let valueArray2 = new Array();
    let valueArray3 = new Array();

    for (let index = 0; index < jsonArray.length; index++) {
        let object = jsonArray[index];
        let keys = Object.keys(object);
        valueArray.push([object[keys[0]]])
        valueArray1.push([object[keys[1]]])
        valueArray2.push( [object[keys[2]]])
        valueArray3.push( [object[keys[3]]])
        //valueArrayN.push([object[keys[0]], object[keys[1]], object[keys[2]], object[keys[3]]]);
        console.log(valueArrayN) ;  
    }
        valueArrayN = [valueArray, valueArray1, valueArray2, valueArray3];
      return valueArrayN;
 }

app.get('/', function (req, res) {
    var sql = require("mssql");
    var config = {
    user: 'sa',
    password: '******',
    database: '*****',
    server: 'localhost',
    port: 1433,
    timezone: 'utc',
    multipleStatements: true 
    };
    // connect to the database
    sql.connect(config, function (err) {
    var workbook = XLSX.readFile(__dirname + '/uploads/delivery_excel.xlsx');
    var sheet_name_list = workbook.SheetNames;

    var xlData = XLSX.utils.sheet_to_json(workbook.Sheets[sheet_name_list[0]]);

     console.log();
    let valueArrayN = jsonArrayToValueArray(xlData);

      var i;
      for(i=0; i < 45; i++)
        {

        if (err) console.log(err);
        // create Request object
        var request = new sql.Request();

        //setTimeout(function(){
        request.input('id', sql.Int, valueArrayN[0][da])
        request.input('Date', sql.Int, valueArrayN[1][da])
        request.input('Model', sql.NChar,valueArrayN[2][da])
        request.query("INSERT INTO dbo.plan_excel Values (@id, @Date, @Model)", function (err, recordset) {
           if (err) console.log(err)
           res.send(recordset);
      });

}  //for loop
});
});
var server = app.listen(5010, function () {
    console.log('Server is running..');
});

For this code I am getting the following error and every time I run different records are being inserted like sometimes 11 records sometimes only 4. what could be the reason for that?

[]
[]
[]
_http_outgoing.js:494
    throw new Error('Can\'t set headers after they are sent.');
    ^

Error: Can't set headers after they are sent.
    at validateHeader (_http_outgoing.js:494:11)
    at ServerResponse.setHeader (_http_outgoing.js:501:3)
    at ServerResponse.header (C:\Users\.vscode\MSSQL\oracle_api_working\node_modules\express\lib\response.js:767:10)
    at ServerResponse.send (C:\Users\.vscode\MSSQL\oracle_api_working\node_modules\express\lib\response.js:170:12)
    at ServerResponse.json (C:\Users\.vscode\MSSQL\oracle_api_working\node_modules\express\lib\response.js:267:15)
    at ServerResponse.send (C:\Users\.vscode\MSSQL\oracle_api_working\node_modules\express\lib\response.js:158:21)
    at C:\Users\.vscode\MSSQL\oracle_api_working\example2.js:95:17
    at _query (C:\Users\.vscode\MSSQL\oracle_api_working\node_modules\mssql\lib\base.js:1347:9)
    at Request.tds.Request.err [as userCallback] (C:\Users\.vscode\MSSQL\oracle_api_working\node_modules\mssql\lib\tedious.js:671:15)
    at Request.callback (C:\Users\.vscode\MSSQL\oracle_api_working\node_modules\tedious\lib\request.js:37:27)
PS C:\Users\.vscode\MSSQL\oracle_api_working>

将您的 request.query 行替换为此。

request.query("INSERT INTO dbo.plan_excel Values ${valueArray.join().split(",").map(x => '(' + x + ')').join()}", function (err, recordset) {
request.query(`INSERT INTO dbo.plan_excel Values(${id}, ${date}, '${model}', ${plan})`, [valueArray], function (err, recordset)

您应该只引用模型,因为它是矩阵上唯一声明为字符串的模型

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM