Split worksheets to individual Excel files using Interop


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]));

 
Ebook Download
View all
Learn
View all