将HTML数据表单收集到Excel电子表格中通常涉及以下几个步骤:
以下是一个基于Node.js和ExcelJS库的示例代码,展示如何将HTML表单数据收集到Excel电子表格中:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Form to Excel</title>
</head>
<body>
<form action="/submit" method="POST">
<label for="name">Name:</label>
<input type="text" id="name" name="name"><br><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email"><br><br>
<button type="submit">Submit</button>
</form>
</body>
</html>
const express = require('express');
const bodyParser = require('body-parser');
const ExcelJS = require('exceljs');
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
app.post('/submit', async (req, res) => {
const workbook = new ExcelJS.Workbook();
const worksheet = workbook.addWorksheet('Sheet1');
worksheet.columns = [
{ header: 'Name', key: 'name', width: 10 },
{ header: 'Email', key: 'email', width: 20 }
];
worksheet.addRow({ name: req.body.name, email: req.body.email });
const buffer = await workbook.xlsx.writeBuffer();
res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
res.setHeader('Content-Disposition', 'attachment; filename=data.xlsx');
res.send(buffer);
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
action
属性指向正确的服务器端处理路径。exceljs
)。Content-Type
和Content-Disposition
。通过以上步骤和示例代码,你可以实现将HTML数据表单收集到Excel电子表格中,并解决常见的相关问题。
领取专属 10元无门槛券
手把手带您无忧上云