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,
    });
  }
}
相关推荐
E_ICEBLUE1 天前
Excel vs CSV:在系统数据处理中该如何选择?
java·excel·csv·格式转换
weixin_318088111 天前
Power query代替PowerBI加载数据到excel
excel·powerbi·power query
weixin_419349791 天前
excel批量把自身加上链接,这一列本身就是网址
excel
meng半颗糖1 天前
vue3+typeScript 在线预览 excel,word,pdf
typescript·word·excel
开开心心_Every1 天前
时间自动校准工具:一键同步网络服务器时间
游戏·随机森林·微信·pdf·逻辑回归·excel·语音识别
清山博客1 天前
EXCEL根据身份证号计算出生日期和截止某日的年龄
excel
偷心伊普西隆1 天前
Python EXCEL 半自动化切分数据集
python·自动化·excel
weixin_462446232 天前
Python 解析 Excel 图表(Chart)信息实战:从 xlsx 中提取标题、字体和数据
python·数据分析·excel·报表自动化
薛定谔的猫喵喵2 天前
解决 xlrd 2.0+ 版本只支持 xls 格式的问题
python·excel
椎4952 天前
苍穹外卖资源点整理+个人错误解析-Day12-数据统计-EXCEL报表
excel