Skip to content

Commit dc35fd0

Browse files
Merge pull request #1940 from syncfusion-content/998246-LoadExcelG-drive
998246: Added the UG content for load excel file from google drive
2 parents 1dfd162 + 8969b3b commit dc35fd0

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

Document-Processing/Excel/Spreadsheet/Blazor/open-and-save.md

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,117 @@ An Excel file encoded as a Base64 string can be loaded into the Spreadsheet comp
7272
{% endhighlight %}
7373
{% endtabs %}
7474

75+
### Open an Excel file from Google Drive
76+
To load an Excel file from `Google Drive` in the Blazor Spreadsheet, follow the steps below.
77+
78+
**Prerequisites:**
79+
- [Google Cloud project](https://developers.google.com/workspace/guides/create-project) in the Google Cloud Console.
80+
- [Service account](https://cloud.google.com/iam/docs/service-accounts-create) within the GCP project.
81+
- [Service account key](https://cloud.google.com/iam/docs/keys-create-delete) (JSON) available on disk.
82+
- [Google Drive API enabled](https://console.cloud.google.com/apis/library/drive.googleapis.com) for the project.
83+
- [Google Drive account](https://drive.google.com/) with access to the file to download.
84+
- [Google.Apis.Drive.v3](https://www.nuget.org/packages/Google.Apis.Drive.v3) NuGet package installed in your project to access Google Drive API.
85+
86+
**Step 1:** Install required NuGet packages
87+
88+
To use Google Drive with the Blazor Spreadsheet, install the following packages:
89+
90+
- [Google.Apis.Drive.v3](https://www.nuget.org/packages/Google.Apis.Drive.v3) — to access the Google Drive API
91+
- [Syncfusion.Blazor.Spreadsheet](https://www.nuget.org/packages/Syncfusion.Blazor.Spreadsheet) — to use the Syncfusion Blazor Spreadsheet component
92+
93+
**Step 2:** Include the following namespaces in the **Index.razor** file
94+
95+
Import the required namespaces at the top of the file:
96+
97+
```
98+
@using Google.Apis.Auth.OAuth2;
99+
@using Google.Apis.Drive.v3;
100+
@using Google.Apis.Services;
101+
@using Syncfusion.Blazor.Spreadsheet;
102+
@using System.IO;
103+
```
104+
105+
**Step 3:** Download the Excel file, convert to bytes, and prepare for binding
106+
107+
Add the below code example to download the `Google Drive` file using the Drive API, convert the stream to a byte array, and bind it to the [DataSource](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Spreadsheet.SfSpreadsheet.html#Syncfusion_Blazor_Spreadsheet_SfSpreadsheet_DataSource) property.
108+
109+
{% tabs %}
110+
{% highlight razor %}
111+
112+
@page "/"
113+
114+
@if (IsSpreadsheetDataLoaded)
115+
{
116+
<SfSpreadsheet DataSource="DataSourceBytes">
117+
<SpreadsheetRibbon></SpreadsheetRibbon>
118+
</SfSpreadsheet>
119+
}
120+
@code{
121+
122+
public byte[] DataSourceBytes { get; set; }
123+
124+
// Flag to indicate whether the spreadsheet data has been loaded and is ready for rendering
125+
public bool IsSpreadsheetDataLoaded { get; set; }
126+
127+
protected override async Task OnInitializedAsync()
128+
{
129+
//Download the document from Google Drive
130+
MemoryStream stream = await GetDocumentFromGoogleDrive();
131+
132+
//Set the position as '0'
133+
stream.Position = 0;
134+
135+
// Convert the MemoryStream to a byte array to be used as the DataSource
136+
DataSourceBytes = stream.ToArray();
137+
138+
// Set the flag to true to indicate that the spreadsheet data is ready
139+
IsSpreadsheetDataLoaded = true;
140+
}
141+
142+
// Download file from Google Drive
143+
public async Task<MemoryStream> GetDocumentFromGoogleDrive()
144+
{
145+
//Define the path to the service account key file
146+
string serviceAccountKeyPath = "Your_service_account_key_path";
147+
148+
//Specify the file ID of the file to download
149+
string fileID = "Your_file_id";
150+
151+
try
152+
{
153+
//Authenticate the Google Drive API access using the service account key
154+
GoogleCredential credential = GoogleCredential.FromFile(serviceAccountKeyPath).CreateScoped(DriveService.ScopeConstants.Drive);
155+
156+
//Create the Google Drive service
157+
DriveService service = new DriveService(new BaseClientService.Initializ()
158+
{
159+
HttpClientInitializer = credential
160+
});
161+
162+
//Create a request to get the file from Google Drive
163+
var request = service.Files.Get(fileID);
164+
165+
//Download the file into a MemoryStream
166+
MemoryStream stream = new MemoryStream();
167+
await request.DownloadAsync(stream);
168+
169+
return stream;
170+
}
171+
catch (Exception ex)
172+
{
173+
Console.WriteLine($"Error retrieving document from Google Drive: {ex.Message}");
174+
throw;
175+
}
176+
}
177+
}
178+
179+
{% endhighlight %}
180+
{% endtabs %}
181+
182+
N> Replace **Your_file_id** with the actual Google Drive file ID, and **Your_service_account_key_path** with the actual path to your service account key JSON file.
183+
184+
N> The **FileID** is the unique identifier for a Google Drive file. For example, if the file URL is: `https://drive.google.com/file/d/abc123xyz456/view?usp=sharing`, then the file ID is `abc123xyz456`.
185+
75186
### Supported file formats
76187
The Spreadsheet component supports opening the following file formats:
77188
* Microsoft Excel Workbook (.xlsx)

0 commit comments

Comments
 (0)