C# 将整个数据集转换为Excel,并使用EPPlus c将其存储在Azure blob中
我使用了下面的代码来生成excel,但如何将此excel传递给blobC# 将整个数据集转换为Excel,并使用EPPlus c将其存储在Azure blob中,c#,asp.net,azure,asp.net-core,C#,Asp.net,Azure,Asp.net Core,我使用了下面的代码来生成excel,但如何将此excel传递给blob using (DataSet ds = finalDataSetOutput) { if (ds != null && ds.Tables.Count > 0) { usin
using (DataSet ds = finalDataSetOutput)
{
if (ds != null && ds.Tables.Count > 0)
{
using (ExcelPackage pck = new ExcelPackage())
{
foreach (DataTable dt in ds.Tables)
{
ExcelWorksheet ws = pck.Workbook.Worksheets.Add(dt.TableName);
ws.Cells["A1"].LoadFromDataTable(dt, true);
int i = 1;
foreach (DataColumn dc in dt.Columns)
{
i++;
if (dc.DataType == typeof(decimal))
ws.Column(i).Style.Numberformat.Format = "#0.00";
}
}
pck.SaveAs(new FileInfo(@"D:\SampleExcels\NewFile.xlsx"));
}
}
}
如何创建excel并存储在azure blob中。我最近做了类似的事情,尽管我使用azure函数从集合而不是数据集填充excel数据。原理应该是一样的-只需连接到Blob容器并创建一个新的CloudBlockBlob: 注意:使用System.ComponentModel.Description装饰器将允许您在输出Excel中具有列标题。详情如下:
[FunctionName("WriteExcelToBlob")]
public async Task Run(
[TimerTrigger("*/30 * * * * *")] TimerInfo timer,
[Blob("excelFiles", FileAccess.Write, Connection = "Storage")] CloudBlobContainer blobContainer,
ILogger log
)
{
var fileNameSuffix = DateTime.Now.ToString("yyyyMMdd_HHmmss");
var myCollection = new List<MyObject>();
var newBlobName = $"myFile_{fileNameSuffix}.xlsx";
var newBlob = blobContainer.GetBlockBlobReference(newBlobName);
using (var excel = new ExcelPackage())
{
var worksheet = excel.Workbook.Worksheets.Add("My Worksheet");
worksheet.Cells.LoadFromCollection(myCollection);
using (var stream = await newBlob.OpenWriteAsync())
{
excel.SaveAs(stream);
}
}
}
using System.ComponentModel;
public class MyObject
{
[Description("Name")]
public string Name { get; set; }
[Description("Home Address")]
public string HomeAddress { get; set; }
}