nextjs当后端使-读取excel文件

目前nextjs有种php的感觉,现在的需求是读取excel文件,入数据库,拆分出读取excel的代码如下:

javascript 复制代码
import { NextRequest } from "next/server";
import { join } from "path";
import { readFile } from "fs/promises";
import * as XLSX from "xlsx";

export async function POST(req: NextRequest) {
  const uploadDir = join(process.cwd(), "public", "/assets");
  const filepath = `${uploadDir}/wp_postmeta.xlsx`;

  try {
    const fileContent = await readFile(filepath);
    const workbook = XLSX.read(fileContent, { type: "buffer" });
    const sheetName = workbook.SheetNames[0]; // Assuming there's only one sheet
    const sheetData = XLSX.utils.sheet_to_json(workbook.Sheets[sheetName]);
    console.log(sheetData);

    return new Response(JSON.stringify({ message: "文件处理成功" }), {
      status: 200,
    });
  } catch (error) {
    console.error("Error reading or processing XLSX file:", error);
    return new Response(JSON.stringify({ error: "文件处理错误" }), {
      status: 500,
    });
  }
}
相关推荐
哆啦A梦15888 小时前
商城后台管理系统 03 Vue项目-实现表格导出EXCEL表格
前端·vue.js·excel
xingzhemengyou18 小时前
python pandas操作excel
python·excel·pandas
小小心LOVE9 小时前
Vue3 安装和使用 vue-office来实现 Word、Excel 和 PDF 文件的预览
vue.js·word·excel
cyhysr9 小时前
oracle的model子句让sql像excel一样灵活2
sql·oracle·excel
oh,huoyuyan1 天前
【界面案例】火语言RPA读取Excel文件,循环写入界面表格
excel·rpa
2501_907136821 天前
Excel数据根据标题行自动匹配合并到指定模板文件
excel·软件需求
分***81 天前
批量识别身份证并导出excel工具分享,身份证识别工具离线识别 + 字段精准优化,Win10/11 直接用
excel·身份证识别
runepic1 天前
Python 批量合并多个 Excel 数据(自动补 0 + 生成明细)
java·python·excel
hellotutu1 天前
Java 读取 Excel 文件
java·开发语言·excel
yivifu1 天前
Excel中Lookup函数实现临界点归入下一个等级的方法
java·前端·excel