英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论