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,
    });
  }
}
相关推荐
骆驼爱记录26 分钟前
WPS页码设置:第X页共Y-1页
自动化·word·excel·wps·新人首发
Cxiaomu1 天前
Python 文件解析: Excel / Word / PDF 的解析、处理、预览与下载
python·word·excel
2501_930707781 天前
如何使用C#代码从 PDF 中提取表格并另存为Excel文件
pdf·excel
pacong1 天前
B生所学EXCEL
人工智能·excel
城数派1 天前
2014-2025年全国监测站点的逐年空气质量数据(15个指标\Excel\Shp格式)
数据分析·excel
亮子AI1 天前
【Excel】免费并且有暗黑模式的 Excel
excel
罗政1 天前
Excel打印格式批量调整方法,上千份表格一键统一设置
excel
城数派2 天前
2005-2025年我国逐日露点温度栅格数据
数据分析·excel
LAM LAB2 天前
【VBA】Excel实现批量对单元格替换、加粗文字
excel·vba
海兰2 天前
【接上篇】多格式文档支持扩展方案(PDF_Word_Excel)
pdf·word·excel