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,
    });
  }
}
相关推荐
夜流冰4 小时前
Excel - MS Support for Excel: 2 Collaborate
数据库·windows·excel
LingRannn12 小时前
【vim 中如何替换】
编辑器·vim·excel
yesyesido13 小时前
智能文件格式转换器:文本/Excel与CSV无缝互转的在线工具
开发语言·python·excel
开开心心_Every1 天前
免费进销存管理软件:云端本地双部署
java·游戏·微信·eclipse·pdf·excel·语音识别
Kasen's experience1 天前
Excel 怎么快速合并同一个ID不同行的大量相同单元格
excel
mudtools1 天前
基于.NET操作Excel COM组件生成数据透视报表
c#·.net·excel
yangminlei1 天前
Spring Boot+EasyExcel 实战:大数据量 Excel 导出(高效无 OOM)
spring boot·后端·excel
NignSah2 天前
Microsoft Excel World Championship 2025-2025EXCEL大赛,折纸
microsoft·excel
hhzz2 天前
Springboot项目中使用POI操作Excel(详细教程系列1/3)
spring boot·后端·excel·poi·easypoi
林月明2 天前
【VBA】点击一个按钮实现自动更新excel文件列数据
excel·vba·宏文件·一键数据更新