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 vlookup一对多(第二篇)
excel
开开心心就好5 小时前
专为视障人士设计的免费辅助工具
windows·计算机视觉·计算机外设·excel·散列表·推荐算法·csdn开发云
transformer_WSZ5 小时前
excel两列数据绘制折线图
excel·折线图
蒋胜山10 小时前
Excel 练习题(5)
经验分享·excel
Data-Miner17 小时前
数以轻舟聚焦Excel-Agent场景:当AI做表工具学会说人话
人工智能·excel
夏日清风有你1 天前
Excel 中绘制散点图(Scatter Plot)
excel
诸葛大钢铁1 天前
如何比较Excel表格内容?根据三种情况提供比较方法
经验分享·excel·比对excel·内容比对
wtsolutions1 天前
JSON-to-Excel 本地化应用发布:安全离线转换,数据零泄露
安全·json·excel
深念Y1 天前
AI时代办公格式的演进:PPT与Word的替代已现,Excel将走向何方?
数据库·人工智能·html·word·powerpoint·excel·markdown
一晌小贪欢1 天前
《Python办公Excel处理》第二节:精通openpyxl,让Excel排版与读写自动化
python·自动化·excel