add exceljs for xlsx file gen

This commit is contained in:
Ahmed 2024-09-12 15:09:47 +03:00
parent 73afeee79b
commit 68a19cafd7
1 changed files with 109 additions and 30 deletions

87
main.js
View File

@ -6,7 +6,8 @@ const config = require("./config/db.config.js");
const session = require('express-session'); const session = require('express-session');
const bcrypt = require('bcrypt'); const bcrypt = require('bcrypt');
const signupRouter = require('./signup'); // Adjust path as necessary const signupRouter = require('./signup'); // Adjust path as necessary
const ExcelJS = require('exceljs');
const cors = require('cors');
const PORT = process.env.PORT || 8080; // Default port is 5000 const PORT = process.env.PORT || 8080; // Default port is 5000
@ -33,11 +34,13 @@ const pool = mysql.createPool({
}); });
const app = express(); const app = express();
app.use(cors()); // Enable CORS for all origins
// app.use(cors({
// origin: '[ http://192.168.1.6:8081 , http://localhost:8081 ]'
// }));
app.set('view engine', 'ejs'); app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views')); app.set('views', path.join(__dirname, 'views'));
@ -54,6 +57,19 @@ app.use(express.static(path.join(__dirname, 'static')));
app.use(signupRouter); app.use(signupRouter);
app.get('/x', function (request, response) {
if (request.session.loggedin) {
// Output username
// response.send('Welcome back, ' + request.session.username + '!');
console.log(request.session.username, 'open main page');
// Render home page with username
response.render('home', { username: request.session.username });
} else {
// response.send('Please login to view this page!');
response.redirect('/login');
}
});
app.get('/login', function (request, response) { app.get('/login', function (request, response) {
/// Render login template with any message from query parameters /// Render login template with any message from query parameters
console.log('Message:', request.query.message); // Log the message console.log('Message:', request.query.message); // Log the message
@ -64,6 +80,7 @@ app.get('/login', function (request, response) {
}); });
app.post('/auth', async function (request, response) { app.post('/auth', async function (request, response) {
// Capture the input fields // Capture the input fields
const { login, password } = request.body; const { login, password } = request.body;
@ -151,6 +168,7 @@ app.get('/logout', function (request, response) {
}); });
}); });
// Serve the signup form // Serve the signup form
app.get('/signup', function (request, response) { app.get('/signup', function (request, response) {
@ -182,6 +200,67 @@ if (!config.enableSignup) {
}); });
app.get('/download', async (req, res) => {
const filename = req.query.filename || 'default.xlsx'; // Get filename from query parameter
console.log('got the filename', filename);
const workbook = new ExcelJS.Workbook();
const worksheet = workbook.addWorksheet('Sheet1');
// Set column widths
worksheet.columns = [
{ width: 9 },
{ width: 16 },
{ width: 42 },
{ width: 94 }
];
// Add header row
const headerRow = worksheet.addRow(['Модель', 'Сер.номер', 'Заявленная неисправность', 'Произведенная работа']);
// Define border style
const thinBlackBorder = {
top: { style: 'thin', color: { argb: 'FF000000' } },
left: { style: 'thin', color: { argb: 'FF000000' } },
bottom: { style: 'thin', color: { argb: 'FF000000' } },
right: { style: 'thin', color: { argb: 'FF000000' } }
};
// Style the header row
headerRow.eachCell({ includeEmpty: true }, (cell, colNumber) => {
cell.fill = {
type: 'pattern',
pattern: 'solid',
fgColor: { argb: '92D050' }
};
cell.font = {
name: 'Calibri',
size: 11,
color: { argb: '000000' },
bold: false
};
cell.alignment = { vertical: 'middle', horizontal: 'center' };
cell.border = thinBlackBorder;
});
// Style the rest of the cells with border
worksheet.eachRow({ includeEmpty: true }, (row) => {
row.eachCell({ includeEmpty: true }, (cell) => {
if (!cell.border) { // Apply border only if not already defined
cell.border = thinBlackBorder;
}
});
});
// Generate buffer from workbook
const buffer = await workbook.xlsx.writeBuffer();
// Set headers and send file
res.setHeader('Content-Disposition', `attachment; filename*=UTF-8''${encodeURIComponent(filename)}.xlsx`);
res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
res.send(buffer);
});
app.listen(PORT, () => { app.listen(PORT, () => {
console.info(`Listening on http://0.0.0.0:${PORT}/`); console.info(`Listening on http://0.0.0.0:${PORT}/`);
}); });