1.6.1.2 Workbook Manipulation

Origin provides the capabilities for workbook manipulation by using Origin C, such as merging, splitting, etc.

Merge Workbooks

To merge many workbooks into one workbook, actually it is to copy the worksheets from the source workbooks to the target workbook. To add worksheet to a workbook, the AddLayer method is available.

The following example is to merge all workbooks in current folder to the newly created workbook.

WorksheetPage wksPgTarget;
wksPgTarget.Create("Origin");  // Create the target workbook
Folder fld = Project.ActiveFolder();  // Get the active/current folder	
foreach(PageBase pb in fld.Pages) 
{  // Loop all Pages in folder
	WorksheetPage wksPgSource = pb;  // Convert the Page to WorksheetPage
	// If convert failed, that is to say the Page is not WorksheetPage
	if(!wksPgSource) 
        {  
		continue;  // Next Page
	}
	// Skip the target workbook
	if(wksPgTarget.GetName() == wksPgSource.GetName()) 
        {
		continue;
	}
	// Loop all worksheet in workbook for merging
	foreach(Layer lay in wksPgSource.Layers) 
        {
		Worksheet wks = lay;  // Get the worksheet
		// Add the worksheet to target workbook
		wksPgTarget.AddLayer(wks, 0, false);  
	}
	// If not to keep the source workbook, destroy it
	wksPgSource.Destroy();
}

Split Workbook

The example above is merging multiple workbooks into one workbook. It is also able to split a workbook into multiple workbooks, which contain single worksheet.

WorksheetPage wksPgSource("Book1");  // Workbook with multiple worksheets
// Loop over all worksheets
foreach(Layer lay in wksPgSource.Layers) 
{
	Worksheet wks = lay;  // Get worksheet
	WorksheetPage wksPgTarget;
	wksPgTarget.Create("Origin");  // Create new workbook
	wksPgTarget.AddLayer(wks);  // Add worksheet to the new workbook
	wksPgTarget.Layers(0).Destroy();  // Delete the first worksheet
}