Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const router = express.Router()
- const path = require('path')
- .resolve(__dirname)
- .replace(appRoot, '')
- const XLSX = require('xlsx')
- router.get(path + '/', (req, res) => {
- const location = '/uploads/'
- // อ่านไฟล์ต้นฉบับ
- const wb = XLSX.readFile(`${appRoot}${location}/gsb.xlsx`)
- // เลือก Sheet ที่ 1
- const sheet = wb.SheetNames[0]
- // เปิด Sheet นั้น
- const ws = wb.Sheets[sheet]
- // เพิ่มข้อมูลใหม่ลงไปใน sheet นั้น
- // sheet_add_aoa ช่วยในการแปลง Array ไปต่อไว้ใน Sheet ตำแหน่งที่ A4
- XLSX.utils.sheet_add_aoa(ws, [[1, 2], [2, 3], [3, 4]], { origin: 'A4' })
- // ตั้งชื่อไฟล์ที่จะบันทึก
- const name = Date.now() + '.xlsx'
- const path = 'uploads/exports/' + name
- // บันทึกไฟล์ Excel
- try {
- XLSX.writeFile(wb, path)
- } catch (error) {}
- // Download ไฟล์
- const file = appRoot + '/' + path
- res.download(file)
- })
- module.exports = router
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement