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,
    });
  }
}
相关推荐
DS随心转APP8 小时前
AI 一键导出 Word 与 Excel 实战应用指南
人工智能·ai·word·excel·deepseek·ai导出鸭
spencer_tseng10 小时前
excel 2003 [Cell division function]
excel·office
开开心心就好12 小时前
小白友好的程序联网封锁实用工具
windows·eureka·计算机外设·rabbitmq·word·excel·csdn开发云
SunnyDays10111 天前
Python操作Excel批注:从基础添加到高级自定义的完整指南
开发语言·python·excel
Eiceblue1 天前
Python 操作 Excel:数据分组、分类汇总与取消分组全解
开发语言·python·excel
城数派1 天前
2026年500米分辨率DEM地形数据(全球/全国/分省/分市)
数据库·arcgis·信息可视化·数据分析·excel
SunnyDays10111 天前
Python 操作 Excel 超链接:添加网页、文件、工作表和图片链接
python·excel
专注VB编程开发20年1 天前
我制作excel工作簿的选项卡,发给deep seek, 昨天修改了一天
前端·vue.js·excel
星越华夏2 天前
python办公自动化,csv文件/excel文件差集合并
开发语言·python·excel
开开心心就好2 天前
180套模板的图片艺术拼接实用工具
linux·服务器·网络·spring·智能手机·maven·excel