Read and Write Execel (Generate Excel ) File using ExpressJs, NodeJs, xlsx , Multer , Ejs
Автор: Nikhil Verma
Загружено: 2025-08-22
Просмотров: 6
Описание:
Express js main execel file koi likhi hai to use read in json format sheet_to_json and excel file create kar sakte hain json main likhe format ko excel main use json_to_sheet
pehle wahi install npm i express nodemon ejs xlsx multer
https://expressjs.com/
https://www.npmjs.com/package/xlsx
multer use for upload file or images.
express code likhkar index.js main multer xlsx ejs path import and ejs ka middleware set and
const upload = multer({ dest: 'uploads/' }) // Set the destination for uploaded files uploads folder main.
// Read Excel file and convert it to JSON format using XLSX library.
pehle views folder banakar excel.ejs ka folder banakar usmain html code for upload excel file and upload in json format in upload-excel webpage.
app.get('/', (req, res) = {
res.render('excel')
})
app.post('/upload-excel', upload.single('excelFile'), (req, res) = { // Handle the file upload
const filePath = path.join(__dirname, 'uploads', req.file.filename) // Get the path of the uploaded file
const workbook = XLSX.readFile(filePath) // Read the uploaded Excel file using XLSX library
const worksheet = workbook.Sheets[workbook.SheetNames[0]] // Get the first sheet
const data = XLSX.utils.sheet_to_json(worksheet) // Convert the sheet to JSON format.excel file ka data webpage per json format main dikhega.
res.json({
message: 'Excel file uploaded successfully',
data // Send the JSON data as a response
})
})
// Write Export Excel file : means yahan json main likha data excel file main export hoga.
app.get('/export-excel', (req, res) = { // Handle the export request. export-excel route par jab bhi request aayegi to yeh function chalega.and data main likha matter webpage per automatic excel file main dal dega use save karke open karo and data usmain likha hoga.
const data = [ // Sample data to be exported to Excel
{ Name: 'John Doe', Age: 30, City: 'New York' },
{ Name: 'Jane Smith', Age: 25, City: 'Los Angeles' },
{ Name: 'Sam Wilson', Age: 35, City: 'Chicago' }
]
const worksheet = XLSX.utils.json_to_sheet(data) // Convert JSON data to a worksheet
const workbook = XLSX.utils.book_new() // Create a new workbook
XLSX.utils.book_append_sheet(workbook, worksheet, 'Sheet1') // Add the worksheet to the workbook. sheet1 ki jagah kuch aur name bhi de sakte hain.
const excelBuffer = XLSX.write(workbook, { type: 'buffer', bookType: 'xlsx' }) // Convert the workbook to a buffer
res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') // Set the response headers. (vnd.openxmlformats-officedocument.spreadsheetml.sheet) yeh Excel file ka MIME type hai.
res.setHeader('Content-Disposition', 'attachment; filename=data.xlsx') // Set the response headers. data.xlsx is the name of the exported excel file. xlsx excel file ka extension hai.
res.send(excelBuffer) // Send the Excel file as a response.
})
app.listen(port, () = {
console.log(`Example app listening on port ${port}`)
})
Повторяем попытку...
Доступные форматы для скачивания:
Скачать видео
-
Информация по загрузке: