Free Support Forum - aspose.com

Copying worksheets multiple times in c# code

Hello - I have two workbooks, NewWorkbook and TemplateWorkbook. My application has a button to Print All reports on the page. When iterating through the reports, I use TemplateWorkbook for the templates. For example, Report 1 uses Template 1, Report 2 uses Template 2 and Report 3 uses Template 1.

Here's my code:

Workbook NewWorkbook = new Workbook();
Workbook TemplateWorkbook = new Workbook(@"C:\Aspose\AsposeTemplates.xls");
NewWorkbook.Worksheets.Add("01A Sales");
NewWorkbook.Worksheets["01A Sales"].Copy(TemplateWorkbook.Worksheets["Template 1"]);
Worksheet worksheet;
worksheet = NewWorkbook.Worksheets["01A Sales"];
worksheet.Cells.ImportDataTable(a.Tables[0], false, 7, 0, true);
NewWorkbook.Worksheets.Add("02A Sales");
NewWorkbook.Worksheets["02A Sales"].Copy(TemplateWorkbook.Worksheets["Template 2"]); //***NullReferenceException - Object reference not set to an instance of an object.
worksheet = NewWorkbook.Worksheets["02A Sales"];
worksheet.Cells.ImportDataTable(a.Tables[1], false, 7, 0, true);
At the *** mark is where the code crashes and I get the Null Reference Exception.
Any suggesstions on how to fix this code ? I'm running this in Visual Studio 2010.
Funny thing is that it works just fine in Visual Studio 2008.

Hi,

That looks really strange that you only got issue/error in VS.NET 2010 and in VS.NET 2008, it works fine though. Aspose.Cells is a pure .NET class library/component that is written in managed C# and that does not depend on any specific VS.NET to work with it. I suspect, you might have different template files or codes or even different Aspose.Cells version in VS.NET 2010 project as compared to VS.NET 2008 project.

Anyways, please create a console application in VS.NET 2010, zip it and post it here, also attach the template files e.g “AsposeTemplates.xls” etc. to reproduce the issue on our end. We will check your issue soon.

Also, we recommend you to kindly try our the latest version/fix: Aspose.Cells for .NET (Latest Version)

Thank you.

Hello and thank you for the quick reply.

I found the solution to my problem as follows:

Instead of creating a new worksheet with the name and then trying to do a copy to that worksheet, I changed the process to add a worksheet with no parameters, do the copy, then change the name of the worksheet.

Here's the snippit of code that works:

NewWorkbook.Worksheets[0].Copy(TemplateWorkbook.Worksheets["Template 1"]);
NewWorkbook.Worksheets[0].Name = "01A Sales";
worksheet = NewWorkbook.Worksheets["01A Sales"];
worksheet.Cells.ImportDataTable(a.Tables[0], false, 7, 0, true);
NewWorkbook.Worksheets.Add();
NewWorkbook.Worksheets[1].Copy(TemplateWorkbook.Worksheets["Template 2"]);
NewWorkbook.Worksheets[1].Name = "02A Sales";
worksheet = NewWorkbook.Worksheets["02A Sales"];
worksheet.Cells.ImportDataTable(a.Tables[4], false, 7, 0, true);

Hi,


Good to know that your issue is sorted out.

Feel free to contact us any time if you need further help or have any other issue. We will be happy to assist you.

Thank you.