如何在使用Node.js上传Excel表格到MySQL时忽略第一行?

huangapple go评论79阅读模式
英文:

how to ignore 1st row while uploading excel sheet to MySQL using nodejs?

问题

app.post("/upload", (req, res) => {

var fileName = req.file;

console.log(fileName);

readXlsxFile(req.file.path).then((rows) => {

console.log(rows);

var con = mysql2.createConnection({
host: "localhost",
user: "root",
password: "root",
database: "personal_db"
});

con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
var sql = "INSERT IGNORE INTO information (id, name) VALUES ?";
var values = rows;
// var finValue = values.trim();
console.log(values);
con.query(sql, [values], function (err, result) {

  console.log("Number of records inserted: " + result.affectedRows);
});

});
});

how to ignore 1st row of the excel while uploading excel sheet to MySQL using a query, its works by IGNORE but also inserts 1st row which consists of table headings

英文:
app.post("/upload", (req, res)=>{

var fileName = req.file;

console.log(fileName);

readXlsxFile(req.file.path).then((rows) => {

  console.log(rows);

  var con = mysql2.createConnection({
  host: "localhost",
  user: "root",
  password: "root",
  database: "personal_db"
});
   
 con.connect(function(err) {
      if (err) throw err;
      console.log("Connected!");
      var sql = "INSERT IGNORE INTO information (id, name) VALUES ?";
      var values = rows;
      // var finValue = values.trim();
      console.log(values);
      con.query(sql, [values], function (err, result) {
    
        console.log("Number of records inserted: " + result.affectedRows);
      });
    });
    });

how to ignore 1st row of the excel while uploading excel sheet to MySQL using a query, its works by IGNORE but also inserts 1st row which consists of table headings

答案1

得分: 1

Your rows object is an array. Before you use it you can remove its first element with shift

rows.shift()
const values = rows

or, slightly faster, with slice

const values = rows.slice(1)

Pro tip Try to avoid var unless you're developing browser code for obsolete browsers.

英文:

Your rows object is an array. Before you use it you can remove its first element with shift

rows.shift()
const values = rows

or, slightly faster, with slice

const values = rows.slice(1)

Pro tip Try to avoid var unless you're developing browser code for obsolete browsers.

huangapple
  • 本文由 发表于 2020年1月6日 21:27:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612984.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定