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,
    });
  }
}
相关推荐
开开心心就好13 小时前
桌面图标乱了怎么办,一键恢复固定位置工具
运维·服务器·windows·pdf·excel·3dsmax·houdini
偷心伊普西隆15 小时前
EXCEL 自动化链接更新工具设计方案
自动化·excel
ai_coder_ai16 小时前
如何在自动化脚本中使用excel文件?
excel·autojs·自动化脚本·冰狐智能辅助·easyclick
前端程序猿i16 小时前
纯JS 导出 Excel 工具
开发语言·javascript·excel
✎ ﹏梦醒͜ღ҉繁华落℘16 小时前
excel操作 ---删除空行
excel
梦幻通灵2 天前
Excel工作表的复制实战【实用指南】
excel
ManageEngineITSM2 天前
功能越来越强,但 IT 使用体验却越来越差
大数据·excel·资产管理·itsm·工单系统
傻啦嘿哟2 天前
如何使用 Python 操作 Excel 图片:插入、提取与压缩
开发语言·python·excel
石马马户2 天前
Excel 根据A列标签拆分为多个列数据
excel