「東雲 忠太郎」の平凡な日常のできごと

「東雲 忠太郎」の平凡な日常のできごと

2024.02.26
XML
カテゴリ: VB.NET


Imports System.Net.Http

Imports System.Text

Imports System.Threading.Tasks

Imports System.IO


Module Module1


    Sub Main()

        Dim siteUrl As String = "https://your-sharepoint-site-url"

        Dim username As String = "your-username"

        Dim password As String = "your-password"

        Dim folderUrl As String = "/sites/your-site/shared documents/your-folder/" ' 共有フォルダのURL

        Dim fileName As String = "NewExcelFile.xlsx"

        Dim excelFilePath As String = "path\to\your\excel\file.xlsx" ' 作成するエクセルファイルのパス


        CreateExcelFileInSharePoint(siteUrl, username, password, folderUrl, fileName, excelFilePath).Wait()

    End Sub


    Async Function CreateExcelFileInSharePoint(siteUrl As String, username As String, password As String, folderUrl As String, fileName As String, excelFilePath As String) As Task

        Dim credentials As String = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{username}:{password}"))


        Using client As New HttpClient()

            client.DefaultRequestHeaders.Authorization = New System.Net.Http.Headers.AuthenticationHeaderValue("Basic", credentials)


            ' ファイルストリームを読み込む

            Dim fileStream As New FileStream(excelFilePath, FileMode.Open)

            Dim fileContent As New StreamContent(fileStream)


            ' ファイルをアップロードするためのエンドポイントURLを作成

            Dim endpointUrl As String = $"{siteUrl}/_api/web/getfolderbyserverrelativeurl('{folderUrl}')/files/add(url='{fileName}',overwrite=true)"


            ' HTTP POST リクエストを送信してファイルをアップロード

            Dim response As HttpResponseMessage = Await client.PostAsync(endpointUrl, fileContent)


            ' レスポンスをチェックして結果を表示

            If response.IsSuccessStatusCode Then

                Console.WriteLine($"Excel file '{fileName}' created successfully in SharePoint folder '{folderUrl}'.")

            Else

                Console.WriteLine($"Failed to create Excel file in SharePoint folder. StatusCode: {response.StatusCode}")

            End If

        End Using

    End Function

End Module







お気に入りの記事を「いいね!」で応援しよう

Last updated  2024.02.26 02:58:09


【毎日開催】
15記事にいいね!で1ポイント
10秒滞在
いいね! -- / --
おめでとうございます!
ミッションを達成しました。
※「ポイントを獲得する」ボタンを押すと広告が表示されます。
x

© Rakuten Group, Inc.
Create a Mobile Website
スマートフォン版を閲覧 | PC版を閲覧
Share by: