I have a console application which is fire some SQL queries on azure SQL and data will transfer into excel file. This is working fine on my local computer. Issue But I want host this .exe on azure service as scheduler. That time I realize, how to keep my generated file excel on azure.
public static bool CreateExcelDocument(DataSet ds, string excelFilename)
{
try
{
using (SpreadsheetDocument document = SpreadsheetDocument.Create(excelFilename, SpreadsheetDocumentType.Workbook))
{
WriteExcelFile(ds, document);
}
Trace.WriteLine("Successfully created: " + excelFilename);
return true;
}
catch (Exception ex)
{
Trace.WriteLine("Failed, exception thrown: " + ex.Message);
return false;
}
}
In above code, What I need to pass "excelFilename"?
In above code, What I need to pass "excelFilename"?
In Azure, I suggest save the excel file to Azure Blob Storage. Based on your code, you could create a new excel which hosted in memory stream. After wrote data to this excel file, we can upload the memory stream to Blob Storage. Code below is for your reference.
public static bool CreateExcelDocument(DataSet ds, string fileName)
{
try
{
MemoryStream ms = new MemoryStream();
using (SpreadsheetDocument document = SpreadsheetDocument.Create(ms, SpreadsheetDocumentType.Workbook))
{
WriteExcelFile(ds, document);
}
//You need to create a storage account and put your azure storage connection string in following place
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("put your azure storage connection string here");
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("excelfilecontainer");
container.CreateIfNotExists();
CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);
ms.Position = 0;
blockBlob.UploadFromStream(ms);
return true;
}
catch (Exception ex)
{
return false;
}
}
To use upper method, you just need to place the file name in the second parameter.
CreateExcelDocument(ds, "abc.xlsx");
After that, a file named abc.xlsx will be created in excelfilecontainer of your Blob Storage. You could view or download it from Azure Storage Explorer or Azure Storage Client Library.
And If excel sheet or dataset have more than one. Then how to add new sheet ?
We also could read the blob data to a memory stream. Then we can open a SpreadsheetDocument based this stream. After add a new sheet. We need to save this stream back to blob storage. Here is the sample code.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("storage connection string");
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference("excelfilecontainer");
// Retrieve reference to a blob named "myblob.txt"
CloudBlockBlob blockBlob = container.GetBlockBlobReference("abc.xlsx");
using (var memoryStream = new MemoryStream())
{
blockBlob.DownloadToStream(memoryStream);
memoryStream.Position = 0;
using (SpreadsheetDocument doc = SpreadsheetDocument.Open(memoryStream, false))
{
}
}