0
using Syncfusion.XlsIO;
using System.IO;
using System.Reflection;

namespace MergeExcelFiles
{
    class Program
    {
        static void Main(string[] args)
        {
            //Initialize ExcelEngine
            using (ExcelEngine excelEngine = new ExcelEngine())
            {
                //Initialize Application
                IApplication application = excelEngine.Excel;

                //Set default application version
                application.DefaultVersion = ExcelVersion.Excel2013;

                //Open existing workbooks with data entered
                Assembly assembly = typeof(Program).GetTypeInfo().Assembly;

                Stream fileStream1 = assembly.GetManifestResourceStream("OrderStatus1.xlsx");
                IWorkbook workbook1 = application.Workbooks.Open(fileStream1);
                IWorksheet worksheet = workbook1.Worksheets[0];

                Stream fileStream2 = assembly.GetManifestResourceStream("OrderStatus2.xlsx");
                IWorkbook workbook2 = application.Workbooks.Open(fileStream2);

                Stream fileStream3 = assembly.GetManifestResourceStream("OrderStatus3.xlsx");
                IWorkbook workbook3 = application.Workbooks.Open(fileStream3);

                //The content in the range A1:C8 from workbook2 and workbook3 is copied to workbook1
                int lastRow = worksheet.UsedRange.LastRow;
                workbook2.Worksheets[0].UsedRange.CopyTo(worksheet.Range[lastRow + 1, 1]);

                lastRow = worksheet.UsedRange.LastRow;
                workbook3.Worksheets[0].UsedRange.CopyTo(worksheet.Range[lastRow + 1, 1]);

                worksheet.UsedRange.AutofitColumns();

                //Save the workbook
                workbook1.SaveAs("Output.xlsx");
            }
        }
    }
}

Error Message:

Unhandled Exception: System.ArgumentNullException: Value cannot be null. Parameter name: stream at Syncfusion.XlsIO.Implementation.Collections.WorkbooksCollection.Open(Stream stream, ExcelOpenType openType) at Syncfusion.XlsIO.Implementation.Collections.WorkbooksCollection.Open(Stream stream) at MergeExcelFiles.Program.Main(String[] args) in C:\Users\test\Desktop\MergeExcelFiles\MergeExcelFiles\Program.cs:line 24

Note: I have set the build action for all the files as Embedded Resource. These files contain thousands of rows. Will that be an issue, causing this error? The same code is working fine with different files.

  • When you paused the code in the debugger and I sorted the lobe of code that threw the error, did you find the stream to be null as the error claimed? When you traced the stream back to where it came from, what did you discover about why it was null? – Caius Jard Jul 17 '20 at 18:40
  • You should put a breakpoint in each filestream and check whether they actually get a stream or not. By the code you are providing the problem is probably in the first file OrderStatus1.xlsx. Check if that file is in the same path that your c# console app is – nalnpir Jul 17 '20 at 18:47
  • I am getting stream as null. File is available in the same path – Dinesh babu Jul 17 '20 at 19:23

1 Answers1

1

If you use the excel files as an embedded resource you should look where you have place it in your project. If you place the file in the root of the prject you should load the file in this way:

Stream fileStream1 = assembly.GetManifestResourceStream("MergeExcelFiles.OrderStatus1.xlsx");

and if you have place it in an subfolder do something like this:

assembly.GetManifestResourceStream("MergeExcelFiles.Subfolder.OrderStatus1.xlsx");

This link to a stackoverflow post have a good answer, some more information to the problem and hopefully helps you: How to read embedded resource text file

kanukiesel
  • 175
  • 7