我正在使用以下代码:
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"));
但是,我收到一个错误说streamToTemplate& streamToOutput为null。什么似乎是问题?此外,其余代码是否有效?
答案 0 :(得分:1)
检查两个Excel文件的属性。 Build Action应该是'Embedded Resource'。
此外,您提供给GetManifestResource流的字符串看起来太简单了。字符串应为:
<defaultNamespace>.<folderName>.<fileName>
MSDN Article
This answer建议调用GetManifestResourceNames,然后使用调试器查看资源名称。当你找到自己的方式时,这似乎是一个很好的方式。