如何解决如何通过使用计时器触发器在azure函数应用程序中创建Excel文件
我在azure函数应用程序中创建了一个方法,该方法从类中获取静态数据并将其存储到excel文件中,而我正在将excel文件存储在存储帐户中。当我运行该方法时,会在存储帐户中创建一个空的Excel文件
-
如何在Azure function-app中创建Excel文件
-
如何在Excel文件中存储数据
[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方法:
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();
}
}
我的Azure函数绑定:
{
"bindings": [
{
"name": "myblob","type": "blob","path": "name/empchange.csv","direction": "in","connection": "AzureWebJobsstorage"
},{
"name": "outputblob","path": "name/ExcelFile.xlsx","direction": "out","connection": "AzureWebJobsstorage"
}
],"disabled": false
}
解决方法
如果要将csv文件转换为excel文件,我们可以使用软件包EPPlus
例如
[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);
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。