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 小时前
资条生成器 V1.3 - 财务 HR 专属 Excel 工具
excel·实用工具
m0_502724954 小时前
vue3在线预览excel表格
javascript·vue.js·excel
Java小王子呀6 小时前
JAVA 导出Excel中添加下拉框用POI
java·excel
傻啦嘿哟21 小时前
Python 操作 Excel 条件格式指南
开发语言·python·excel
jgyzl1 天前
2026.3.20 用EasyExcel实现excel报表的导入与导出
java·python·excel
Mr.45672 天前
Spring Boot 3 + EasyExcel 3.x 实战:构建高效、可靠的Excel导入导出服务
spring boot·后端·excel
如意机反光镜裸2 天前
excel怎么快速导入oracle
数据库·oracle·excel
开开心心就好2 天前
免费轻量级PDF阅读器,打开速度快
windows·计算机视觉·visualstudio·pdf·计算机外设·excel·myeclipse
城数派3 天前
2015-2025年我国区县逐年二手房房价数据(Excel/Shp格式)
excel