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,
    });
  }
}
相关推荐
funnycoffee1234 小时前
EXCEL VBA Color 将选中区域 的指定字符,设置字体颜色(比如红)
excel·excel vba·vba字体颜色
m5655bj5 小时前
通过 Python 删除 Excel 中的空白行列
python·ui·excel
2501_944934735 小时前
大专信息统计与分析专业,怎么提升Excel高级函数的使用能力?
大数据·excel
优选资源分享5 小时前
ASAP Utilities V9.2:Excel 办公效率插件 中文版
excel
技小宝5 小时前
如何在WPS/Excel中批量翻译长文本?
经验分享·职场和发展·excel·wps
AI_567817 小时前
Excel数据透视表提速:Power Query预处理百万数据
数据库·excel
颜挺锐19 小时前
EXCEL中A列连接B列中间加| 怎么写公式
excel
J_liaty1 天前
基于POI实现Excel文件导入导出
excel·poi
Texous1 天前
Java解析Excel图片
java·excel·wps·office·excel图片处理·excel图片解析·excel图片提取
chatexcel2 天前
从Excel到PPT:如何利用自动化工具重构数据汇报流程
自动化·powerpoint·excel