简体   繁体   English

使用ExcelPackage从Excel模板读取并写入Excel

[英]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.

相关问题 MVC 5如何使用EPPlus(OfficeOpenXml)ExcelPackage上传Excel文件并读取行,直到最后填充的数据行 - MVC 5 How to upload Excel file and read rows till the last populated data row using EPPlus (OfficeOpenXml) ExcelPackage 使用C#中的ExcelPackage将XML文件导入到具有现有地图的excel中 - Import XML file to excel with existing map using ExcelPackage in C# .NET C#使用ExcelPackage通过JS post导出到excel - .NET C# exporting to excel via JS post using ExcelPackage 错误:使用EPPLUS dll导出到excel时未定义类型&#39;ExcelPackage&#39; - Error: Type 'ExcelPackage' is not defined while using EPPLUS dll to export to excel 使用Aspose读取Excel模板 - Read Excel template using Aspose 从Excel读取数据并使用Specflow写入功能文件 - Read data from excel and write to feature file using specflow 使用C#从Excel电子表格中读取/写入 - To read/write from excel spreadsheet using C# C#如何使用Interop从Excel读取和写入数据,而不显示Excel? - C# How to read and write data from Excel using Interop, without making Excel visible? 设置使用ExcelPackage创建的Excel单元格的货币格式 - Set currency format for Excel cell that is created with ExcelPackage 如何从excel模板中读取数据 - How to read data from excel template
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM