Reputation: 67
I created a method in my azure function app which gets the static data from a class and stores it to a excel file I am storing the excel file in a storage account. when I run the method a empty Excel file is created in storage account
How to Create a Excel file in Azure function-app
How to store a data in Excel file
[FunctionName("Function1")]
public static void Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, ILogger log, [Blob("name/empchange.csv", FileAccess.Read, Connection = "AzureWebJobsStorage")] Stream myblob, [Blob("name/TestNewExcel.xlsx", FileAccess.Write, Connection = "AzureWebJobsStorage")] TextWriter outputblob)
{
log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
try
{
// Read CSV File
ReadCsvFile(myblob);
// create Excel File
CreateExcel(outputblob);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
Excel method:
static void CreateExcel(TextWriter outputblob)
{
data clsdata = new data();
DataTable table = clsdata.Getdata();
using (SpreadsheetDocument document = SpreadsheetDocument.Create(outputblob.ToString(), SpreadsheetDocumentType.Workbook))
{
WorkbookPart workbookPart = document.AddWorkbookPart();
workbookPart.Workbook = new Workbook();
WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
var sheetData = new SheetData();
worksheetPart.Worksheet = new Worksheet(sheetData);
Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets());
Sheet sheet = new Sheet() { Id = workbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "Sheet1" };
sheets.Append(sheet);
Row headerRow = new Row();
List<String> columns = new List<string>();
foreach (DataColumn column in table.Columns)
{
columns.Add(column.ColumnName);
Cell cell = new Cell();
cell.DataType = CellValues.String;
cell.CellValue = new CellValue(column.ColumnName);
headerRow.AppendChild(cell);
}
sheetData.AppendChild(headerRow);
foreach (System.Data.DataRow dsrow in table.Rows)
{
Row newRow = new Row();
foreach (String col in columns)
{
Cell cell = new Cell();
cell.DataType = CellValues.String;
cell.CellValue = new CellValue(dsrow[col].ToString());
newRow.AppendChild(cell);
}
sheetData.AppendChild(newRow);
}
workbookPart.Workbook.Save();
}
}
my Azure function Bindings:
{
"bindings": [
{
"name": "myblob",
"type": "blob",
"path": "name/empchange.csv",
"direction": "in",
"connection": "AzureWebJobsStorage"
},
{
"name": "outputblob",
"type": "blob",
"path": "name/ExcelFile.xlsx",
"direction": "out",
"connection": "AzureWebJobsStorage"
}
],
"disabled": false
}
Upvotes: 1
Views: 4307
Reputation: 23141
If you want to convert csv file to excel file, we can use the package EPPlus
For example
[FunctionName("Function1")]
public static async Task Run(
[TimerTrigger("0 */5 * * * *",RunOnStartup =true)] TimerInfo myTimer,
[Blob("input/test.csv", FileAccess.ReadWrite, Connection = "AzureWebJobsStorage")] CloudBlockBlob blob,
[Blob("test/TestNewExcel.xlsx", FileAccess.Write, Connection = "AzureWebJobsStorage")] Stream output,
ILogger log)
{
log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
string worksheetsName = "TEST";
bool firstRowIsHeader = false;
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
var format = new ExcelTextFormat();
format.Delimiter = ',';
format.EOL = "\r";
using (var ms = new MemoryStream()) {
using (ExcelPackage package = new ExcelPackage(new FileInfo(@"E:\test.xlsx")))
{
string content = await blob.DownloadTextAsync();
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add(worksheetsName);
worksheet.Cells["A1"].LoadFromText(content, format, OfficeOpenXml.Table.TableStyles.Medium27, firstRowIsHeader);
await package.SaveAsAsync(output).ConfigureAwait(false);
}
}
}
Upvotes: 1