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,
    });
  }
}
相关推荐
城数派7 分钟前
1990-2025年我国省市县三级的逐年土地覆盖数据(9类用地/Excel/Shp格式)
excel
catoop33 分钟前
构建高稳健性、可交互的复杂 Excel 报表方法论:切片、流式与动态公式
excel
葡萄城技术团队2 小时前
Excel VBA 核心概念全解析:宏、模块、过程的区别与联系(含 SpreadJS Web 替代方案)
excel
懒羊羊--搞点小技术4 小时前
Excel有一层表头和两层表头导出
excel
认真的小羽❅1 天前
0-1手写通用的 Excel 导入/导出工具类
java·excel
catoop1 天前
Excel 实战技巧:单元格相对引用 INDIRECT、ROW、COLUMN 函数
excel
Teable任意门互动1 天前
中小企业进销存实战:Teable多维表格从零搭建高效库存管理系统
开发语言·数据库·excel·飞书·开源软件
零零发聊技术1 天前
Excel 2016版的TextJoin函数为什么不能用?
excel·textjoin
catoop1 天前
Excel 实战技巧:动态单元格引用中使用 LET 函数优化 Excel 公式性能与可读性
excel
lengxuemo1 天前
Excel做正态分布图
学习·excel