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,
    });
  }
}
相关推荐
codeJinger6 小时前
【Python】操作Excel文件
python·excel
城数派15 小时前
谷歌18亿建筑足迹数据集 Google Open Buildings V3
数据库·arcgis·信息可视化·数据分析·excel
whatzhang00720 小时前
在 macOS 上从零配置 Vim:开启语法高亮 + 安装 vim-polyglot + 设置 gruvbox 主题
macos·vim·excel
fengyehongWorld20 小时前
Excel 杂项知识
excel
开开心心就好1 天前
免费好用:PPT演示计时提醒工具
windows·计算机视觉·计算机外设·逻辑回归·excel·深度优先·csdn开发云
NocoBase2 天前
为 Excel 数据快速构建 Web 应用:4 种方法对比
前端·人工智能·低代码·开源·excel
fengyehongWorld2 天前
Excel 照相机功能
excel
yuhulkjv3352 天前
豆包导出的Excel公式失效
人工智能·ai·chatgpt·excel·豆包·deepseek·ai导出鸭
诸葛大钢铁3 天前
Java实现Excel文件合并
java·windows·excel
Ada大侦探3 天前
Excel实现某一列特定选项进行筛选且配上特定颜色
excel