-
Notifications
You must be signed in to change notification settings - Fork 26
Description
// server.js
const express = require('express');
const multer = require('multer');
const xlsx = require('xlsx');
const mysql = require('mysql2/promise');
const app = express();
const upload = multer({ dest: 'uploads/' });
// اتصال بقاعدة البيانات
const db = mysql.createPool({
host: 'localhost',
user: 'root',
password: 'password',
database: 'accounting_db'
});
// رفع ملف إكسل وتحويله لجدول
app.post('/upload', upload.single('file'), async (req, res) => {
try {
const wb = xlsx.readFile(req.file.path);
const data = xlsx.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]);
for (let r of data) {
await db.query(
"INSERT INTO invoices (invoiceNumber,date,amount,customer) VALUES (?,?,?,?)",
[r.InvoiceNumber, r.Date, r.Amount, r.Customer]
);
}
res.json({ message: "تم إدخال البيانات", count: data.length });
} catch (e) {
res.status(500).json({ error: e.message });
}
});
app.listen(3000, () => console.log("http://localhost:3000 جاهز"));