Skip to content

Commit f84435b

Browse files
Merge pull request #2136 from syncfusion-content/1003858-spreadsheet-cloud
1003858: Added Spreadsheet cloud storage ug contents
2 parents 169ebac + 7ca3593 commit f84435b

32 files changed

Lines changed: 2097 additions & 898 deletions

Document-Processing-toc.html

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5081,8 +5081,45 @@
50815081
<li><a href="/document-processing/excel/spreadsheet/react/getting-started">Getting Started</a></li>
50825082
<li><a href="/document-processing/excel/spreadsheet/react/nextjs-getting-started">Getting Started with NextJS</a></li>
50835083
<li><a href="/document-processing/excel/spreadsheet/react/data-binding">Data Binding</a></li>
5084-
<li><a href="/document-processing/excel/spreadsheet/react/open-save">Open and Save</a></li>
5085-
<li><a href="/document-processing/excel/spreadsheet/react/docker-deployment">Docker Deployment</a></li>
5084+
<li><a href="/document-processing/excel/spreadsheet/react/open/open">Open Excel File</a>
5085+
<ul>
5086+
<li><a href="/document-processing/excel/spreadsheet/react/open/open-excel-file/file-uploader">using a file uploader</a></li>
5087+
<li><a href="/document-processing/excel/spreadsheet/react/open/open-excel-file/external-url">using an external URL</a></li>
5088+
<li><a href="/document-processing/excel/spreadsheet/react/open/open-excel-file/blob-data">from blob data</a></li>
5089+
<li><a href="/document-processing/excel/spreadsheet/react/open/open-excel-file/base64-string">from base64 string</a></li>
5090+
<li><a href="/document-processing/excel/spreadsheet/react/open/open-excel-file/server">located on a server</a></li>
5091+
<li><a href="/document-processing/excel/spreadsheet/react/open/open-excel-file/aws-lambda">using AWS Lambda hosted web service</a></li>
5092+
</ul>
5093+
</li>
5094+
<li><a href="/document-processing/excel/spreadsheet/react/save/save">Save Excel File</a>
5095+
<ul>
5096+
<li><a href="/document-processing/excel/spreadsheet/react/save/save-excel-file/blob-data">as blob data</a></li>
5097+
<li><a href="/document-processing/excel/spreadsheet/react/save/save-excel-file/base64-string">as base64 string</a></li>
5098+
<li><a href="/document-processing/excel/spreadsheet/react/save/save-excel-file/server">to a server</a></li>
5099+
<li><a href="/document-processing/excel/spreadsheet/react/save/save-excel-file/aws-lambda">using AWS Lambda hosted web service</a></li>
5100+
</ul>
5101+
</li>
5102+
<li><a href="/document-processing/excel/spreadsheet/react/open-from-cloud">Open From Cloud</a>
5103+
<ul>
5104+
<li><a href="/document-processing/excel/spreadsheet/react/cloud-storage/open-from-cloud/aws-s3-bucket">AWS S3</a></li>
5105+
<li><a href="/document-processing/excel/spreadsheet/react/cloud-storage/open-from-cloud/azure-blob-storage">Azure Blob Storage</a></li>
5106+
<li><a href="/document-processing/excel/spreadsheet/react/cloud-storage/open-from-cloud/google-cloud-storage">Google Cloud Storage</a></li>
5107+
</ul>
5108+
</li>
5109+
<li><a href="/document-processing/excel/spreadsheet/react/save-to-cloud">Save To Cloud</a>
5110+
<ul>
5111+
<li><a href="/document-processing/excel/spreadsheet/react/cloud-storage/save-to-cloud/aws-s3-bucket">AWS S3</a></li>
5112+
<li><a href="/document-processing/excel/spreadsheet/react/cloud-storage/save-to-cloud/azure-blob-storage">Azure Blob Storage</a></li>
5113+
<li><a href="/document-processing/excel/spreadsheet/react/cloud-storage/save-to-cloud/google-cloud-storage">Google Cloud Storage</a></li>
5114+
</ul>
5115+
</li>
5116+
<li><a href="/document-processing/excel/spreadsheet/react/server-deployment">Server Deployment</a>
5117+
<ul>
5118+
<li><a href="/document-processing/excel/spreadsheet/react/server-deployment/docker-deployment">How to Deploy Spreadsheet Docker Image Locally</a></li>
5119+
<li><a href="/document-processing/excel/spreadsheet/react/server-deployment/how-to-deploy-spreadsheet-server-to-azure-app-service-using-visual-studio">Deploy Spreadsheet Server Docker Image to Azure App Service using Azure CLI</a></li>
5120+
<li><a href="/document-processing/excel/spreadsheet/react/server-deployment/how-to-deploy-spreadsheet-server-to-azure-app-service-using-azure-cli">Deploy Spreadsheet Server to Azure App Service using Visual Studio</a></li>
5121+
</ul>
5122+
</li>
50865123
<li><a href="/document-processing/excel/spreadsheet/react/worksheet">Worksheet</a></li>
50875124
<li><a href="/document-processing/excel/spreadsheet/react/cell-range">Cell Range</a></li>
50885125
<li><a href="/document-processing/excel/spreadsheet/react/editing">Editing</a></li>
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
---
2+
layout: post
3+
title: Opening excel from AWS S3 in React Spreadsheet control | Syncfusion
4+
description: Learn about how to Open an Excel file from AWS S3 in React Spreadsheet control of Syncfusion Essential JS 2 and more details.
5+
platform: document-processing
6+
control: Open file from AWS S3
7+
documentation: ug
8+
---
9+
10+
# Open file from AWS S3
11+
12+
To load a file from AWS S3 in a Spreadsheet Component, you can follow the steps below
13+
14+
**Step 1:** Create a Simple Spreadsheet Sample in React
15+
16+
Start by following the steps provided in this [link](../../React//getting-started.md) to create a simple Spreadsheet sample in React. This will give you a basic setup of the Spreadsheet component.
17+
18+
**Step 2:** Modify the `SpreadsheetController.cs` File in the Web Service Project
19+
20+
1. Create a web service project in .NET Core 3.0 or above. You can refer to this [link](../../../Spreadsheet/React/open-save.md) for instructions on how to create a web service project.
21+
22+
2. Open the `SpreadsheetController.cs` file in your web service project.
23+
24+
3. Import the required namespaces at the top of the file:
25+
26+
```csharp
27+
28+
using Amazon;
29+
using Amazon.Runtime;
30+
using Amazon.S3;
31+
using Amazon.S3.Model;
32+
using Amazon.S3.Transfer;
33+
34+
```
35+
36+
4. Add the following private fields and constructor parameters to the `SpreadsheetController` class, In the constructor, assign the values from the configuration to the corresponding fields.
37+
38+
```csharp
39+
40+
private IConfiguration _configuration;
41+
public readonly string _accessKey;
42+
public readonly string _secretKey;
43+
public readonly string _bucketName;
44+
45+
public SpreadsheetController(IWebHostEnvironment hostingEnvironment, IMemoryCache cache, IConfiguration configuration)
46+
{
47+
_hostingEnvironment = hostingEnvironment;
48+
_cache = cache;
49+
_configuration = configuration;
50+
_accessKey = _configuration.GetValue<string>("AccessKey");
51+
_secretKey = _configuration.GetValue<string>("SecretKey");
52+
_bucketName = _configuration.GetValue<string>("BucketName");
53+
}
54+
55+
```
56+
57+
5. Create the `OpenFromS3()` method to open the document from the AWS S3 bucket.
58+
59+
```csharp
60+
61+
[Route("api/[controller]")]
62+
[ApiController]
63+
public class SpreadsheetController : ControllerBase
64+
{
65+
[HttpPost]
66+
[Route("OpenFromS3")]
67+
public async Task<IActionResult> OpenFromS3([FromBody] FileOptions options)
68+
{
69+
try
70+
{
71+
//Set AWS region and credentials
72+
var region = RegionEndpoint.USEast1;
73+
var config = new AmazonS3Config { RegionEndpoint = region };
74+
var credentials = new BasicAWSCredentials("your-access-key", "your-secretkey");
75+
//Create an S3 client to interact with AWS
76+
using (var client = new AmazonS3Client(credentials, config))
77+
{
78+
using (MemoryStream stream = new MemoryStream())
79+
{
80+
//Get the full file name using input from the client
81+
string bucketName = "your-bucket-name";
82+
string fileName = options.FileName + options.Extension;
83+
//Download the file from S3 into memory
84+
var response = await client.GetObjectAsync(new GetObjectRequest
85+
{
86+
BucketName = bucketName,
87+
Key = fileName
88+
});
89+
await response.ResponseStream.CopyToAsync(stream);
90+
stream.Position = 0; // Reset stream position for reading
91+
//Wrap the stream as a FormFile for processing
92+
OpenRequest open = new OpenRequest
93+
{
94+
File = new FormFile(stream, 0, stream.Length, options.FileName, fileName)
95+
};
96+
//Convert Excel file to JSON using Workbook.Open method.
97+
var result = Workbook.Open(open);
98+
//Return the JSON result to the client
99+
return Content(result, "application/json");
100+
}
101+
}
102+
}
103+
catch (Exception ex)
104+
{
105+
// Handle any errors and return a message
106+
Console.WriteLine($"Error: {ex.Message}");
107+
return Content("Error occurred while processing the file.");
108+
}
109+
}
110+
111+
// To receive file details from the client.
112+
public class FileOptions
113+
{
114+
public string FileName { get; set; } = string.Empty;
115+
public string Extension { get; set; } = string.Empty;
116+
}
117+
}
118+
119+
```
120+
121+
6. Open the `appsettings.json` file in your web service project, Add the following lines below the existing `"AllowedHosts"` configuration.
122+
123+
```json
124+
125+
{
126+
"Logging": {
127+
"LogLevel": {
128+
"Default": "Information",
129+
"Microsoft.AspNetCore": "Warning"
130+
}
131+
},
132+
"AllowedHosts": "*",
133+
"AccessKey": "Your Access Key from AWS S3",
134+
"SecretKey": "Your Secret Key from AWS S3",
135+
"BucketName": "Your Bucket name from AWS S3"
136+
}
137+
138+
```
139+
140+
N> Replace **Your Access Key from AWS S3**, **Your Secret Key from AWS S3**, and **Your Bucket name from AWS S3** with your actual AWS access key, secret key and bucket name.
141+
142+
**Step 3:** Modify the index File in the Spreadsheet sample to make a fetch call to the server to retrieve and load the Excel file from the AWS S3 bucket into the client-side spreadsheet.
143+
144+
```typescript
145+
146+
<button class="e-btn" (click)="openFromS3()">Import XLS files from AWS S3 bucket</button>
147+
148+
// Function to open a spreadsheet file from AWS S3 via an API call
149+
const openFromS3 = () => {
150+
spreadsheet.showSpinner();
151+
// Make a POST request to the backend API to fetch the file from S3. Replace the URL with your local or hosted endpoint URL.
152+
fetch('https://localhost:portNumber/api/spreadsheet/OpenFromS3', {
153+
method: 'POST',
154+
headers: {
155+
'Content-Type': 'application/json',
156+
},
157+
body: JSON.stringify({
158+
FileName: fileInfo.name, // Name of the file to open
159+
Extension: fileInfo.extension, // File extension
160+
}),
161+
})
162+
.then((response) => response.json()) // Parse the response as JSON
163+
.then((data) => {
164+
spreadsheet.hideSpinner();
165+
// Load the spreadsheet data into the UI.
166+
spreadsheet.openFromJson({ file: data, triggerEvent: true });
167+
})
168+
.catch((error) => {
169+
// Log any errors that occur during the fetch operation
170+
window.alert('Error importing file:', error);
171+
});
172+
};
173+
174+
```
175+
176+
N> The **AWSSDK.S3** NuGet package must be installed in your application to use the previous code example.
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
---
2+
layout: post
3+
title: Open excel from Azure Blob in React Spreadsheet control | Syncfusion
4+
description: Learn about how to Open an Excel file from Azure Blob Storage in React Spreadsheet control of Syncfusion Essential JS 2.
5+
platform: document-processing
6+
control: Open file from Azure Blob Storage
7+
documentation: ug
8+
---
9+
10+
# Open file from Azure Blob Storage
11+
12+
To load a file from Azure Blob Storage in a Spreadsheet Component, you can follow the steps below
13+
14+
**Step 1:** Create a Simple Spreadsheet Sample in React
15+
16+
Start by following the steps provided in this [link](../../React//getting-started.md) to create a simple Spreadsheet sample in React. This will give you a basic setup of the Spreadsheet component.
17+
18+
**Step 2:** Modify the `SpreadsheetController.cs` File in the Web Service Project
19+
20+
1. Create a web service project in .NET Core 3.0 or above. You can refer to this [link](../../../Spreadsheet/React/open-save.md) for instructions on how to create a web service project.
21+
22+
2. Open the `SpreadsheetController.cs` file in your web service project.
23+
24+
3. Import the required namespaces at the top of the file:
25+
26+
```csharp
27+
28+
using System;
29+
using System.IO;
30+
using System.Threading.Tasks;
31+
using Microsoft.AspNetCore.Http;
32+
using Microsoft.AspNetCore.Mvc;
33+
using Microsoft.Extensions.Configuration;
34+
using Syncfusion.EJ2.Spreadsheet;
35+
using Azure.Storage.Blobs;
36+
using Azure.Storage.Blobs.Specialized;
37+
38+
```
39+
40+
4. Add the following private fields and constructor parameters to the `SpreadsheetController` class, In the constructor, assign the values from the configuration to the corresponding fields.
41+
42+
```Csharp
43+
44+
private readonly string _storageConnectionString;
45+
private readonly string _storageContainerName;
46+
47+
public SpreadsheetController(IConfiguration configuration)
48+
{
49+
// Fetch values from appsettings.json
50+
_storageConnectionString = configuration.GetValue<string>("connectionString");
51+
_storageContainerName = configuration.GetValue<string>("containerName");
52+
}
53+
54+
```
55+
56+
5. Create the `OpenFromAzure()` method to open the document from the Azure Blob Storage.
57+
58+
```Csharp
59+
60+
[HttpPost]
61+
[Route("OpenFromAzure")]
62+
public async Task<IActionResult> OpenFromAzure([FromBody] FileOptions options)
63+
{
64+
if (options == null || string.IsNullOrWhiteSpace(options.FileName) || string.IsNullOrWhiteSpace(options.Extension))
65+
return BadRequest("Invalid file options.");
66+
67+
try
68+
{
69+
using (MemoryStream stream = new MemoryStream())
70+
{
71+
string fileName = options.FileName + options.Extension;
72+
73+
// Connect to Azure Blob Storage
74+
BlobServiceClient blobServiceClient = new BlobServiceClient(_storageConnectionString);
75+
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(_storageContainerName);
76+
BlockBlobClient blockBlobClient = containerClient.GetBlockBlobClient(fileName);
77+
78+
// Download file into memory
79+
await blockBlobClient.DownloadToAsync(stream);
80+
stream.Position = 0;
81+
82+
// Wrap stream as FormFile and convert to Spreadsheet-compatible JSON
83+
OpenRequest open = new OpenRequest
84+
{
85+
File = new FormFile(stream, 0, stream.Length, options.FileName, fileName)
86+
};
87+
88+
string result = Workbook.Open(open);
89+
return Content(result, "application/json");
90+
}
91+
}
92+
catch (Exception ex)
93+
{
94+
Console.WriteLine($"Error: {ex.Message}");
95+
return Content("Error occurred while processing the file.");
96+
}
97+
}
98+
99+
// DTO that receives file details from the client
100+
public class FileOptions
101+
{
102+
public string FileName { get; set; } = string.Empty;
103+
public string Extension { get; set; } = string.Empty;
104+
}
105+
106+
```
107+
108+
6. Open the `appsettings.json` file in your web service project, Add the following lines below the existing `"AllowedHosts"` configuration.
109+
110+
```Json
111+
112+
{
113+
"Logging": {
114+
"LogLevel": {
115+
"Default": "Information",
116+
"Microsoft.AspNetCore": "Warning"
117+
}
118+
},
119+
"AllowedHosts": "*",
120+
"connectionString": "DefaultEndpointsProtocol=https;AccountName=yourAccount;AccountKey=yourKey;EndpointSuffix=core.windows.net",
121+
"containerName": "your-container-name"
122+
}
123+
124+
```
125+
N> Note: Install the Azure.Storage.Blobs NuGet package in the service project.
126+
127+
**Step 3:** Modify the index File in the Spreadsheet sample to make a fetch call to the server to retrieve and load the Excel file from the Google Cloud Storage into the client-side spreadsheet.
128+
129+
```typescript
130+
131+
<button class="e-btn" onClick={openFromAzure}>
132+
Import XLS file from Azure Blob Storage
133+
</button>;
134+
135+
const openFromAzure = () => {
136+
spreadsheet.showSpinner();
137+
138+
fetch("https://localhost:portNumber/api/spreadsheet/OpenFromAzure", {
139+
method: "POST",
140+
headers: { "Content-Type": "application/json" },
141+
body: JSON.stringify({
142+
FileName: fileInfo.name, // e.g., "Report"
143+
Extension: fileInfo.extension // e.g., ".xlsx"
144+
})
145+
})
146+
.then((res) => res.json())
147+
.then((data) => {
148+
spreadsheet.hideSpinner();
149+
spreadsheet.openFromJson({ file: data, triggerEvent: true });
150+
})
151+
.catch((err) => window.alert("Error importing file: " + err));
152+
};
153+
154+
```

0 commit comments

Comments
 (0)