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,
    });
  }
}
相关推荐
神奇侠202419 小时前
基于PaddleOCR对图片中的excel进行识别并转换成word(一)
python·word·excel·paddleocr
林枫依依20 小时前
Unity 将Excel表格中的数据导入到Mysql数据表中
数据库·mysql·excel
CodeJourney.21 小时前
基于DeepSeek与Excel的动态图表构建:技术融合与实践应用
数据库·人工智能·算法·excel
神奇侠202421 小时前
基于PaddleOCR对图片中的excel进行识别并转换成word优化(二)
opencv·excel·paddleocr
xll_0071 天前
使用Python设置excel单元格的字体(font值)
开发语言·python·excel
inxunoffice1 天前
批量将多个 Excel 表格中的某张图片替换为新的图片
excel
林枫依依1 天前
Unity 创建、读取、改写Excel表格数据
unity·excel
杜大哥1 天前
wps打开的excel如何插入提示框?
excel·wps
HBR666_2 天前
vue3 excel文件导入
前端·excel
玥老师2 天前
Excel/WPS表格中图片链接转换成对应的实际图片
excel·wps