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,
    });
  }
}
相关推荐
weixin_4624462314 小时前
Python 解析 Excel 图表(Chart)信息实战:从 xlsx 中提取标题、字体和数据
python·数据分析·excel·报表自动化
薛定谔的猫喵喵14 小时前
解决 xlrd 2.0+ 版本只支持 xls 格式的问题
python·excel
椎49517 小时前
苍穹外卖资源点整理+个人错误解析-Day12-数据统计-EXCEL报表
excel
喜欢吃豆18 小时前
从「文件URL」到「模型可理解内容」:一套完整的文件上传与解析处理流程详解(含PDF/Excel/图片)
pdf·大模型·excel
帆张芳显18 小时前
智表zcell产品V3.5 版发布,新增行列选中操作等功能
前端·javascript·excel·插件·canva可画
夜喵YM19 小时前
基于 Spire.XLS.Free for Java 实现无水印 Excel 转 PDF
java·pdf·excel
TheNextByte120 小时前
如何将Android联系人导出为 Excel 格式
android·excel
☀Mark_LY20 小时前
java读取excel文件返回JSON
java·json·excel
Ahtacca20 小时前
拒绝重复造轮子:利用自定义注解封装POI,实现Java通用Excel解析
java·javascript·vue·excel
私人珍藏库20 小时前
[吾爱大神原创工具] Word图片批量导出并插入Excel对应单元格
word·excel