【问题标题】:msgraph - download xlsx file from sharepoint using msgraph servicemsgraph - 使用 msgraph 服务从 sharepoint 下载 xlsx 文件
【发布时间】:2021-08-15 21:44:04
【问题描述】:

我需要从myFolder 下载所有xlsx 文件我有两种方法getExcelSheets 来获取所有excel 工作表列表

然后使用此信息通过调用getFileContentById 方法使用@microsoft.graph.downloadUrl 下载文件,但我未能获取并将其转换为xlsx 格式

import { Client } from "@microsoft/microsoft-graph-client";
import axios from "axios";
import * as MicrosoftGraph from "@microsoft/microsoft-graph-types";

export async function getExcelSheets(
  template_name: string,
  msgraph_client: Client,
): Promise<MicrosoftGraph.DriveItem[]> {
  const result = await msgraph_client
    .api(
      `drives/{driver_id}/root:/myFolder/${template_name}:/children`
    )
    .get();
  return result.value as MicrosoftGraph.DriveItem[];
}

export async function getFileContentById(download_url: string): Promise<any> {
  const response = await axios.get(download_url);
  return response;
}

关于我如何获取文件然后将其转换为xlsx的任何提示我正在使用此方法将缓冲区转换为xlsx

xlsx.read(file, { type: "buffer" })

【问题讨论】:

    标签: excel typescript microsoft-graph-api xlsx


    【解决方案1】:

    您在 getFileContentById 中进行的 Axios 调用将收到一个流,您可以将其写入文件或转换为 xlsx,如下所示,

    export async function getFileContentById(download_url: string): Promise<any> {
       const writer = fs.createWriteStream('file.xlsx');
       return axios({
          method: 'get',
          url: download_url,
          responseType: 'stream',
       }).then(response => {
          // Save the file and read later
          response.data.pipe(writer);
          
          // OR Convert the binary to xlsx usibng the library
          const sheet = XLSX.read(response.data, { type: "buffer" });
          console.log(sheet)
          /*
          {
            SheetNames: [ 'Sheet1' ],
            Sheets: { Sheet1: { A1: [Object], '!ref': 'A1' } }
          }
          */
       });
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-05
      • 2020-01-11
      • 2012-12-21
      • 2019-01-07
      • 2022-09-27
      • 1970-01-01
      • 1970-01-01
      • 2021-08-02
      相关资源
      最近更新 更多