[英]Using ExcelPackage to Read from Excel Template and Write to Excel
I am using the following bit of code: 我正在使用以下代码:
var assembly = Assembly.GetExecutingAssembly();
Stream streamToTemplete=assembly.GetManifestResourceStream("DailyAuction.xltx");
Stream streamToOutput = assembly.GetManifestResourceStream("dailyAuctionEmail.xls");
//using (ExcelPackage excelPackage = new ExcelPackage(newFile, templateFile))
using(streamToTemplete)
using (streamToOutput)
{
using (ExcelPackage excelPackage = new ExcelPackage(streamToOutput, streamToTemplete))
{
ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets["Daily Auction"];
int startRow = 4;
int row = startRow;
foreach (DailyEmail dailyEmail in listDailyEmail) //iterate through all the rows in the list
{
worksheet.Cells[row, 1].Value = dailyEmail.As_At.ToString("dd-MMM-yy HH:mm");
worksheet.Cells[row, 2].Value = dailyEmail.Auction_Date.ToString("dd-MMM-yy");
}
excelPackage.Save();
}
}
var attachment = new Attachment(streamToOutput, new ContentType("application/vnd.ms-excel"));
However, I am getting an error saying streamToTemplate & streamToOutput are null. 但是,我收到一条错误消息,说streamToTemplate和streamToOutput为null。 What seems to be the problem?
似乎是什么问题? Also, will the rest of the code work?
另外,其余代码是否可以正常工作?
Check the properties on both the Excel files. 检查两个Excel文件上的属性。 Build Action should be 'Embedded Resource'.
构建操作应为“嵌入式资源”。
Also, the string that you are providing to GetManifestResource stream looks too simple. 另外,您提供给GetManifestResource流的字符串看起来也很简单。 The string should be:
该字符串应为:
<defaultNamespace>.<folderName>.<fileName>
MSDN Article <defaultNamespace>.<folderName>.<fileName>
MSDN文章
This answer suggests calling the GetManifestResourceNames, then using the debugger to take a look at the resource names. 该答案建议调用GetManifestResourceNames,然后使用调试器查看资源名称。 Seems a good way to go while you're finding your way.
在寻找自己的路途时,似乎是一个不错的路要走。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.