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,
    });
  }
}
相关推荐
WarPigs17 小时前
Excel WPS表格笔记
excel·wps
fengyehongWorld19 小时前
Excel 切片器 快速过滤数据
excel
骆驼爱记录19 小时前
Word兼容性问题全解析
自动化·word·excel·wps·新人首发
funnycoffee1232 天前
EXCEL VBA Color 将选中区域 的指定字符,设置字体颜色(比如红)
excel·excel vba·vba字体颜色
m5655bj2 天前
通过 Python 删除 Excel 中的空白行列
python·ui·excel
2501_944934732 天前
大专信息统计与分析专业,怎么提升Excel高级函数的使用能力?
大数据·excel
优选资源分享2 天前
ASAP Utilities V9.2:Excel 办公效率插件 中文版
excel
技小宝2 天前
如何在WPS/Excel中批量翻译长文本?
经验分享·职场和发展·excel·wps
AI_56782 天前
Excel数据透视表提速:Power Query预处理百万数据
数据库·excel
颜挺锐2 天前
EXCEL中A列连接B列中间加| 怎么写公式
excel