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,
    });
  }
}
相关推荐
CircleMouse8 小时前
Excel导出问题:accessExternalStylesheet
excel
学习研习社18 小时前
如何在 Windows 上恢复已删除/未保存的 Excel 文件 - 5 种快速方法
windows·excel
ohoy21 小时前
easyPoi导出多sheet页 一个班级一张Sheet
excel
缺点内向1 天前
如何使用C#将Excel工作表拆分为独立文件
开发语言·c#·.net·excel
凌盛羽1 天前
用Python非常流行的openpyxl库对Excel(.xlsx格式)文件进行创建、读取、写入、显示等操作
数据库·python·链表·excel
CodeCraft Studio1 天前
Excel处理控件Aspose.Cells教程:使用 C# 在 Excel 中创建股票高低收盘图
信息可视化·c#·excel·aspose·股票收盘图·c# excel库·收盘图
Oll Correct1 天前
Excel基础操作(四)
笔记·excel
我命由我123451 天前
Excel - 在 Excel 中为指定列设置下拉选项
经验分享·学习·职场和发展·编辑器·excel·求职招聘·学习方法
Full Stack Developme1 天前
Java实现Word、Excel、PDF文件 在线预览
java·word·excel
heartbeat..2 天前
使用 Apache POI 实现 Excel 文件读写(导入 导出)操作的工具类
java·apache·excel·文件