Generally we can find code snippets related to Interop on internet easily. But I had a very unique requirement for which I could not find a code sample. So, had to create it and now sharing it with the community hoping it can help someone save sometime.
This code sample splits the worksheets in an Excel document to individual independent Excel files, and it works as follows:
BreakLinks: As we can create formulas in an Excel sheet. These formulas can be linked to other worksheets or to a different Excel file altogether. Splitting worksheet make those links not working, since referenced worksheet is now also split to a new Excel file, so cannot be referenced. So, following code break those links which internally replaces it with its value.
application.Workbooks[1].BreakLink(sourceFileName, XlLinkType.xlLinkTypeExcelLinks);
Splitting: Following code splits the worksheets in an Excel document
newWorkbook = application.Workbooks.Add(1);
((Worksheet)application.Workbooks[1].Sheets[counter]).Copy(((Worksheet)newWorkbook.Sheets[1]));
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment
It is the same account you read, post and publish with — and you will come straight back to this page.
Abhiram ThulasiPosted Apr 11, 2016, 4:03 AM
How can I convert this into 2 different XML files ?
sam smithPosted Aug 5, 2011, 10:28 AM
can you also post how to add more than 2 worksheets..?