我有以下方法:
public byte[] CreateBatchFile(string batchId, bool isBatchCandidate, string providerId)
{
var currentDate = DateTime.Now;
var fileName = $"B{batchId}_{providerId}_{"Upgrade"}_{currentDate.Date.ToString().Replace("\\", " ")}_{currentDate.ToShortTimeString().Replace(":", " ")}";
var agreementItems = this.GetMTNRenewalBatchAgreementItems(batchId, isBatchCandidate, providerId);
var columnHeadings = new List<string>()
{
"MSISDN", "SIM No", "Dealer Code", "Date", "Package Code", "Package Tariff Code", "Action requested", "MTN Done", "MTN Comments"
};
Application excel = new Application();
excel.Visible = false;
excel.DisplayAlerts = false;
Workbook workbook = excel.Workbooks.Add(Type.Missing);
Worksheet worksheet = (Worksheet)workbook.ActiveSheet;
worksheet.Name = "Renewal Request";
worksheet.Cells.Font.Size = 15;
//add column headings
for (int i = 1; i <= columnHeadings.Count; i++)
{
worksheet.Cells[1, i] = columnHeadings[i - 1];
}
//row indexer
int rowCounter = 2;
//add row data
for (int i = 1; i <= agreementItems.Count; i++)
{
//get the actual agreement item
var item = agreementItems[i - 1];
worksheet.Cells[rowCounter, i] = item.Msisdn;
worksheet.Cells[rowCounter, i] = item.SimNumber;
worksheet.Cells[rowCounter, i] = currentDate;
worksheet.Cells[rowCounter, i] = item.ProductCode;
worksheet.Cells[rowCounter, i] = item.ProviderProductIdentifier;
worksheet.Cells[rowCounter, i] = "Upgrade";
worksheet.Cells[rowCounter, i] = "";
worksheet.Cells[rowCounter, i] = "";
rowCounter++;
}
//add the worksheet to the workbook
workbook.Worksheets.Add(worksheet);
MemoryStream ms = new MemoryStream();
workbook.SaveAs(ms);
ms.Seek(0, SeekOrigin.Begin);
byte[] buffer = new byte[(int)ms.Length];
buffer = ms.ToArray();
workbook.Close();
excel.Quit();
return buffer;
}
我希望此方法返回一个byte[]
,但是问题是,当我将工作簿保存到MemoryStream
时,MemoryStream.Length
总是显示0。
我在做什么错?