These examples show how to create a workbook, add data and meta data to the workbook, and save the workbook to an OGW file. After saving the workbook to an OGW file the workbook is destroyed. Then a new workbook is created using the OGW file. The data and meta data is read from the created workbook.
' This code references Origin or OrgLab8 and Microsoft XML v2.6 ' This example expects write access to C: ' If you do not have write access to C: then ' change the fileName assignment below to a drive with write access. Dim fileName As String fileName = "C:\testBook.ogw" ' Connect to Origin Dim orig As Origin.Application Set orig = New Origin.Application If orig Is Nothing Then MsgBox ("Failed to connect to Origin.") Exit Sub End If ' Create a new workbook Dim workBookName As String workBookName = orig.CreatePage(2, "", "Origin") ' Put data into the worksheet Dim i As Integer Dim data(1 To 32, 1 To 2) As Double For i = 1 To 32 data(i, 1) = i data(i, 2) = i * 1.012 Next If Not orig.PutWorksheet("", data) Then MsgBox ("Failed to put data into worksheet.") Exit Sub End If ' Create some metadata Dim xml As MSXML2.DOMDocument Set xml = New MSXML2.DOMDocument Dim xmlOrigin As MSXML2.IXMLDOMElement Set xmlOrigin = xml.appendChild(xml.createElement("OriginStorage")) Dim xmlData As MSXML2.IXMLDOMElement Set xmlData = xmlOrigin.appendChild(xml.createElement("Color")) xmlData.Text = "Blue" Set xmlData = xmlOrigin.appendChild(xml.createElement("Length")) xmlData.Text = "42" ' Put metadata into the workbook page Dim Workbook As Origin.WorksheetPage Set Workbook = orig.ActivePage If Workbook Is Nothing Then MsgBox ("Failed to get workbook.") Exit Sub End If If Not Workbook.SetMetaData(xmlOrigin.xml, "UserBookInfo", True) Then MsgBox ("Failed to set metadata into workbook.") Exit Sub End If ' Save the workbook as an OGW file ' No COM way to do this, use LabTalk orig.Execute ("save -i " + fileName) ' Destroy the workbook orig.DestroyPage (Workbook.Name) Set Workbook = Nothing ' Open the OGW file to create a new workbook If Not orig.Load(fileName, True) Then MsgBox ("Failed to load file.") Exit Sub End If ' Copy data from worksheet Dim dataGet As Variant dataGet = orig.GetWorksheet("", 0, 0, -1, -1, ARRAY2D_VARIANT) ' Put data into Excel worksheet Dim dataRows As Integer dataRows = UBound(dataGet) Range("A1").Resize(dataRows, 2) = dataGet ' Get metadata from workbook Set Workbook = orig.ActivePage If Workbook Is Nothing Then MsgBox ("Failed to get workbook from OGW.") Exit Sub End If xmlOrigin.Text = Workbook.GetMetaData("UserBookInfo", True) ' Put metadata into Excel worksheet ' Disconnect from Origin Set orig = Nothing
// This code references Origin or OrgLab8 and Microsoft XML v2.6 static void Main(string[] args) { // This example expects write access to C: // If you do not have write access to C: then // change the fileName assignment below to a drive with write access. string strFileName = "C:\\testBook.ogw"; // Connect to Origin or Orglab Origin.Application orgApp = new Origin.Application(); if (null == orgApp) { Console.WriteLine("Failed to connect to Origin or Orglab."); return; } CreateOGW(orgApp, strFileName); OpenOGW(orgApp, strFileName); Console.ReadKey(); // Disconnect from Origin orgApp = null; } static bool CreateOGW(Origin.Application orgApp, string strFileName) { // Create a new workbook and worksheet string strWBookName = orgApp.CreatePage((int)Origin.PAGETYPES.OPT_WORKSHEET, "OriginLab", "Origin", 2); if( "" == strWBookName ) { Console.WriteLine("Failed to create an Origin Workbook."); return false; } Origin.Worksheet wks = orgApp.FindWorksheet(""); // Use empty string to get active if (null == wks) { Console.WriteLine("Failed to find the newly created worksheet."); return false; } Origin.WorksheetPage wkbook = (Origin.WorksheetPage)wks.Parent; if (null == wkbook) { Console.WriteLine("Failed to get the newly created workbook."); return false; } // Put data into the worksheet int i; Double[] d1 = new double[32]; Double[] d2 = new double[32]; for( i = 0; i < 32; i++ ) { d1[i] = i; d2[i] = i * 1.012; } wks.Columns[0].SetData(d1, System.Type.Missing); //col (1) wks.Columns[1].SetData(d2, System.Type.Missing); //col (2) // Create XML meta-data to be put into workbook XmlDocument xmlDoc = new XmlDocument(); XmlElement elemRoot = xmlDoc.CreateElement("OriginStorage"); xmlDoc.AppendChild(elemRoot); XmlElement elem; elem = xmlDoc.CreateElement("Color"); elem.InnerText = "Blue"; elemRoot.AppendChild(elem); elem = xmlDoc.CreateElement("Length"); elem.InnerText = "42"; elemRoot.AppendChild(elem); // Put meta-data into workbook if (!wkbook.SetMetaData(elemRoot.OuterXml, "UserBookInfo", true)) { Console.WriteLine("Failed to set meta data into workbook."); return false; } // Save the workbook as an OGW file // No COM way to do this, use LabTalk orgApp.Execute("save -i " + strFileName, strWBookName); // Destroy the workbook wkbook.Destroy(); return true; } static bool OpenOGW(Origin.Application orgApp, string strFileName) { // Open the OGW file to create a new workbook if( !orgApp.Load(strFileName, true) ) { Console.WriteLine("Failed to load OGW file."); return false; } // Get the active workbook and it's active worksheet Origin.Worksheet wks = orgApp.FindWorksheet(""); // Use empty string to get active if (null == wks) { Console.WriteLine("Failed to find the newly created worksheet."); return false; } Origin.WorksheetPage wkbook = (Origin.WorksheetPage)wks.Parent; if (null == wkbook) { Console.WriteLine("Failed to get the newly created workbook."); return false; } // Get and output the data from the worksheet object do1 = wks.Columns[0].GetData(Origin.ARRAYDATAFORMAT.ARRAY1D_NUMERIC, 0, -1, 0); //col (1) object do2 = wks.Columns[1].GetData(Origin.ARRAYDATAFORMAT.ARRAY1D_NUMERIC, 0, -1, 0); //col (2) double[] d1 = do1 as double[]; double[] d2 = do2 as double[]; for (int i = 0; i < d1.GetUpperBound(0); i++) { Console.WriteLine(i + "\t" + d1[i] + "\t" + d2[i]); } // Get and output the meta data from the workbook string strXML = wkbook.GetMetaData("UserBookInfo", true); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(strXML); return true; }