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,
    });
  }
}
相关推荐
heartbeat..2 小时前
使用 Apache POI 实现 Excel 文件读写(导入 导出)操作的工具类
java·apache·excel·文件
星空椰5 小时前
Java Excel转PDF
pdf·excel
ohoy7 小时前
easypoi 带图片导入 公司表
excel
appleคิดถึง18 小时前
tp6数据导出excel文件时对大数据量处理
php·excel·tp6
heartbeat..18 小时前
注解 + 反射:Web 项目 Excel 一键导出工具 EnhancedExportExcelUtil 详解
java·excel·poi
yivifu1 天前
Excel表格取多行数据中的唯一值及多条件数据查询问题
excel
天一生水water1 天前
顶级多项目管理 Excel 模板
excel·敏捷流程
取啥都被占用1 天前
VBA的excel逐行替换到word模板及打印还原
excel·vba
坚定信念,勇往无前1 天前
vue3图片,pdf,word,excel,ppt多格式文件预览组件Vue Doc Viewers Plus
pdf·word·excel
weixin_462446232 天前
【原创实践】python 获取节假日列表 并保存为excel
数据库·python·excel