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,
    });
  }
}
相关推荐
开开心心就好3 小时前
电子教材下载工具,支持多链接批量下载
windows·随机森林·计算机视觉·pdf·计算机外设·逻辑回归·excel
码上上班4 小时前
k8s中安装metrics,实现hpa
容器·kubernetes·excel
fanchenxinok1 天前
LIN矩阵Excel ⇄ LDF互转工具:打通设计数据与协议描述的关键桥梁
矩阵·excel·lin·ldf·excel和ldf互转
拆房老料1 天前
多人协同编辑Excel时,筛选相互干扰怎么办?Onlyoffice中国版给出了与WPS一样的答案
编辑器·excel·开源软件·wps
Data-Miner1 天前
Excel-Agent:你的专属 AI 数据分析助手
人工智能·数据分析·excel
xcLeigh1 天前
告别 Excel 繁琐操作!Metabase让数据可视化触手可及
mysql·docker·信息可视化·excel·数据可视化·metabase·cpolar
葡萄城技术团队1 天前
Excel 2025 年度回顾:文化、匠心与 Copilot 开启的计算新纪元
excel·copilot
tlwlmy1 天前
python excel图片批量拼接导出
前端·python·excel
开开心心就好3 天前
绿色版PDF多功能工具,支持编辑转换
人工智能·windows·pdf·ocr·excel·语音识别·harmonyos
优选资源分享3 天前
资条生成器 V1.3 - 财务 HR 专属 Excel 工具
excel·实用工具