Spire.XLS is a professional Excel API that enables developers to create, manage, manipulate, convert and print Excel worksheets. Get free and professional technical support for Spire.XLS for .NET, Java, Android, C++, Python.

Wed Apr 09, 2014 1:00 pm

Is it possible to copy one workbook to another? I haven't found such opportunity.
I tried to use MemoryStream for it (saving to it and loading another workbook from it), but I got ArgumentNullException:
"Value cannot be null. Parameter name: root"

Thanks

user001
 
Posts: 5
Joined: Wed Apr 09, 2014 12:54 pm

Thu Apr 10, 2014 2:18 am

Hello,

Thanks for your inquiry.
You can do it by copying the worksheet, there are some codes for your reference.
Code: Select all
Workbook original = new Workbook();
original.LoadFromFile("original.xlsx");
Workbook target = new Workbook();
target.LoadFromFile("target.xlsx");
foreach (Worksheet sheet in original.Worksheets)
            {
                target.Worksheets.AddCopy(sheet);
            }
target.SaveToFile("Result.xlsx",ExcelVersion.Version2010);

If the issue persists, could you please provide us your document for testing?
Thanks,
Gary
E-iceblue support team
User avatar

Gary.zhang
 
Posts: 1380
Joined: Thu Apr 04, 2013 1:30 am

Thu Apr 10, 2014 7:32 am

Thank you very much for quick response!

I have one input file and I need to produce lots of output files with different values. But I don't want each time read input file from disk. I want smth like:
  • 1. read original workbook from file
  • 2. copy original to current
  • 3. populate current with data
  • 4. save current to file
And I need repeat 2-4 many times. So your solution is not very suitable in this situation.

I created empty Book in Excel and tried to copy it like this:
Code: Select all
var original = new Workbook();
original.LoadFromFile("Book1.xlsx");
var target = new Workbook();

using (var stream = new MemoryStream())
{
    original.SaveToStream(stream, FileFormat.Version2010);
    stream.Seek(0, SeekOrigin.Begin);
    target.LoadFromStream(stream);
}

But it doesn't work. I get ArgumentNullException.
Do you have another suggestions about how I can do this?

Thank you

user001
 
Posts: 5
Joined: Wed Apr 09, 2014 12:54 pm

Thu Apr 10, 2014 8:27 am

Hello,

Thanks for the reply.
Please add the ExcelVersion when loading.
Code: Select all
target.LoadFromStream(stream,ExcelVersion.Version2010);

If there are any questions, welcome to get it back to us.
Sincerely,
Gary
E-iceblue support team
User avatar

Gary.zhang
 
Posts: 1380
Joined: Thu Apr 04, 2013 1:30 am

Thu Apr 10, 2014 11:48 am

Hi,

Now it works fine:
Code: Select all
var original = new Workbook();
original.LoadFromFile("Book1.xlsx");
var target = new Workbook();

using (var stream = new MemoryStream())
{
    original.SaveToStream(stream, FileFormat.Version2010);
    stream.Seek(0, SeekOrigin.Begin);
    target.LoadFromStream(stream, ExcelVersion.Version2010);
}

Thanks a lot.

user001
 
Posts: 5
Joined: Wed Apr 09, 2014 12:54 pm

Fri Apr 11, 2014 1:45 am

Glad to hear that.

If there are any questions, welcome to get it back to us.
Sincerely,
Gary
E-iceblue support team
User avatar

Gary.zhang
 
Posts: 1380
Joined: Thu Apr 04, 2013 1:30 am

Return to Spire.XLS