@Nandan Hegde I have tried with C# http trigger function. Sharing the code snippet. for excel operations I have used OpenXML SDK. Also for the replace operation I have used C# only, you may use regex expressions to reduce line of codes.
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
------------
public static class Function1
{
[FunctionName("Function1")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
try
{
string StorageConnection = "<your storage connection>"; //read this from configuration
if (CloudStorageAccount.TryParse(StorageConnection, out CloudStorageAccount storageAccount))
{
CloudBlobClient BlobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = BlobClient.GetContainerReference("<container name>");
if (await container.ExistsAsync())
{
CloudBlob file = container.GetBlobReference("Blobtest.xlsx"); //replace the filename
if (await file.ExistsAsync())
{
using (MemoryStream ms = new MemoryStream())
{
await file.DownloadToStreamAsync(ms);
Stream blobStream = file.OpenReadAsync().Result;
using (SpreadsheetDocument workbook = SpreadsheetDocument.Open(ms, true, new OpenSettings { AutoSave = true }))
{
SharedStringTablePart sharedStringsPart = workbook.WorkbookPart.SharedStringTablePart;
IEnumerable<Text> sharedStringTextElements = sharedStringsPart.SharedStringTable.Descendants<Text>();
DoReplace(sharedStringTextElements);
IEnumerable<WorksheetPart> worksheetParts = workbook.GetPartsOfType<WorksheetPart>();
foreach (var worksheet in worksheetParts)
{
var allTextElements = worksheet.Worksheet.Descendants<Text>();
DoReplace(allTextElements);
}
}
ms.Seek(0L, SeekOrigin.Begin);
var cloudBlockBlob = container.GetBlockBlobReference("Blobtest_Updated1.xlsx");
if (await cloudBlockBlob.ExistsAsync() == false)
{
ms.Position = 0;
using (var uploadStream = ms)
{
await cloudBlockBlob.UploadFromStreamAsync(uploadStream);
}
}
}
}
}
}
}
catch (Exception e)
{
throw e;
}
string name = req.Query["name"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;
string responseMessage = string.IsNullOrEmpty(name)
? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
: $"Hello, {name}. This HTTP triggered function executed successfully.";
return new OkObjectResult(responseMessage);
}
private static void DoReplace(IEnumerable<Text> textElements)
{
string[] chars = new string[] { ",", ".", "/", "!", "@", "#", "$", "%", "^", "&", "*", "'", "\"", ";", "_", "(", ")", ":", "|", "[", "]" };
foreach (var text in textElements)
{
for(int i = 0; i < chars.Length; i++)
{
if(text.Text.Contains(chars[i]))
{
text.Text = text.Text.Replace(chars[i], "");
}
}
}
}
}
You can modify the code according to your requirement. You can directly get the sheet data as well:
WorkbookPart workbookPart = myWorkbook.WorkbookPart;
WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();
SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>();
If you think this answer helped , please "Accept the answer" and "Up-vote" so that it can help others in the community.