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,
    });
  }
}
相关推荐
傻啦嘿哟3 小时前
Python将Excel工作表转换为PDF:从入门到实战
python·pdf·excel
罗政4 小时前
WPS Excel快速进行同表内的单元格差异对比(高亮)
excel·wps
默默提升实验室10 小时前
Excel 数据透视表一键批量合并居中单元格(失效处理办法)
excel
Tony66668888817 小时前
EasyExcel导出多张图片
excel
罗政20 小时前
WPS Excel如何快速交换列(调整列顺序),删除多个不连续的列
excel·wps
小矮强20 小时前
Excel:通过身份证提取出生日期并计算年龄
excel
C#程序员一枚2 天前
导出百万量数据到Excel表
c#·excel
热心市民lcj2 天前
excel比较两列内容差异怎么弄,excel两个列进行数据比较。找A列有B列没有的数据显示到C列
excel
CodeCraft Studio2 天前
Excel处理控件Aspose.Cells教程:使用Python从Excel工作表中删除数据透视表
开发语言·python·excel·aspose·aspose.cells·数据透视表
开开心心_Every2 天前
Excel图片提取工具,批量导出无限制
学习·pdf·华为云·.net·excel·harmonyos·1024程序员节