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,
    });
  }
}
相关推荐
CodeCraft Studio19 小时前
Excel处理控件Aspose.Cells教程:使用 Python 将 Pandas DataFrame 转换为 Excel
python·json·excel·pandas·csv·aspose·dataframe
星空的资源小屋1 天前
PPTist,一个完全免费的 AI 生成 PPT 在线网站
人工智能·python·电脑·excel
开开心心_Every1 天前
免费语音合成工具:66种音色随心选
人工智能·面试·java-ee·计算机外设·电脑·maven·excel
偷心伊普西隆2 天前
EXCEL VBA 清空Excel工作表(Sheet)的方法
microsoft·excel
Coding_Doggy2 天前
苍穹外卖Day12 | Apache POI、导出Excel报表、HttpServletResponse、工作台
excel
l1t3 天前
张泽鹏先生手搓的纯ANSI处理UTF-8与美团龙猫调用expat库读取Excel xml对比测试
xml·人工智能·excel·utf8·expat
Source.Liu3 天前
【Python自动化】 21 Pandas Excel 操作完整指南
python·excel·pandas
会飞的小菠菜3 天前
如何根据Excel数据表生成多个合同、工作证、录取通知书等word文件?
word·excel·模板·数据表·生成文件
Access开发易登软件4 天前
Access开发导出PDF的N种姿势,你get了吗?
后端·低代码·pdf·excel·vba·access·access开发
課代表4 天前
VBA 中的 Excel 工作表函数
excel·vba·函数·对象·属性·range·静态变量