0

I want to Copy Sheet from excel, create copy of sheet with particular name.

 Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook(excelFilePath);
                //Create a Worksheets object with reference to the sheets of the Workbook.
                WorksheetCollection sheets = workbook.Worksheets;
                sheets.AddCopy("Cash Bonuses"); 

Now the problem is it copies data of Sheet "Cash Bonuses" but it makes Sheet name as "Sheet111". I want to make this sheet with specified name like "Cash".How to do that ? Once data is copied to new tab , i want to delete old tab "Cash Bonuses" and rename new tab as "Cash bonuses" from "Cash".

Bokambo
  • 4,204
  • 27
  • 79
  • 130

1 Answers1

1

Please note, in order to copy the contents of a worksheet to another worksheet, you need to add a blank worksheet to the collection and then call its Copy method while passing the object of existing worksheet (one that needs to be copied) otherwise you will lose data on the destination worksheet.

Please try the following piece of code as it tries to accomplish all your requirements. Hopefully, the comments will help you understand what the statements mean.

var workbook = new Aspose.Cells.Workbook(excelFilePath);
var sheets = workbook.Worksheets;
//Access 1st worksheet from the collection
//You may also pass the worksheet name to access a particular worksheet
var sheet0 = sheets[0];
//Add a new worksheet to the collection and name it as desired
var sheet1 = sheets[sheets.Add()];
sheet1.Name = "Cash";
//Copy the contents of 1st worksheet onto the new worksheet
sheet1.Copy(sheet0);
//Delete 1st worksheet
sheets.RemoveAt(sheet0.Index);
//Rename newly added worksheet to 'Cash bonuses'
sheet1.Name = "Cash bonuses";
//Save result
workbook.Save(dir + "output.xlsx");

Note: I work with Aspose as Developer Evangelist.

Prorata
  • 483
  • 3
  • 10
  • Thanks..i will surely try out your solution...but the problem is one of the sheets in my excel is giving generic error "index out of bound exception" while converting excel to pdf. I am not able to figure out what is the exact error as soon as i do workbook.Save it gives me this error.If a sheet as checkbox control while converting from excel to pdf using aspose , does it gives error. I am using Aspose 8.5.2.0 version.I was not able to find exact error thats why i was tring workaround solution mentioned above. – Bokambo Oct 16 '16 at 14:08
  • Can i get the code to find which cell in excel is causing this error "index out of bound exception" ? – Bokambo Oct 17 '16 at 04:04
  • I am afraid, there isn't a code snippet to find out the problem cause, however, I can suggest you to give the latest version of Aspose.Cells for .NET 16.10.0 (available in Aspose download section & via NuGet) a try for the recent concerns. In case the problem persists, we need to review your sample to determine the cause of exception therefore, please create a new thread in Aspose.Cells support forum and share your sample along with code snippet to replicate the said exception. – Prorata Oct 17 '16 at 04:54