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,
    });
  }
}
相关推荐
Channing Lewis5 小时前
Python读取excel转成html,并且复制excel中单元格的颜色(字体或填充)
python·html·excel
醉卧考场君莫笑14 小时前
excel数据统计与数据可视化
信息可视化·excel
weixin_4404016915 小时前
WPS Excel 宏使用
excel··wps
GalenZhang88815 小时前
Excel/WPS 表格数据合并操作指南
excel·wps
海拥✘16 小时前
Excel制作跳动爱心动画:一步步创建动态数学心形图
excel
教练、我想打篮球17 小时前
127 apache poi3.11 写 word 中内嵌 表格换行的输出
word·excel·docx·换行
醉卧考场君莫笑1 天前
EXCEL数据分析基础(没有数据统计和数据可视化)
信息可视化·数据分析·excel
yesyesyoucan1 天前
智能文件格式转换平台:文本/Excel与CSV的无缝互转解决方案
excel
hqyjzsb2 天前
2026年AI证书选择攻略:当“平台绑定”与“能力通用”冲突,如何破局?
大数据·c语言·人工智能·信息可视化·职场和发展·excel·学习方法
牛奔2 天前
Linux 的日志分析命令
linux·运维·服务器·python·excel