Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. const router = express.Router()
  2. const path = require('path')
  3. .resolve(__dirname)
  4. .replace(appRoot, '')
  5. const XLSX = require('xlsx')
  6.  
  7. router.get(path + '/', (req, res) => {
  8. const location = '/uploads/'
  9.  
  10. // อ่านไฟล์ต้นฉบับ
  11. const wb = XLSX.readFile(`${appRoot}${location}/gsb.xlsx`)
  12.  
  13. // เลือก Sheet ที่ 1
  14. const sheet = wb.SheetNames[0]
  15.  
  16. // เปิด Sheet นั้น
  17. const ws = wb.Sheets[sheet]
  18.  
  19. // เพิ่มข้อมูลใหม่ลงไปใน sheet นั้น
  20. // sheet_add_aoa ช่วยในการแปลง Array ไปต่อไว้ใน Sheet ตำแหน่งที่ A4
  21. XLSX.utils.sheet_add_aoa(ws, [[1, 2], [2, 3], [3, 4]], { origin: 'A4' })
  22.  
  23. // ตั้งชื่อไฟล์ที่จะบันทึก
  24. const name = Date.now() + '.xlsx'
  25. const path = 'uploads/exports/' + name
  26.  
  27. // บันทึกไฟล์ Excel
  28. try {
  29. XLSX.writeFile(wb, path)
  30. } catch (error) {}
  31.  
  32. // Download ไฟล์
  33. const file = appRoot + '/' + path
  34. res.download(file)
  35. })
  36.  
  37. module.exports = router
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement