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,
    });
  }
}
相关推荐
悟能不能悟33 分钟前
springboot controller返回的是HttpServletResponse成功返回excel文件流,失败就返回失败参数
spring boot·后端·excel
野比带雄1 天前
対excel时间格式的理解
excel
缺点内向1 天前
Java:轻松实现 Excel 文档属性添加
java·开发语言·excel
Teable任意门互动1 天前
从飞书多维表格 简道云到Teable多维表格:企业为何选择Teable作为新一代智能数据协作平台?
数据库·excel·钉钉·飞书·开源软件
AC赳赳老秦1 天前
DeepSeek + Excel 实战:多表联动分析与异常数据自动预警教程
microsoft·rabbitmq·excel·etcd·memcached·memcache·deepseek
2501_930707781 天前
如何使用C#代码将 Excel 文件转换为 SVG
开发语言·c#·excel
Eiceblue1 天前
将 Python 列表导出为 Excel 文件:一维、二维、字典列表
开发语言·python·excel·visual studio code
Shi_haoliu2 天前
inno setup6.6.1实例,制作安装包,创建共享文件夹,写入注册表(提供给excel加载项,此文章解释iss文件)
前端·vue.js·windows·excel
2501_907136822 天前
通用财务账模板【Excel版】
excel·软件需求
CodeCraft Studio2 天前
Excel处理控件Aspose.Cells教程:使用C#在Excel中创建折线图
java·c#·excel·aspose.cells·excel图表·excel api库·excel折线图