首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >msgraph从sharepoint下载xlsx文件,使用msgraph服务

msgraph从sharepoint下载xlsx文件,使用msgraph服务
EN

Stack Overflow用户
提问于 2021-08-15 21:44:04
回答 1查看 112关注 0票数 0

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

然后使用这些信息通过调用@microsoft.graph.downloadUrl方法下载文件,但是我没有将其转换为xlsx格式。

代码语言:javascript
复制
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

代码语言:javascript
复制
xlsx.read(file, { type: "buffer" })
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-18 09:49:28

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

代码语言:javascript
复制
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' } }
      }
      */
   });

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68795541

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档