<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>finaquant.com</title>
	<atom:link href="http://finaquant.com/feed" rel="self" type="application/rss+xml" />
	<link>http://finaquant.com</link>
	<description>Innovation in Business Analytics - Calculation Engines &#38; Frameworks</description>
	<lastBuildDate>Thu, 13 Jun 2013 16:48:06 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Calling table functions from Microsoft Excel</title>
		<link>http://finaquant.com/calling-table-functions-from-microsoft-excel/3487?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=calling-table-functions-from-microsoft-excel</link>
		<comments>http://finaquant.com/calling-table-functions-from-microsoft-excel/3487#comments</comments>
		<pubDate>Sun, 26 May 2013 11:34:31 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Calculation engine]]></category>
		<category><![CDATA[.net library]]></category>
		<category><![CDATA[excel]]></category>
		<category><![CDATA[integration]]></category>
		<category><![CDATA[table functions]]></category>

		<guid isPermaLink="false">http://finaquant.com/?p=3487</guid>
		<description><![CDATA[This article explains how the table functions of the non-commercial .net library finaquant® protos can be called from within Microsoft Excel using VBA macros. Two possible integration options are presented here: 1) Calling a .net method from an Excel macro &#8230; <a href="http://finaquant.com/calling-table-functions-from-microsoft-excel/3487">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>This article explains how the table functions of the non-commercial .net library <a href="http://finaquant.com/products/finaquantprotos" title="finaquant® protos – free .net library with table functions" target="_blank">finaquant® protos</a> can be called from within Microsoft Excel using VBA macros.</p>
<p><img src="http://finaquant.com/wp-content/uploads/2013/05/CallingNETFromExcel_VisualStudio.png" alt="Calling .NET from Excel" width="600" height="306" class="alignnone size-full wp-image-3500" /></p>
<p>Two possible integration options are presented here:</p>
<p><strong>1) Calling a .net method from an Excel macro</strong><br />
Once called from a macro, the .net method reads the input tables from the given excel worksheets before executing the table functions to obtain output tables. The output tables are in turn written into the desired excel sheets.</p>
<p>Note that names of the worksheets are passed from the macro to the .net method as parameters for selecting the input tables. Each worksheet contains a single input table. </p>
<p>This integration option can be useful for demonstrations that are executed directly from excel. See the code examples below:</p>
<span class="collapseomatic" id="id9471"  title="Excel VBA macro calling .net method: CalculatePrices()">Excel VBA macro calling .net method: CalculatePrices()</span><div id="target-id9471" class="collapseomatic_content">

<div class="wp_syntax"><table><tr><td class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #008000;">' Calculate price table with given cost and margin tables
</span><span style="color: #008000;">' 1) Read input tables: Costs, Margins, MetaData
</span><span style="color: #008000;">' 2) Calculate price table
</span><span style="color: #008000;">' 3) Write resultant price table into a sheet
</span><span style="color: #E56717; font-weight: bold;">Private</span> <span style="color: #E56717; font-weight: bold;">Sub</span> CalculatePrices()
<span style="color: #151B8D; font-weight: bold;">Dim</span> pxl <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #E56717; font-weight: bold;">New</span> ProtosExcelFunc
<span style="color: #151B8D; font-weight: bold;">Dim</span> CostTableSheet, MarginTableSheet, MetaDataSheet, PriceTableSheet <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">String</span>
&nbsp;
CostTableSheet = <span style="color: #800000;">&quot;CostTable&quot;</span>
MetaDataSheet = <span style="color: #800000;">&quot;MetaData&quot;</span>
&nbsp;
MarginTableSheet = <span style="color: #800000;">&quot;MarginTable1&quot;</span>
PriceTableSheet = <span style="color: #800000;">&quot;PriceTable1&quot;</span>
&nbsp;
<span style="color: #008000;">'MarginTableSheet = &quot;MarginTable2&quot;
</span><span style="color: #008000;">'PriceTableSheet = &quot;PriceTable2&quot;
</span>
<span style="color: #008000;">' call .net method
</span>MsgBox pxl.CalculatePrices(Application.ActiveWorkbook.FullName, CostTableSheet, _
    MarginTableSheet, MetaDataSheet, PriceTableSheet)
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Sub</span></pre></td></tr></table></div>

<p></div>&nbsp;</p>
<span class="collapseomatic" id="id5076"  title=".net method called by Excel macro: CalculatePrices()">.net method called by Excel macro: CalculatePrices()</span><div id="target-id5076" class="collapseomatic_content">

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Calculate price table for given cost and margin tables</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;ExcelFilePath&quot;&gt;Full file path of excel file&lt;/param&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;CostTableSheet&quot;&gt;Name of worksheet for reading cost table (input table)&lt;/param&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;MarginTableSheet&quot;&gt;Name of worksheet for reading margin table (input table)&lt;/param&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;MetaDataSheet&quot;&gt;Name of worksheet for reading field names and types (input table)&lt;/param&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;PriceTableSheet&quot;&gt;Name of target worksheet for writing price table (output table)&lt;/param&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;returns&gt;Message to calling function&lt;/returns&gt;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> CalculatePrices<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> ExcelFilePath, <span style="color: #6666cc; font-weight: bold;">string</span> CostTableSheet, <span style="color: #6666cc; font-weight: bold;">string</span> MarginTableSheet,
	<span style="color: #6666cc; font-weight: bold;">string</span> MetaDataSheet, <span style="color: #6666cc; font-weight: bold;">string</span> PriceTableSheet<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0600FF; font-weight: bold;">try</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #008080; font-style: italic;">// read meta data</span>
		DataTable MetaTbl <span style="color: #008000;">=</span> ExcelFunc<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadTableFromExcelSheet_ODBC</span><span style="color: #008000;">&#40;</span>ExcelFilePath, MetaDataSheet<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		MetaData md <span style="color: #008000;">=</span> MetaData<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadFieldsFromDataTable</span><span style="color: #008000;">&#40;</span>MetaTbl<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">// read input tables</span>
		DataTable CostTbl <span style="color: #008000;">=</span> ExcelFunc<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadTableFromExcelSheet_ODBC</span><span style="color: #008000;">&#40;</span>ExcelFilePath, CostTableSheet<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		DataTable MarginTbl <span style="color: #008000;">=</span> ExcelFunc<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadTableFromExcelSheet_ODBC</span><span style="color: #008000;">&#40;</span>ExcelFilePath, MarginTableSheet<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">// convert DataTable to MatrixTable</span>
		MatrixTable CostTable <span style="color: #008000;">=</span> MatrixTable<span style="color: #008000;">.</span><span style="color: #0000FF;">Import_from_DataTable</span><span style="color: #008000;">&#40;</span>CostTbl, md<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		MatrixTable MarginTable <span style="color: #008000;">=</span> MatrixTable<span style="color: #008000;">.</span><span style="color: #0000FF;">Import_from_DataTable</span><span style="color: #008000;">&#40;</span>MarginTbl, md<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">// Calculate price table with table function 'MultiplySelectedKeyFigures'</span>
		MatrixTable PriceTable <span style="color: #008000;">=</span> MatrixTable<span style="color: #008000;">.</span><span style="color: #0000FF;">MultiplySelectedKeyFigures</span><span style="color: #008000;">&#40;</span>CostTable, <span style="color: #008000;">&#40;</span>MarginTable <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span>,
		InputKeyFigTbl1<span style="color: #008000;">:</span> <span style="color: #666666;">&quot;costs&quot;</span>, InputKeyFigTbl2<span style="color: #008000;">:</span> <span style="color: #666666;">&quot;margin&quot;</span>, OutputKeyFig<span style="color: #008000;">:</span> <span style="color: #666666;">&quot;price&quot;</span>,
		JokerMatchesAllvalues<span style="color: #008000;">:</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">// convert MatrixTable to DataTable</span>
		DataTable PriceTbl <span style="color: #008000;">=</span> MatrixTable<span style="color: #008000;">.</span><span style="color: #0000FF;">Export_To_DataTable</span><span style="color: #008000;">&#40;</span>PriceTable<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">// write price table into excel sheet</span>
		ExcelFunc<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteTableToExcelSheet</span><span style="color: #008000;">&#40;</span>PriceTbl, ExcelFilePath, SheetName<span style="color: #008000;">:</span> PriceTableSheet, SaveWorkbook<span style="color: #008000;">:</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">// return affirmative message to caller</span>
		<span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #666666;">&quot;Price table is calculated successfully!&quot;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>Exception ex<span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #666666;">&quot;CalculatePrices: An error has occured! &quot;</span> <span style="color: #008000;">+</span> ex<span style="color: #008000;">.</span><span style="color: #0000FF;">Message</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p></div>&nbsp;</p>
<p><strong>2) Directly executing the .net method </strong><br />
&#8230; which reads input tables from the given excel file before making the required table calculations.</p>
<p>In this option, an excel sheets are used solely as a database to store input and output tables; no additional VBA code (Excel macro) is needed. See the code example below:</p>
<span class="collapseomatic" id="id7365"  title=".net code for calculating prices: CalculatePriceTable()">.net code for calculating prices: CalculatePriceTable()</span><div id="target-id7365" class="collapseomatic_content">

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Calculate price table with given cost and margin tables</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> CalculatePriceTable<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #008080; font-style: italic;">// Get full file path</span>
	<span style="color: #6666cc; font-weight: bold;">string</span> filepath <span style="color: #008000;">=</span> <span style="color: #000000;">System.<span style="color: #0000FF;">IO</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Path</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetFullPath</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">@&quot;Resources\DotNetIntegration.xlsm&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// call table function</span>
	ProtosExcelFunc pxl <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ProtosExcelFunc<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #6666cc; font-weight: bold;">string</span> msg <span style="color: #008000;">=</span> pxl<span style="color: #008000;">.</span><span style="color: #0000FF;">CalculatePrices</span><span style="color: #008000;">&#40;</span>filepath, CostTableSheet<span style="color: #008000;">:</span> <span style="color: #666666;">&quot;CostTable&quot;</span>, MarginTableSheet<span style="color: #008000;">:</span> <span style="color: #666666;">&quot;MarginTable1&quot;</span>,
		MetaDataSheet<span style="color: #008000;">:</span> <span style="color: #666666;">&quot;MetaData&quot;</span>, PriceTableSheet<span style="color: #008000;">:</span> <span style="color: #666666;">&quot;PriceTable1&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #000000;">System.<span style="color: #0000FF;">Diagnostics</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Debug</span><span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Message from CalculatePrices: &quot;</span> <span style="color: #008000;">+</span> msg<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p></div>&nbsp;</p>
<p>All the example codes above and others can be found in <a href="http://finaquant.com/download_wdm/30" title="Microsoft Visual Studio file ProtosExcelIntegration including a macro-enabled Excel file for demonstrating excel-.net integration (finaquant protos and excel), version: 25 May 2013, R1.04">MS Visual Studio File ProtosExcelIntegration 1.04</a>.<span id="more-3487"></span></p>
<h3>Calling a .net method from an Excel macro</h3>
<ol>
<li>Install the latest release of the non-commercial .net library <a href="http://finaquant.com/products/finaquantprotos" title="finaquant® protos – free .net library with table functions" target="_blank">finaquant® protos</a> on your computer.</li>
<li>Ensure that the proper ODBC driver is installed on your computer. See <a href="http://finaquant.com/importing-and-exporting-a-datatable-from-an-excel-file/3456" title="Importing and Exporting a DataTable from/to an Excel file" target="_blank">related article</a> for more information.</li>
<li>Download and unzip the latest release of <a href="http://finaquant.com/download_wdm/30" title="Microsoft Visual Studio file ProtosExcelIntegration including a macro-enabled Excel file for demonstrating excel-.net integration (finaquant protos and excel), version: 25 May 2013, R1.04">MS Visual Studio File ProtosExcelIntegration 1.04</a></li>
<li>Open the VS file ProtosExcelIntegration as administrator: Find the application (Visual Studio) in program starter menu, right mouse-click and select Run as administrator.</li>
<p><a href="http://finaquant.com/wp-content/uploads/2013/05/CallingNETFromExcel_RunAsAdmin.png" rel="lightbox[3487]" title="Calling table functions from Microsoft Excel"><img src="http://finaquant.com/wp-content/uploads/2013/05/CallingNETFromExcel_RunAsAdmin-285x300.png" alt="Run Visual Studio as Administrator" width="285" height="300" class="alignnone size-medium wp-image-3502" /></a></p>
<li>Ensure that Microsoft Office and Excel Object libraries are referenced by the VS project. You need to update references if you have a Microsoft Office version other than 2010.</li>
<p><a href="http://finaquant.com/wp-content/uploads/2013/05/CallingNETFromExcel_ExcelReferences.png" rel="lightbox[3487]" title="Calling table functions from Microsoft Excel"><img src="http://finaquant.com/wp-content/uploads/2013/05/CallingNETFromExcel_ExcelReferences-300x158.png" alt="VS Project References" width="300" height="158" class="alignnone size-medium wp-image-3504" /></a></p>
<li>Ensure that following VS project configurations are selected:</li>
<p>Application>Output type: Class Library<br />
<a href="http://finaquant.com/wp-content/uploads/2013/05/CallingNETFromExcel_ClassLibrary.png" rel="lightbox[3487]" title="Calling table functions from Microsoft Excel"><img src="http://finaquant.com/wp-content/uploads/2013/05/CallingNETFromExcel_ClassLibrary-300x130.png" alt="Output type: Class Library" width="300" height="130" class="alignnone size-medium wp-image-3505" /></a></p>
<p>Build>Output: Check Register for COM interop<br />
<a href="http://finaquant.com/wp-content/uploads/2013/05/CallingNETFromExcel_RegisterCOM.png" rel="lightbox[3487]" title="Calling table functions from Microsoft Excel"><img src="http://finaquant.com/wp-content/uploads/2013/05/CallingNETFromExcel_RegisterCOM-300x93.png" alt="Output: Check Register for COM interop" width="300" height="93" class="alignnone size-medium wp-image-3506" /></a></p>
<li>Build (or rebuild) the VS project.</li>
<p><a href="http://finaquant.com/wp-content/uploads/2013/05/CallingNETFromExcel_BuildProject.png" rel="lightbox[3487]" title="Calling table functions from Microsoft Excel"><img src="http://finaquant.com/wp-content/uploads/2013/05/CallingNETFromExcel_BuildProject-300x140.png" alt="Build (or rebuild) the VS project" width="300" height="140" class="alignnone size-medium wp-image-3510" /></a></p>
<li>Add library COM reference to Excel: Open the excel file under the folder <em>Resources</em>, go to macro VBA view, add reference named Finaquant Protos and Excel Integration from the menu <em>Tools>References</em></li>
<p><img src="http://finaquant.com/wp-content/uploads/2013/05/CallingNETFromExcel_AddCOMReferenceToExcel.png" alt="Add COM reference to Excel" width="438" height="286" class="alignnone size-full wp-image-3513" /></p>
</ol>
<p>You can now open the excel file under the folder <em>Resources</em> and execute some macros (by pressing F5) that call .net methods for table functions. The VBA macros can be found in <em>Module1</em> in developer view:<br />
<img src="http://finaquant.com/wp-content/uploads/2013/05/CallingNETFromExcel_VBAmacros.png" alt="Excel VBA macros" width="620" height="290" class="alignnone size-full wp-image-3515" /></p>
<p>You can also execute a macro (CalculatePrices2 in Module1) by selecting the input tables (by their worksheet names) and pressing a button in the worksheet named <em>RunMacro</em>:<br />
<img src="http://finaquant.com/wp-content/uploads/2013/05/CallingNETFromExcel_PressButton.png" alt="Calling .net from excel by pressing a button" width="442" height="253" class="alignnone size-full wp-image-3518" /></p>
<p>Resultant price table is written into a new worksheet named <em>ProceTable1</em>:<br />
<a href="http://finaquant.com/wp-content/uploads/2013/05/CallingNETFromExcel_ResultantPriceTable.png" rel="lightbox[3487]" title="Calling table functions from Microsoft Excel"><img src="http://finaquant.com/wp-content/uploads/2013/05/CallingNETFromExcel_ResultantPriceTable.png" alt="Resultant price table" width="497" height="321" class="alignnone size-full wp-image-3520" /></a></p>
<p>This integration option is particularly useful for demonstrations: Users can first enter the calculation parameters (like sheet names for selecting the input tables here) and execute their table calculation by just pressing a button.</p>
<h3>Directly executing the .net method</h3>
<ol>
<li>Open the VS file ProtosExcelIntegration as normal user; you don’t need to open it as administrator.</li>
<li>Ensure that following VS project configurations are selected:<br />
Build>Output: Uncheck Register for COM interop<br />
Application>Output type: Select Console Application</li>
<li>Uncomment the test method you want to execute in code file Program.cs and press F5</li>
<p><img src="http://finaquant.com/wp-content/uploads/2013/05/CallingNETFromExcel_CombineTables.png" alt="Directly executing .net method" width="620" height="234" class="alignnone size-full wp-image-3522" />
</ol>
<p>Resultant table is written into a new worksheet named <em>CombTable2</em>:<br />
<img src="http://finaquant.com/wp-content/uploads/2013/05/CallingNETFromExcel_ResultantCombinedTable.png" alt="Resultant Combined Table" width="486" height="320" class="alignnone size-full wp-image-3523" /></p>
<span id="dprv_cp-v2.13" lang="en" xml:lang="en" class="notranslate" style="vertical-align:baseline; padding: 3px 3px 3px 3px; margin-top:2px; margin-bottom:2px; border-collapse:separate; line-height:16px;float:none; font-family: Tahoma, MS Sans Serif; font-size:13px;border:1px solid #bbbbbb;background:#FFFFFF none;display:inline-block;" title="certified 26 May 2013 13:57:59 UTC by Digiprove certificate P405672" ><a href="http://www.digiprove.com/prove_compliance.aspx?id=P405672%26guid=4wIFgf67PEK1BOlv9ll8pg" target="_blank" rel="copyright" style="height:16px; line-height: 16px; border:0px; padding:0px; margin:0px; float:none; display:inline; text-decoration: none; background:transparent none; line-height:normal; font-family: Tahoma, MS Sans Serif; font-style:normal; font-weight:normal; font-size:11px;"><img src="http://finaquant.com/wp-content/plugins/digiproveblog/dp_seal_trans_16x16.png" style="max-width:none !important;vertical-align:-3px; display:inline; border:0px; margin:0px; padding:0px; float:none; background:transparent none" border="0" alt=""/><span style="font-family: Tahoma, MS Sans Serif; font-style:normal; font-size:11px; font-weight:normal; color:#636363; border:0px; float:none; display:inline; text-decoration:none; letter-spacing:normal; padding:0px; padding-left:8px; vertical-align:1px;margin-bottom:2px" onmouseover="this.style.color='#A35353';" onmouseout="this.style.color='#636363';">Copyright&nbsp;secured&nbsp;by&nbsp;Digiprove&nbsp;&copy;&nbsp;2013&nbsp;Tunc&nbsp;Ali&nbsp;Kütükcüoglu</span></a><!--C0A5BD58EE7A4F5C172B5B0B59EC13D6B29E665E7AB2B945F282302CD7506E17--></span>]]></content:encoded>
			<wfw:commentRss>http://finaquant.com/calling-table-functions-from-microsoft-excel/3487/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Importing and Exporting a DataTable from/to an Excel file</title>
		<link>http://finaquant.com/importing-and-exporting-a-datatable-from-an-excel-file/3456?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=importing-and-exporting-a-datatable-from-an-excel-file</link>
		<comments>http://finaquant.com/importing-and-exporting-a-datatable-from-an-excel-file/3456#comments</comments>
		<pubDate>Thu, 02 May 2013 14:47:35 +0000</pubDate>
		<dc:creator>tuncalik</dc:creator>
				<category><![CDATA[Calculation engine]]></category>
		<category><![CDATA[DataTable]]></category>
		<category><![CDATA[excel]]></category>
		<category><![CDATA[integration]]></category>

		<guid isPermaLink="false">http://finaquant.com/?p=3456</guid>
		<description><![CDATA[As explained in the related article, a MatrixTable of finaquant can easily be converted to or from a DataTable of the .net framework (ADO.net). That is, importing or exporting a MatrixTable from/to an excel sheet can be accomplished in two &#8230; <a href="http://finaquant.com/importing-and-exporting-a-datatable-from-an-excel-file/3456">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>As explained in the <a href="http://finaquant.com/converting-a-matrixtable-to-a-datatable-and-vice-versa/3452" title="Converting a MatrixTable to a DataTable, and vice versa" target="_blank">related article</a>, a MatrixTable of finaquant can easily be converted to or from a <a href="http://msdn.microsoft.com/en-us/library/system.data.datatable.aspx" title="DataTable Class" target="_blank">DataTable</a> of the .net framework (ADO.net).</p>
<p><img src="http://finaquant.com/wp-content/uploads/2013/05/Excel_DataTable_ExcelDataMatrixTable.png" alt="Data integration of excel with .net framework" width="595" height="108" class="alignnone size-full wp-image-3461" /></p>
<p>That is, importing or exporting a MatrixTable from/to an excel sheet can be accomplished in two steps:</p>
<p><em>Import:</em> (1) From Excel to DataTable (2) From DataTable to MatrixTable</p>
<p><em>Export:</em> (1) From MatrixTable to DataTable (2) From DataTable to Excel</p>
<h2>Data integration of .net with excel</h2>
<p>Tables and values can be exchanged between .net and excel by using the methods in C# listed below. </p>
<p>The requirements for debugging and running these methods:</p>
<p>1) Microsoft Office and Excel must be installed<br />
2) Following excel-related COM assemblies must be referenced:<br />
<code>Microsoft Office XX.0 Object Library</code><br />
<code>Microsoft Excel XX.0 Object Library</code><br />
3) Following namespaces must be included in reference list in the code:<br />
<code>using System.Data;</code><br />
<code>using Excel = Microsoft.Office.Interop.Excel;</code></p>
<p>Feel free to use the code wherever you want provided that you leave any copyright information for Finaquant Analytics intact. We would appreciate any comment for possible corrections and improvements in the code.<span id="more-3456"></span></p>
<h3>Import DataTable from Excel Sheet using ODCB driver</h3>
<p>This method reads a table in an excel sheet into a DataTable object using the ODBC adapter. It requires <a href="http://www.microsoft.com/en-us/download/details.aspx?id=13255" title="Microsoft Access Database Engine 2010 Redistributable " target="_blank">Microsoft Access Database Engine</a> as driver software. The first line of the table in excel sheet must contain field names.</p>
<span class="collapseomatic" id="id8108"  title="C# code for ReadTableFromExcelSheet_ODBC()">C# code for ReadTableFromExcelSheet_ODBC()</span><div id="target-id8108" class="collapseomatic_content">

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Reads a table in an excel sheet into DataTable object using ODBC adapter.</span>
<span style="color: #008080; font-style: italic;">/// First line of table must contain field names.</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;ExcelFilePath&quot;&gt;A valid file path like  @&quot;C:\Users\John\Documents\Inventory.xlsx&lt;/param&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;SheetName&quot;&gt;Name of excel sheet&lt;/param&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;returns&gt;Table&lt;/returns&gt;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> DataTable ReadTableFromExcelSheet_ODBC<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> ExcelFilePath, <span style="color: #6666cc; font-weight: bold;">string</span> SheetName<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #008080; font-style: italic;">// PARAMETER CHECKS</span>
	<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>ExcelFilePath <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">||</span> ExcelFilePath <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">&#41;</span>
		<span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;ReadTableFromExcelSheet_ODBC: Null or empty string FilePath&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>SheetName <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">||</span> SheetName <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">&#41;</span>
		<span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;ReadTableFromExcelSheet_ODBC: Null or empty string SheetName&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span> File<span style="color: #008000;">.</span><span style="color: #0000FF;">Exists</span><span style="color: #008000;">&#40;</span>ExcelFilePath<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
		<span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;ReadTableFromExcelSheet_ODBC: Excel file is not found!&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// parameter checks OK, continue...</span>
	<span style="color: #6666cc; font-weight: bold;">string</span> ConnStr <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=&quot;</span> <span style="color: #008000;">+</span> ExcelFilePath <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;;&quot;</span><span style="color: #008000;">;</span>
	<span style="color: #6666cc; font-weight: bold;">string</span> Sql <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;SELECT * FROM [&quot;</span> <span style="color: #008000;">+</span> SheetName <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;$]&quot;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF; font-weight: bold;">try</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">var</span> adp <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> OdbcDataAdapter<span style="color: #008000;">&#40;</span>Sql, ConnStr<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">// fill datatable</span>
		DataTable dt <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DataTable<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		adp<span style="color: #008000;">.</span><span style="color: #0000FF;">Fill</span><span style="color: #008000;">&#40;</span>dt<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #0600FF; font-weight: bold;">return</span> dt<span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>Exception ex<span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;ReadTableFromExcelSheet_ODBC: &quot;</span> <span style="color: #008000;">+</span> ex<span style="color: #008000;">.</span><span style="color: #0000FF;">Message</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p></div>&nbsp;</p>
<h3>Import DataTable from Excel Sheet using OLEDB driver</h3>
<p>This method reads a table from an excel sheet into a DataTable using the OLEDB driver.</p>
<span class="collapseomatic" id="id8272"  title="C# code for ReadTableFromExcelSheet_OLEDB">C# code for ReadTableFromExcelSheet_OLEDB</span><div id="target-id8272" class="collapseomatic_content">

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Read DataTable from a sheet of excel file.</span>
<span style="color: #008080; font-style: italic;">/// Header line of the table in in excel sheet must contain field names.</span>
<span style="color: #008080; font-style: italic;">/// Copyrights: Finaquant Analytics - www.finaquant.com</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;Tbl&quot;&gt;DataTable input&lt;/param&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;ExcelFilePath&quot;&gt;A valid file path like  @&quot;C:\Users\John\Documents\Inventory.xlsx&lt;/param&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;SheetName&quot;&gt;Name of excel sheet&lt;/param&gt;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> DataTable ReadTableFromExcelSheet_OLEDB<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> ExcelFilePath, <span style="color: #6666cc; font-weight: bold;">string</span> SheetName<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #008080; font-style: italic;">// var connectionString = string.Format(&quot;Provider=Microsoft.Jet.OLEDB.4.0; data source={0}; Extended Properties=Excel 8.0;&quot;, Filepath);</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// PARAMETER CHECKS</span>
	<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>ExcelFilePath <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">||</span> ExcelFilePath <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">&#41;</span>
		<span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;ReadTableFromExcelSheet_OLEDB: Null or empty string FilePath&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>SheetName <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">||</span> SheetName <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">&#41;</span>
		<span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;ReadTableFromExcelSheet_OLEDB: Null or empty string SheetName&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>File<span style="color: #008000;">.</span><span style="color: #0000FF;">Exists</span><span style="color: #008000;">&#40;</span>ExcelFilePath<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
		<span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;ReadTableFromExcelSheet_OLEDB: Excel file is not found!&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// parameter checks OK, continue...</span>
	<span style="color: #008080; font-style: italic;">// set extended property for connection string</span>
	<span style="color: #6666cc; font-weight: bold;">string</span> ExtendedProperty<span style="color: #008000;">;</span>
	<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>ExcelFilePath<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span>ExcelFilePath<span style="color: #008000;">.</span><span style="color: #0000FF;">LastIndexOf</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">'.'</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToLower</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;.xlsx&quot;</span><span style="color: #008000;">&#41;</span>
		ExtendedProperty <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Excel 12.0 Xml&quot;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>ExcelFilePath<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span>ExcelFilePath<span style="color: #008000;">.</span><span style="color: #0000FF;">LastIndexOf</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">'.'</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToLower</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;.xls&quot;</span><span style="color: #008000;">&#41;</span>
		ExtendedProperty <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Excel 8.0&quot;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>ExcelFilePath<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span>ExcelFilePath<span style="color: #008000;">.</span><span style="color: #0000FF;">LastIndexOf</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">'.'</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToLower</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;.xlsm&quot;</span><span style="color: #008000;">&#41;</span>
		ExtendedProperty <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Excel 12.0 Macro&quot;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>ExcelFilePath<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span>ExcelFilePath<span style="color: #008000;">.</span><span style="color: #0000FF;">LastIndexOf</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">'.'</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToLower</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;.xlsb&quot;</span><span style="color: #008000;">&#41;</span>
		ExtendedProperty <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Excel 12.0&quot;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF; font-weight: bold;">else</span>
		<span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;ReadTableFromExcelSheet_OLEDB: Invalid file type!&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// var connectionString = string.Format(&quot;Provider=Microsoft.Jet.OLEDB.4.0; data source={0}; Extended Properties=\&quot;Excel 12.0;HDR=Yes;IMEX=1\&quot;&quot;, FilePath);</span>
	<span style="color: #0600FF; font-weight: bold;">var</span> connectionString <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Provider=Microsoft.ACE.OLEDB.12.0; data source={0}; Extended Properties=<span style="color: #008080; font-weight: bold;">\&quot;</span>{1};HDR=Yes;IMEX=1<span style="color: #008080; font-weight: bold;">\&quot;</span>&quot;</span>, ExcelFilePath, ExtendedProperty<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF; font-weight: bold;">try</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">var</span> adapter <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> OleDbDataAdapter<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;SELECT * FROM [&quot;</span> <span style="color: #008000;">+</span> SheetName <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;$]&quot;</span>, connectionString<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #0600FF; font-weight: bold;">var</span> TblOut <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DataTable<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		adapter<span style="color: #008000;">.</span><span style="color: #0000FF;">FillSchema</span><span style="color: #008000;">&#40;</span>TblOut, SchemaType<span style="color: #008000;">.</span><span style="color: #0000FF;">Source</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008080; font-style: italic;">// fill table</span>
		adapter<span style="color: #008000;">.</span><span style="color: #0000FF;">Fill</span><span style="color: #008000;">&#40;</span>TblOut<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #0600FF; font-weight: bold;">return</span> TblOut<span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>Exception ex<span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;ReadTableFromExcelSheet_OLEDB: Could not read table from excel file!<span style="color: #008080; font-weight: bold;">\n</span>&quot;</span> <span style="color: #008000;">+</span> ex<span style="color: #008000;">.</span><span style="color: #0000FF;">Message</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\n</span>&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p></div>&nbsp;</p>
<h3>Import DataTable from a Range in Excel Sheet</h3>
<p>This method reads a table from a given range in an excel sheet into a DataTable.</p>
<span class="collapseomatic" id="id3131"  title="C# code for ReadTableFromExcelRange">C# code for ReadTableFromExcelRange</span><div id="target-id3131" class="collapseomatic_content">

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Read table from a range in an excel sheet.</span>
<span style="color: #008080; font-style: italic;">/// Header line of the table in in excel sheet must contain field names.</span>
<span style="color: #008080; font-style: italic;">/// Copyrights: Finaquant Analytics - www.finaquant.com</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;ExcelFilePath&quot;&gt;A valid file path like  @&quot;C:\Users\John\Documents\Inventory.xlsx&lt;/param&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;SheetName&quot;&gt;Name of excel sheet&lt;/param&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;RangeStr&quot;&gt;A valid range string like &quot;B3:E10&quot;&lt;/param&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;returns&gt;Table&lt;/returns&gt;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> DataTable ReadTableFromExcelRange<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> ExcelFilePath, <span style="color: #6666cc; font-weight: bold;">string</span> SheetName, <span style="color: #6666cc; font-weight: bold;">string</span> RangeStr<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #008080; font-style: italic;">// PARAMETER CHECKS</span>
	<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>ExcelFilePath <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">||</span> ExcelFilePath <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">&#41;</span>
		<span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;ReadTableFromExcelRange: Null or empty string FilePath&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>SheetName <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">||</span> SheetName <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">&#41;</span>
		<span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;ReadTableFromExcelRange: Null or empty string SheetName&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>RangeStr <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">||</span> RangeStr <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">&#41;</span>
		<span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;ReadTableFromExcelRange: Null or empty string RangeStr&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>File<span style="color: #008000;">.</span><span style="color: #0000FF;">Exists</span><span style="color: #008000;">&#40;</span>ExcelFilePath<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
		<span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;ReadTableFromExcelRange: Excel file is not found!&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// parameter checks OK ...</span>
	<span style="color: #6666cc; font-weight: bold;">object</span> misValue <span style="color: #008000;">=</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Reflection</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Missing</span><span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Value</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF; font-weight: bold;">var</span> xlApp <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Excel<span style="color: #008000;">.</span><span style="color: #0000FF;">Application</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #6666cc; font-weight: bold;">bool</span> IfReadOnly <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
	Excel<span style="color: #008000;">.</span><span style="color: #0000FF;">Worksheet</span> xlWorkSheet <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
	Excel<span style="color: #008000;">.</span><span style="color: #0000FF;">Workbook</span> xlWorkBook <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF; font-weight: bold;">try</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #008080; font-style: italic;">// open workbook</span>
		xlWorkBook <span style="color: #008000;">=</span> xlApp<span style="color: #008000;">.</span><span style="color: #0000FF;">Workbooks</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Open</span><span style="color: #008000;">&#40;</span>ExcelFilePath, <span style="color: #FF0000;">0</span>, IfReadOnly, <span style="color: #FF0000;">5</span>, <span style="color: #666666;">&quot;&quot;</span>, <span style="color: #666666;">&quot;&quot;</span>, <span style="color: #0600FF; font-weight: bold;">true</span>,
				Microsoft<span style="color: #008000;">.</span><span style="color: #0000FF;">Office</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Interop</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Excel</span><span style="color: #008000;">.</span><span style="color: #0000FF;">XlPlatform</span><span style="color: #008000;">.</span><span style="color: #0000FF;">xlWindows</span>, <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\t</span>&quot;</span>, <span style="color: #0600FF; font-weight: bold;">false</span>, <span style="color: #0600FF; font-weight: bold;">false</span>, <span style="color: #FF0000;">0</span>, <span style="color: #0600FF; font-weight: bold;">true</span>, <span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">// check if sheet exists</span>
		<span style="color: #6666cc; font-weight: bold;">bool</span> SheetExists <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>Excel<span style="color: #008000;">.</span><span style="color: #0000FF;">Worksheet</span> sheet <span style="color: #0600FF; font-weight: bold;">in</span> xlWorkBook<span style="color: #008000;">.</span><span style="color: #0000FF;">Sheets</span><span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>sheet<span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Equals</span><span style="color: #008000;">&#40;</span>SheetName<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
			<span style="color: #008000;">&#123;</span>
				SheetExists <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
				<span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span>
			<span style="color: #008000;">&#125;</span>
		<span style="color: #008000;">&#125;</span>
&nbsp;
		<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>SheetExists<span style="color: #008000;">&#41;</span>
			<span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;ReadTableFromExcelRange: Sheet is not found in excel file!&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">// get worksheet</span>
		xlWorkSheet <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>Excel<span style="color: #008000;">.</span><span style="color: #0000FF;">Worksheet</span><span style="color: #008000;">&#41;</span>xlWorkBook<span style="color: #008000;">.</span><span style="color: #0000FF;">Sheets</span><span style="color: #008000;">.</span><span style="color: #0000FF;">get_Item</span><span style="color: #008000;">&#40;</span>SheetName<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">// get range object</span>
		Excel<span style="color: #008000;">.</span><span style="color: #0000FF;">Range</span> myRange <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>Excel<span style="color: #008000;">.</span><span style="color: #0000FF;">Range</span><span style="color: #008000;">&#41;</span>xlWorkSheet<span style="color: #008000;">.</span><span style="color: #0000FF;">get_Range</span><span style="color: #008000;">&#40;</span>RangeStr<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">// initiate data table</span>
		DataTable tbl <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DataTable<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">// get column names from header line</span>
		<span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> myRange<span style="color: #008000;">.</span><span style="color: #0000FF;">Columns</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			<span style="color: #6666cc; font-weight: bold;">object</span> CellValue <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&#41;</span> myRange<span style="color: #008000;">.</span><span style="color: #0000FF;">Cells</span><span style="color: #008000;">&#91;</span><span style="color: #FF0000;">1</span>, i <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Value2</span><span style="color: #008000;">;</span>
			tbl<span style="color: #008000;">.</span><span style="color: #0000FF;">Columns</span><span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Add</span><span style="color: #008000;">&#40;</span>CellValue<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">// read values row by row</span>
		<span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> myRange<span style="color: #008000;">.</span><span style="color: #0000FF;">Rows</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			tbl<span style="color: #008000;">.</span><span style="color: #0000FF;">Rows</span><span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
			<span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> j <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> j <span style="color: #008000;">&lt;</span> myRange<span style="color: #008000;">.</span><span style="color: #0000FF;">Columns</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">;</span> j<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span>
			<span style="color: #008000;">&#123;</span>
				<span style="color: #6666cc; font-weight: bold;">object</span> CellValue <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&#41;</span> myRange<span style="color: #008000;">.</span><span style="color: #0000FF;">Cells</span><span style="color: #008000;">&#91;</span><span style="color: #FF0000;">2</span> <span style="color: #008000;">+</span> i, j <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Value2</span><span style="color: #008000;">;</span>
				tbl<span style="color: #008000;">.</span><span style="color: #0000FF;">Rows</span><span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>j<span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> CellValue<span style="color: #008000;">;</span>
			<span style="color: #008000;">&#125;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #008080; font-style: italic;">// return data table</span>
		<span style="color: #0600FF; font-weight: bold;">return</span> tbl<span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>Exception ex<span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;ReadTableFromExcelRange: &quot;</span> <span style="color: #008000;">+</span> ex<span style="color: #008000;">.</span><span style="color: #0000FF;">Message</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0600FF; font-weight: bold;">finally</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">try</span>
		<span style="color: #008000;">&#123;</span>
			releaseObject<span style="color: #008000;">&#40;</span>xlWorkSheet<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			releaseObject<span style="color: #008000;">&#40;</span>xlWorkBook<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			releaseObject<span style="color: #008000;">&#40;</span>xlApp<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#123;</span> <span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #008080; font-style: italic;">//Helper function for releasing unused resources</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> releaseObject<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> obj<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0600FF; font-weight: bold;">try</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #000000;">System.<span style="color: #0000FF;">Runtime</span><span style="color: #008000;">.</span><span style="color: #0000FF;">InteropServices</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Marshal</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ReleaseComObject</span><span style="color: #008000;">&#40;</span>obj<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		obj <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>Exception ex<span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		obj <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
		<span style="color: #000000;">System.<span style="color: #0000FF;">Diagnostics</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Debug</span><span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Exception Occured while releasing object &quot;</span> <span style="color: #008000;">+</span> ex<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0600FF; font-weight: bold;">finally</span>
	<span style="color: #008000;">&#123;</span>
		GC<span style="color: #008000;">.</span><span style="color: #0000FF;">Collect</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p></div>&nbsp;</p>
<h3>Read a Value from a Cell in Excel Sheet</h3>
<p>This method reads the value of a cell of an excel sheet.</p>
<span class="collapseomatic" id="id8619"  title="C# code for ReadValueFromExcelCell">C# code for ReadValueFromExcelCell</span><div id="target-id8619" class="collapseomatic_content">

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Read value from a cell of an excel sheet.</span>
<span style="color: #008080; font-style: italic;">/// Copyrights: Finaquant Analytics - www.finaquant.com</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;ExcelFilePath&quot;&gt;A valid file path like  @&quot;C:\Users\John\Documents\Inventory.xlsx&lt;/param&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;SheetName&quot;&gt;Name of excel sheet&lt;/param&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;CellStr&quot;&gt;A valid cell string like &quot;B3&quot;&lt;/param&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;returns&gt;Scalar value&lt;/returns&gt;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">object</span> ReadValueFromExcelCell<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> ExcelFilePath, <span style="color: #6666cc; font-weight: bold;">string</span> SheetName, <span style="color: #6666cc; font-weight: bold;">string</span> CellStr<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #008080; font-style: italic;">// PARAMETER CHECKS</span>
	<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>ExcelFilePath <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">||</span> ExcelFilePath <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">&#41;</span>
		<span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;ReadValueFromExcelCell: Null or empty string FilePath&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>SheetName <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">||</span> SheetName <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">&#41;</span>
		<span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;ReadValueFromExcelCell: Null or empty string SheetName&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>CellStr <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">||</span> CellStr <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">&#41;</span>
		<span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;ReadValueFromExcelCell: Null or empty string CellStr&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>File<span style="color: #008000;">.</span><span style="color: #0000FF;">Exists</span><span style="color: #008000;">&#40;</span>ExcelFilePath<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
		<span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;ReadValueFromExcelCell: Excel file is not found!&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// parameter checks OK ...</span>
	<span style="color: #6666cc; font-weight: bold;">object</span> misValue <span style="color: #008000;">=</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Reflection</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Missing</span><span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Value</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF; font-weight: bold;">var</span> xlApp <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Excel<span style="color: #008000;">.</span><span style="color: #0000FF;">Application</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #6666cc; font-weight: bold;">bool</span> IfReadOnly <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
	Excel<span style="color: #008000;">.</span><span style="color: #0000FF;">Worksheet</span> xlWorkSheet <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
	Excel<span style="color: #008000;">.</span><span style="color: #0000FF;">Workbook</span> xlWorkBook <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF; font-weight: bold;">try</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #008080; font-style: italic;">// open workbook</span>
		xlWorkBook <span style="color: #008000;">=</span> xlApp<span style="color: #008000;">.</span><span style="color: #0000FF;">Workbooks</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Open</span><span style="color: #008000;">&#40;</span>ExcelFilePath, <span style="color: #FF0000;">0</span>, IfReadOnly, <span style="color: #FF0000;">5</span>, <span style="color: #666666;">&quot;&quot;</span>, <span style="color: #666666;">&quot;&quot;</span>, <span style="color: #0600FF; font-weight: bold;">true</span>,
				Microsoft<span style="color: #008000;">.</span><span style="color: #0000FF;">Office</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Interop</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Excel</span><span style="color: #008000;">.</span><span style="color: #0000FF;">XlPlatform</span><span style="color: #008000;">.</span><span style="color: #0000FF;">xlWindows</span>, <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\t</span>&quot;</span>, <span style="color: #0600FF; font-weight: bold;">false</span>, <span style="color: #0600FF; font-weight: bold;">false</span>, <span style="color: #FF0000;">0</span>, <span style="color: #0600FF; font-weight: bold;">true</span>, <span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">// check if sheet exists</span>
		<span style="color: #6666cc; font-weight: bold;">bool</span> SheetExists <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>Excel<span style="color: #008000;">.</span><span style="color: #0000FF;">Worksheet</span> sheet <span style="color: #0600FF; font-weight: bold;">in</span> xlWorkBook<span style="color: #008000;">.</span><span style="color: #0000FF;">Sheets</span><span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>sheet<span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Equals</span><span style="color: #008000;">&#40;</span>SheetName<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
			<span style="color: #008000;">&#123;</span>
				SheetExists <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
				<span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span>
			<span style="color: #008000;">&#125;</span>
		<span style="color: #008000;">&#125;</span>
&nbsp;
		<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>SheetExists<span style="color: #008000;">&#41;</span>
			<span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;ReadValueFromExcelCell: Sheet is not found in excel file!&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">// get worksheet</span>
		xlWorkSheet <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>Excel<span style="color: #008000;">.</span><span style="color: #0000FF;">Worksheet</span><span style="color: #008000;">&#41;</span>xlWorkBook<span style="color: #008000;">.</span><span style="color: #0000FF;">Sheets</span><span style="color: #008000;">.</span><span style="color: #0000FF;">get_Item</span><span style="color: #008000;">&#40;</span>SheetName<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">// get range object</span>
		Excel<span style="color: #008000;">.</span><span style="color: #0000FF;">Range</span> myRange <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>Excel<span style="color: #008000;">.</span><span style="color: #0000FF;">Range</span><span style="color: #008000;">&#41;</span>xlWorkSheet<span style="color: #008000;">.</span><span style="color: #0000FF;">get_Range</span><span style="color: #008000;">&#40;</span>CellStr<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">// initiate data table</span>
		<span style="color: #6666cc; font-weight: bold;">object</span> CellValue <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&#41;</span>myRange<span style="color: #008000;">.</span><span style="color: #0000FF;">Cells</span><span style="color: #008000;">&#91;</span><span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Value2</span><span style="color: #008000;">;</span>
		<span style="color: #0600FF; font-weight: bold;">return</span> CellValue<span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>Exception ex<span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;ReadValueFromExcelCell: &quot;</span> <span style="color: #008000;">+</span> ex<span style="color: #008000;">.</span><span style="color: #0000FF;">Message</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0600FF; font-weight: bold;">finally</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">try</span>
		<span style="color: #008000;">&#123;</span>
			releaseObject<span style="color: #008000;">&#40;</span>xlWorkSheet<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			releaseObject<span style="color: #008000;">&#40;</span>xlWorkBook<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			releaseObject<span style="color: #008000;">&#40;</span>xlApp<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#123;</span> <span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p></div>&nbsp;</p>
<h3>Export DataTable to Excel File</h3>
<p>This method writes a DataTable into the first sheet of a new excel file. It makes the excel file open and visible for the user.</p>
<span class="collapseomatic" id="id4650"  title="C# code for WriteTableToExcelFile">C# code for WriteTableToExcelFile</span><div id="target-id4650" class="collapseomatic_content">

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Write table into a new excel file. </span>
<span style="color: #008080; font-style: italic;">/// Make excel file visible to user after writing.</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;tbl&quot;&gt;Table&lt;/param&gt;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> WriteTableToExcelFile<span style="color: #008000;">&#40;</span>DataTable tbl<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>tbl <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">||</span> tbl<span style="color: #008000;">.</span><span style="color: #0000FF;">Columns</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span>
		<span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;WriteTableToExcelFile: Null or empty input table!<span style="color: #008080; font-weight: bold;">\n</span>&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF; font-weight: bold;">try</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #008080; font-style: italic;">// load excel, and create a new workbook</span>
		Excel<span style="color: #008000;">.</span><span style="color: #0000FF;">Application</span> excelApp <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Excel<span style="color: #008000;">.</span><span style="color: #0000FF;">Application</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		excelApp<span style="color: #008000;">.</span><span style="color: #0000FF;">Workbooks</span><span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">// single worksheet</span>
		Excel<span style="color: #008000;">.</span>_Worksheet workSheet <span style="color: #008000;">=</span> excelApp<span style="color: #008000;">.</span><span style="color: #0000FF;">ActiveSheet</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">// column headings</span>
		<span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> tbl<span style="color: #008000;">.</span><span style="color: #0000FF;">Columns</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			workSheet<span style="color: #008000;">.</span><span style="color: #0000FF;">Cells</span><span style="color: #008000;">&#91;</span><span style="color: #FF0000;">1</span>, <span style="color: #008000;">&#40;</span>i <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> tbl<span style="color: #008000;">.</span><span style="color: #0000FF;">Columns</span><span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ColumnName</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">// rows</span>
		<span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> tbl<span style="color: #008000;">.</span><span style="color: #0000FF;">Rows</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			<span style="color: #008080; font-style: italic;">// to do: format datetime values before printing</span>
			<span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> j <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> j <span style="color: #008000;">&lt;</span> tbl<span style="color: #008000;">.</span><span style="color: #0000FF;">Columns</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">;</span> j<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span>
			<span style="color: #008000;">&#123;</span>
				workSheet<span style="color: #008000;">.</span><span style="color: #0000FF;">Cells</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#40;</span>i <span style="color: #008000;">+</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span>, <span style="color: #008000;">&#40;</span>j <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> tbl<span style="color: #008000;">.</span><span style="color: #0000FF;">Rows</span><span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>j<span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
			<span style="color: #008000;">&#125;</span>
		<span style="color: #008000;">&#125;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">// make excel visible for user</span>
		excelApp<span style="color: #008000;">.</span><span style="color: #0000FF;">Visible</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>Exception ex<span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;WriteTableToExcelFile: &quot;</span> <span style="color: #008000;">+</span> ex<span style="color: #008000;">.</span><span style="color: #0000FF;">Message</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p></div>&nbsp;</p>
<h3>Export DataTable to Excel Sheet</h3>
<p>This method writes a DataTable into an excel sheet. A new sheet is created if the sheet with the given name does not exist.</p>
<span class="collapseomatic" id="id3611"  title="C# code for WriteTableToExcelSheet">C# code for WriteTableToExcelSheet</span><div id="target-id3611" class="collapseomatic_content">

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Write table to excel sheet.</span>
<span style="color: #008080; font-style: italic;">/// Copyrights: Finaquant Analytics - www.finaquant.com</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;tbl&quot;&gt;Table&lt;/param&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;ExcelFilePath&quot;&gt;File path for excel file. An error is raised if file is not found.&lt;/param&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;SheetName&quot;&gt;Name of sheet. A new sheet with given name is created if it does not exist.&lt;/param&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;CellStr&quot;&gt;A cell address like &quot;B3&quot;. Upper left corner for the table to be inserted.&lt;/param&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;ClearSheetContent&quot;&gt;If true, clear all sheet content before writing table.&lt;/param&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;SaveWorkbook&quot;&gt;If true, save workbook without making it visible (open) to user.&lt;/param&gt;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> WriteTableToExcelSheet<span style="color: #008000;">&#40;</span>DataTable tbl, <span style="color: #6666cc; font-weight: bold;">string</span> ExcelFilePath, <span style="color: #6666cc; font-weight: bold;">string</span> SheetName,
	<span style="color: #6666cc; font-weight: bold;">string</span> CellStr <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;A1&quot;</span>, <span style="color: #6666cc; font-weight: bold;">bool</span> ClearSheetContent <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span>, <span style="color: #6666cc; font-weight: bold;">bool</span> SaveWorkbook <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #008080; font-style: italic;">// PARAMETER CHECKS</span>
	<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>ExcelFilePath <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">||</span> ExcelFilePath <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">&#41;</span>
		<span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;WriteTableToExcelSheet: Null or empty string FilePath&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>SheetName <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">||</span> SheetName <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">&#41;</span>
		<span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;WriteTableToExcelSheet: Null or empty string SheetName&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>File<span style="color: #008000;">.</span><span style="color: #0000FF;">Exists</span><span style="color: #008000;">&#40;</span>ExcelFilePath<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
		<span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;WriteTableToExcelSheet: Excel file is not found!&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// parameter checks OK ...</span>
	<span style="color: #6666cc; font-weight: bold;">object</span> misValue <span style="color: #008000;">=</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Reflection</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Missing</span><span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Value</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF; font-weight: bold;">var</span> xlApp <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Excel<span style="color: #008000;">.</span><span style="color: #0000FF;">Application</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #6666cc; font-weight: bold;">bool</span> IfReadOnly <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
	Excel<span style="color: #008000;">.</span><span style="color: #0000FF;">Worksheet</span> xlWorkSheet <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
	Excel<span style="color: #008000;">.</span><span style="color: #0000FF;">Workbook</span> xlWorkBook <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF; font-weight: bold;">try</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #008080; font-style: italic;">// open workbook</span>
		xlWorkBook <span style="color: #008000;">=</span> xlApp<span style="color: #008000;">.</span><span style="color: #0000FF;">Workbooks</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Open</span><span style="color: #008000;">&#40;</span>ExcelFilePath, <span style="color: #FF0000;">0</span>, IfReadOnly, <span style="color: #FF0000;">5</span>, <span style="color: #666666;">&quot;&quot;</span>, <span style="color: #666666;">&quot;&quot;</span>, <span style="color: #0600FF; font-weight: bold;">true</span>,
				Microsoft<span style="color: #008000;">.</span><span style="color: #0000FF;">Office</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Interop</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Excel</span><span style="color: #008000;">.</span><span style="color: #0000FF;">XlPlatform</span><span style="color: #008000;">.</span><span style="color: #0000FF;">xlWindows</span>, <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\t</span>&quot;</span>, <span style="color: #0600FF; font-weight: bold;">false</span>, <span style="color: #0600FF; font-weight: bold;">false</span>, <span style="color: #FF0000;">0</span>, <span style="color: #0600FF; font-weight: bold;">true</span>, <span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">// check if sheet exists</span>
		<span style="color: #6666cc; font-weight: bold;">bool</span> SheetExists <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>Excel<span style="color: #008000;">.</span><span style="color: #0000FF;">Worksheet</span> sheet <span style="color: #0600FF; font-weight: bold;">in</span> xlWorkBook<span style="color: #008000;">.</span><span style="color: #0000FF;">Sheets</span><span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>sheet<span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Equals</span><span style="color: #008000;">&#40;</span>SheetName<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
			<span style="color: #008000;">&#123;</span>
				SheetExists <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
				<span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span>
			<span style="color: #008000;">&#125;</span>
		<span style="color: #008000;">&#125;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">// create a new sheet if it doesnot exist</span>
		<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>SheetExists<span style="color: #008000;">&#41;</span>
			xlWorkSheet <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>Excel<span style="color: #008000;">.</span><span style="color: #0000FF;">Worksheet</span><span style="color: #008000;">&#41;</span>xlWorkBook<span style="color: #008000;">.</span><span style="color: #0000FF;">Sheets</span><span style="color: #008000;">.</span><span style="color: #0000FF;">get_Item</span><span style="color: #008000;">&#40;</span>SheetName<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #0600FF; font-weight: bold;">else</span>
		<span style="color: #008000;">&#123;</span>
			xlWorkSheet <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>Excel<span style="color: #008000;">.</span><span style="color: #0000FF;">Worksheet</span><span style="color: #008000;">&#41;</span>xlWorkBook<span style="color: #008000;">.</span><span style="color: #0000FF;">Sheets</span><span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			xlWorkSheet<span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span> <span style="color: #008000;">=</span> SheetName<span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
&nbsp;
		<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>ClearSheetContent<span style="color: #008000;">&#41;</span>
			xlWorkSheet<span style="color: #008000;">.</span><span style="color: #0000FF;">Cells</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ClearContents</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>  <span style="color: #008080; font-style: italic;">// clear sheet content</span>
&nbsp;
		Excel<span style="color: #008000;">.</span><span style="color: #0000FF;">Range</span> myCell <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>Excel<span style="color: #008000;">.</span><span style="color: #0000FF;">Range</span><span style="color: #008000;">&#41;</span>xlWorkSheet<span style="color: #008000;">.</span><span style="color: #0000FF;">get_Range</span><span style="color: #008000;">&#40;</span>CellStr<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">// write column names as table header (first line)</span>
		<span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> tbl<span style="color: #008000;">.</span><span style="color: #0000FF;">Columns</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			myCell<span style="color: #008000;">.</span><span style="color: #0000FF;">Cells</span><span style="color: #008000;">&#91;</span><span style="color: #FF0000;">1</span>, i <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> tbl<span style="color: #008000;">.</span><span style="color: #0000FF;">Columns</span><span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ColumnName</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">// write table entries</span>
		DateTime dt<span style="color: #008000;">;</span>
		<span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> tbl<span style="color: #008000;">.</span><span style="color: #0000FF;">Rows</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			<span style="color: #008080; font-style: italic;">// to do: format datetime values before printing</span>
			<span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> j <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> j <span style="color: #008000;">&lt;</span> tbl<span style="color: #008000;">.</span><span style="color: #0000FF;">Columns</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">;</span> j<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span>
			<span style="color: #008000;">&#123;</span>
				<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>tbl<span style="color: #008000;">.</span><span style="color: #0000FF;">Columns</span><span style="color: #008000;">&#91;</span>j<span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">DataType</span> <span style="color: #008000;">==</span> <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>DateTime<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
				<span style="color: #008000;">&#123;</span>
					dt <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">DateTime</span><span style="color: #008000;">&#41;</span>tbl<span style="color: #008000;">.</span><span style="color: #0000FF;">Rows</span><span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>j<span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
					myCell<span style="color: #008000;">.</span><span style="color: #0000FF;">Cells</span><span style="color: #008000;">&#91;</span>i <span style="color: #008000;">+</span> <span style="color: #FF0000;">2</span>, j <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> dt<span style="color: #008000;">.</span><span style="color: #0000FF;">ToShortDateString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
				<span style="color: #008000;">&#125;</span>
				<span style="color: #0600FF; font-weight: bold;">else</span>
				<span style="color: #008000;">&#123;</span>
					myCell<span style="color: #008000;">.</span><span style="color: #0000FF;">Cells</span><span style="color: #008000;">&#91;</span>i <span style="color: #008000;">+</span> <span style="color: #FF0000;">2</span>, j <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> tbl<span style="color: #008000;">.</span><span style="color: #0000FF;">Rows</span><span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>j<span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
				<span style="color: #008000;">&#125;</span>
			<span style="color: #008000;">&#125;</span>
		<span style="color: #008000;">&#125;</span>
&nbsp;
		<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>SaveWorkbook<span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			xlWorkBook<span style="color: #008000;">.</span><span style="color: #0000FF;">Save</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			xlWorkBook<span style="color: #008000;">.</span><span style="color: #0000FF;">Close</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">true</span>, misValue, misValue<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			xlApp<span style="color: #008000;">.</span><span style="color: #0000FF;">Quit</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #0600FF; font-weight: bold;">else</span>
		<span style="color: #008000;">&#123;</span>
			xlWorkSheet<span style="color: #008000;">.</span><span style="color: #0000FF;">Activate</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			xlApp<span style="color: #008000;">.</span><span style="color: #0000FF;">Visible</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>Exception ex<span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;WriteTableToExcelSheet: &quot;</span> <span style="color: #008000;">+</span> ex<span style="color: #008000;">.</span><span style="color: #0000FF;">Message</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0600FF; font-weight: bold;">finally</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">try</span>
		<span style="color: #008000;">&#123;</span>
			releaseObject<span style="color: #008000;">&#40;</span>xlWorkSheet<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			releaseObject<span style="color: #008000;">&#40;</span>xlWorkBook<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			releaseObject<span style="color: #008000;">&#40;</span>xlApp<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#123;</span> <span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p></div>&nbsp;</p>
<h3>Write a Value into a Cell of Excel Sheet</h3>
<p>This methods writes a given value into a cell of excel sheet.</p>
<span class="collapseomatic" id="id5459"  title="C# code for WriteValueToExcelCell">C# code for WriteValueToExcelCell</span><div id="target-id5459" class="collapseomatic_content">

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Write a value into a cell of excel file.</span>
<span style="color: #008080; font-style: italic;">/// Copyrights: Finaquant Analytics - www.finaquant.com</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;CellValue&quot;&gt;Value&lt;/param&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;ExcelFilePath&quot;&gt;File path for excel file. An error is raised if file is not found.&lt;/param&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;SheetName&quot;&gt;Name of sheet. A new sheet with given name is created if it does not exist.&lt;/param&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;CellStr&quot;&gt;A cell address like &quot;B3&quot; for the value to be inserted.&lt;/param&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;ClearSheetContent&quot;&gt;If true, clear all sheet content before writing table.&lt;/param&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;SaveWorkbook&quot;&gt;If true, save workbook without making it visible (open) to user.&lt;/param&gt;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> WriteValueToExcelCell<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> CellValue, <span style="color: #6666cc; font-weight: bold;">string</span> ExcelFilePath, <span style="color: #6666cc; font-weight: bold;">string</span> SheetName,
	<span style="color: #6666cc; font-weight: bold;">string</span> CellStr, <span style="color: #6666cc; font-weight: bold;">bool</span> ClearSheetContent <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span>, <span style="color: #6666cc; font-weight: bold;">bool</span> SaveWorkbook <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #008080; font-style: italic;">// PARAMETER CHECKS</span>
	<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>CellValue <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
		<span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;WriteValueToExcelCell: Null-valued cell value&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>ExcelFilePath <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">||</span> ExcelFilePath <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">&#41;</span>
		<span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;WriteValueToExcelCell: Null or empty string FilePath&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>SheetName <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">||</span> SheetName <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">&#41;</span>
		<span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;WriteValueToExcelCell: Null or empty string SheetName&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>File<span style="color: #008000;">.</span><span style="color: #0000FF;">Exists</span><span style="color: #008000;">&#40;</span>ExcelFilePath<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
		<span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;WriteValueToExcelCell: Excel file is not found!&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// parameter checks OK ...</span>
	<span style="color: #6666cc; font-weight: bold;">object</span> misValue <span style="color: #008000;">=</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Reflection</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Missing</span><span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Value</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF; font-weight: bold;">var</span> xlApp <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Excel<span style="color: #008000;">.</span><span style="color: #0000FF;">Application</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #6666cc; font-weight: bold;">bool</span> IfReadOnly <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
	Excel<span style="color: #008000;">.</span><span style="color: #0000FF;">Worksheet</span> xlWorkSheet <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
	Excel<span style="color: #008000;">.</span><span style="color: #0000FF;">Workbook</span> xlWorkBook <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF; font-weight: bold;">try</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #008080; font-style: italic;">// open workbook</span>
		xlWorkBook <span style="color: #008000;">=</span> xlApp<span style="color: #008000;">.</span><span style="color: #0000FF;">Workbooks</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Open</span><span style="color: #008000;">&#40;</span>ExcelFilePath, <span style="color: #FF0000;">0</span>, IfReadOnly, <span style="color: #FF0000;">5</span>, <span style="color: #666666;">&quot;&quot;</span>, <span style="color: #666666;">&quot;&quot;</span>, <span style="color: #0600FF; font-weight: bold;">true</span>,
				Microsoft<span style="color: #008000;">.</span><span style="color: #0000FF;">Office</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Interop</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Excel</span><span style="color: #008000;">.</span><span style="color: #0000FF;">XlPlatform</span><span style="color: #008000;">.</span><span style="color: #0000FF;">xlWindows</span>, <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\t</span>&quot;</span>, <span style="color: #0600FF; font-weight: bold;">false</span>, <span style="color: #0600FF; font-weight: bold;">false</span>, <span style="color: #FF0000;">0</span>, <span style="color: #0600FF; font-weight: bold;">true</span>, <span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">// check if sheet exists</span>
		<span style="color: #6666cc; font-weight: bold;">bool</span> SheetExists <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>Excel<span style="color: #008000;">.</span><span style="color: #0000FF;">Worksheet</span> sheet <span style="color: #0600FF; font-weight: bold;">in</span> xlWorkBook<span style="color: #008000;">.</span><span style="color: #0000FF;">Sheets</span><span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>sheet<span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Equals</span><span style="color: #008000;">&#40;</span>SheetName<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
			<span style="color: #008000;">&#123;</span>
				SheetExists <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
				<span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span>
			<span style="color: #008000;">&#125;</span>
		<span style="color: #008000;">&#125;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">// create a new sheet if it doesnot exist</span>
		<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>SheetExists<span style="color: #008000;">&#41;</span>
			xlWorkSheet <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>Excel<span style="color: #008000;">.</span><span style="color: #0000FF;">Worksheet</span><span style="color: #008000;">&#41;</span>xlWorkBook<span style="color: #008000;">.</span><span style="color: #0000FF;">Sheets</span><span style="color: #008000;">.</span><span style="color: #0000FF;">get_Item</span><span style="color: #008000;">&#40;</span>SheetName<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #0600FF; font-weight: bold;">else</span>
		<span style="color: #008000;">&#123;</span>
			xlWorkSheet <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>Excel<span style="color: #008000;">.</span><span style="color: #0000FF;">Worksheet</span><span style="color: #008000;">&#41;</span>xlWorkBook<span style="color: #008000;">.</span><span style="color: #0000FF;">Sheets</span><span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			xlWorkSheet<span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span> <span style="color: #008000;">=</span> SheetName<span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
&nbsp;
		<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>ClearSheetContent<span style="color: #008000;">&#41;</span>
			xlWorkSheet<span style="color: #008000;">.</span><span style="color: #0000FF;">Cells</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ClearContents</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>  <span style="color: #008080; font-style: italic;">// clear sheet content</span>
&nbsp;
		Excel<span style="color: #008000;">.</span><span style="color: #0000FF;">Range</span> myCell <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>Excel<span style="color: #008000;">.</span><span style="color: #0000FF;">Range</span><span style="color: #008000;">&#41;</span>xlWorkSheet<span style="color: #008000;">.</span><span style="color: #0000FF;">get_Range</span><span style="color: #008000;">&#40;</span>CellStr<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		myCell<span style="color: #008000;">.</span><span style="color: #0000FF;">Cells</span><span style="color: #008000;">&#91;</span><span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> CellValue<span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>SaveWorkbook<span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			xlWorkBook<span style="color: #008000;">.</span><span style="color: #0000FF;">Save</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			xlWorkBook<span style="color: #008000;">.</span><span style="color: #0000FF;">Close</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">true</span>, misValue, misValue<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			xlApp<span style="color: #008000;">.</span><span style="color: #0000FF;">Quit</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #0600FF; font-weight: bold;">else</span>
		<span style="color: #008000;">&#123;</span>
			xlWorkSheet<span style="color: #008000;">.</span><span style="color: #0000FF;">Activate</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			xlApp<span style="color: #008000;">.</span><span style="color: #0000FF;">Visible</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>Exception ex<span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;WriteValueToExcelCell: &quot;</span> <span style="color: #008000;">+</span> ex<span style="color: #008000;">.</span><span style="color: #0000FF;">Message</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0600FF; font-weight: bold;">finally</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">try</span>
		<span style="color: #008000;">&#123;</span>
			releaseObject<span style="color: #008000;">&#40;</span>xlWorkSheet<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			releaseObject<span style="color: #008000;">&#40;</span>xlWorkBook<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			releaseObject<span style="color: #008000;">&#40;</span>xlApp<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#123;</span> <span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p></div>&nbsp;</p>
<h3>References and useful links</h3>
<ol>
<li><a href="http://www.microsoft.com/en-us/download/details.aspx?id=13255http://" title="Microsoft Access Database Engine 2010 Redistributable " target="_blank">Microsoft Access Database Engine 2010 Redistributable</a> </li>
<li><a href="http://www.codeproject.com/Questions/445400/Read-Excel-Sheet-Data-into-DataTable" title="Read Excel Sheet Data into DataTable" target="_blank">Read Excel Sheet Data into DataTable</a></li>
<li><a href="http://stackoverflow.com/questions/1991643/microsoft-jet-oledb-4-0-provider-is-not-registered-on-the-local-machine" title="Microsoft.Jet.OLEDB.4.0' provider is not registered on the local machine" target="_blank">Microsoft.Jet.OLEDB.4.0&#8242; provider is not registered on the local machine</a></li>
</ol>
<span id="dprv_cp-v2.13" lang="en" xml:lang="en" class="notranslate" style="vertical-align:baseline; padding: 3px 3px 3px 3px; margin-top:2px; margin-bottom:2px; border-collapse:separate; line-height:16px;float:none; font-family: Tahoma, MS Sans Serif; font-size:13px;border:1px solid #bbbbbb;background:#FFFFFF none;display:inline-block;" title="certified 5 May 2013 21:34:52 UTC by Digiprove certificate P398886" ><a href="http://www.digiprove.com/prove_copyright.aspx?id=P398886%26guid=orSIp_4HGE2gNqLR9pvQPg" target="_blank" rel="copyright" style="height:16px; line-height: 16px; border:0px; padding:0px; margin:0px; float:none; display:inline; text-decoration: none; background:transparent none; line-height:normal; font-family: Tahoma, MS Sans Serif; font-style:normal; font-weight:normal; font-size:11px;"><img src="http://finaquant.com/wp-content/plugins/digiproveblog/dp_seal_trans_16x16.png" style="max-width:none !important;vertical-align:-3px; display:inline; border:0px; margin:0px; padding:0px; float:none; background:transparent none" border="0" alt=""/><span style="font-family: Tahoma, MS Sans Serif; font-style:normal; font-size:11px; font-weight:normal; color:#636363; border:0px; float:none; display:inline; text-decoration:none; letter-spacing:normal; padding:0px; padding-left:8px; vertical-align:1px;margin-bottom:2px" onmouseover="this.style.color='#A35353';" onmouseout="this.style.color='#636363';">Copyright&nbsp;secured&nbsp;by&nbsp;Digiprove&nbsp;&copy;&nbsp;2013&nbsp;Tunc&nbsp;Ali&nbsp;Kütükcüoglu</span></a><!--87FCBA412DC434D737460CDB2452CA316DAC6B8F5CBC63E1770BC4BDFEBBFCFE--></span>]]></content:encoded>
			<wfw:commentRss>http://finaquant.com/importing-and-exporting-a-datatable-from-an-excel-file/3456/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Converting a MatrixTable to a DataTable, and vice versa</title>
		<link>http://finaquant.com/converting-a-matrixtable-to-a-datatable-and-vice-versa/3452?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=converting-a-matrixtable-to-a-datatable-and-vice-versa</link>
		<comments>http://finaquant.com/converting-a-matrixtable-to-a-datatable-and-vice-versa/3452#comments</comments>
		<pubDate>Wed, 01 May 2013 21:10:57 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Calculation engine]]></category>
		<category><![CDATA[DataTable]]></category>
		<category><![CDATA[integration]]></category>
		<category><![CDATA[matrixtable]]></category>

		<guid isPermaLink="false">http://finaquant.com/?p=3452</guid>
		<description><![CDATA[MatrixTable is a type (class) for in-memory representation of data tables just like DataTable of the .net framework (ADO.net). MatrixTable has but a much simplified data structure compared to DataTable with primary focus on mathematical (or analytical) table functions rather &#8230; <a href="http://finaquant.com/converting-a-matrixtable-to-a-datatable-and-vice-versa/3452">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p><a href="http://finaquant.com/help-pages-for-finaquant-products/text-and-numeric-attributes-key-figures" title="Table fields: Text and numeric attributes, key figures" target="_blank">MatrixTable</a> is a type (class) for in-memory representation of data tables just like <a href="http://msdn.microsoft.com/en-us/library/system.data.datatable.aspx" title="DataTable Class" target="_blank">DataTable</a> of the .net framework (ADO.net). <em>MatrixTable</em> has but a much simplified data structure compared to DataTable with primary focus on mathematical (or analytical) <a href="http://finaquant.com/help-pages-for-finaquant-products/what-is-a-table-function" title="What is a table function? Why do we need table functions?" target="_blank">table functions</a> rather than typical sql-based data operations like inserts and updates.</p>
<p><img src="http://finaquant.com/wp-content/uploads/2013/05/MatrixTable_DataTable.png" alt="DataTable to or from MatrixTable" width="370" height="94" class="alignnone size-full wp-image-3443" /></p>
<p>With the following methods of the MatrixTable class, a DataTable can easily be converted into a MatrixTable, and vice versa:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// From DataTable to MatrixTable</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> MatrixTable Import_from_DataTable<span style="color: #008000;">&#40;</span>DataTable dtable, MetaData md,
   <span style="color: #6666cc; font-weight: bold;">string</span> TextReplaceNull <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;NULL&quot;</span>, <span style="color: #6666cc; font-weight: bold;">int</span> NumReplaceNull <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span>, <span style="color: #6666cc; font-weight: bold;">double</span> KeyFigReplaceNull <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// From MatrixTable to DataTable</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> DataTable Export_To_DataTable<span style="color: #008000;">&#40;</span>MatrixTable tbl<span style="color: #008000;">&#41;</span></pre></td></tr></table></div>

<p><span id="more-3452"></span><br />
You can also find open-source DataTable extensions (c#/.net, license: GPL) that include methods for these conversions at <a href="https://datatableextensions.codeplex.com/" title="DataTable extensions at CodePlex.com" target="_blank">CodePlex</a>.</p>
<h2>Converting DataTable to MatrixTable</h2>
<p>In order to convert a DataTable object into a MatrixTable all the fields (columns) of DataTable must be predefined in MetaData with proper field types. </p>
<p>See the c# code example below:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">FinaquantProtos</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Data</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// First, create a DataTable object</span>
<span style="color: #008080; font-style: italic;">// For a helpful reference see: http://www.dotnetperls.com/datatable</span>
DataTable DTbl <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DataTable<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
DTbl<span style="color: #008000;">.</span><span style="color: #0000FF;">Columns</span><span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;product&quot;</span>, <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
DTbl<span style="color: #008000;">.</span><span style="color: #0000FF;">Columns</span><span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;model&quot;</span>, <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
DTbl<span style="color: #008000;">.</span><span style="color: #0000FF;">Columns</span><span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;date&quot;</span>, <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>DateTime<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
DTbl<span style="color: #008000;">.</span><span style="color: #0000FF;">Columns</span><span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;sales&quot;</span>, <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// Add some rows to DataTable</span>
DTbl<span style="color: #008000;">.</span><span style="color: #0000FF;">Rows</span><span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Computer&quot;</span>, <span style="color: #FF0000;">2010</span>, DateTime<span style="color: #008000;">.</span><span style="color: #0000FF;">Now</span>, <span style="color: #FF0000;">25000.0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
DTbl<span style="color: #008000;">.</span><span style="color: #0000FF;">Rows</span><span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Telephone&quot;</span>, <span style="color: #FF0000;">2008</span>, <span style="color: #008000;">new</span> DateTime<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">2010</span>, <span style="color: #FF0000;">8</span>, <span style="color: #FF0000;">30</span><span style="color: #008000;">&#41;</span>, <span style="color: #FF0000;">8000.0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
DTbl<span style="color: #008000;">.</span><span style="color: #0000FF;">Rows</span><span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Camera&quot;</span>, <span style="color: #FF0000;">2012</span>, <span style="color: #008000;">new</span> DateTime<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">2012</span>, <span style="color: #FF0000;">9</span>, <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span>, <span style="color: #FF0000;">34000.0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// define all fields of DataTable in meta data</span>
MetaData md <span style="color: #008000;">=</span> MetaData<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateEmptyMetaData</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
MetaData<span style="color: #008000;">.</span><span style="color: #0000FF;">AddNewField</span><span style="color: #008000;">&#40;</span>md, <span style="color: #666666;">&quot;product&quot;</span>, FieldType<span style="color: #008000;">.</span><span style="color: #0000FF;">TextAttribute</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
MetaData<span style="color: #008000;">.</span><span style="color: #0000FF;">AddNewField</span><span style="color: #008000;">&#40;</span>md, <span style="color: #666666;">&quot;model&quot;</span>, FieldType<span style="color: #008000;">.</span><span style="color: #0000FF;">IntegerAttribute</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
MetaData<span style="color: #008000;">.</span><span style="color: #0000FF;">AddNewField</span><span style="color: #008000;">&#40;</span>md, <span style="color: #666666;">&quot;date&quot;</span>, FieldType<span style="color: #008000;">.</span><span style="color: #0000FF;">DateAttribute</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
MetaData<span style="color: #008000;">.</span><span style="color: #0000FF;">AddNewField</span><span style="color: #008000;">&#40;</span>md, <span style="color: #666666;">&quot;sales&quot;</span>, FieldType<span style="color: #008000;">.</span><span style="color: #0000FF;">KeyFigure</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// convert DataTable to MatrixTable</span>
MatrixTable MTbl <span style="color: #008000;">=</span> MatrixTable<span style="color: #008000;">.</span><span style="color: #0000FF;">Import_from_DataTable</span><span style="color: #008000;">&#40;</span>DTbl, md<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// view MatrixTable</span>
MatrixTable<span style="color: #008000;">.</span><span style="color: #0000FF;">View_MatrixTable</span><span style="color: #008000;">&#40;</span>MTbl, <span style="color: #666666;">&quot;MatrixTable converted from DataTable&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p><img src="http://finaquant.com/wp-content/uploads/2013/05/MatrixTable_DataTable_DtoM.png" alt="DataTable from MatrixTable" width="394" height="132" class="alignnone size-full wp-image-3447" /></p>
<h2>Converting MatrixTable to DataTable</h2>
<p>Quite straightforward, see the code example below:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// make some changes in MatrixTable</span>
MTbl<span style="color: #008000;">.</span><span style="color: #0000FF;">SetFieldValue</span><span style="color: #008000;">&#40;</span>FieldName<span style="color: #008000;">:</span> <span style="color: #666666;">&quot;product&quot;</span>, RowInd<span style="color: #008000;">:</span> <span style="color: #FF0000;">0</span>, FieldValue<span style="color: #008000;">:</span> <span style="color: #666666;">&quot;Laptop&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
MTbl<span style="color: #008000;">.</span><span style="color: #0000FF;">SetFieldValue</span><span style="color: #008000;">&#40;</span>FieldName<span style="color: #008000;">:</span> <span style="color: #666666;">&quot;model&quot;</span>, RowInd<span style="color: #008000;">:</span> <span style="color: #FF0000;">1</span>, FieldValue<span style="color: #008000;">:</span> <span style="color: #FF0000;">1999</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// convert MatrixTable to DataTable</span>
DTbl <span style="color: #008000;">=</span> MatrixTable<span style="color: #008000;">.</span><span style="color: #0000FF;">Export_To_DataTable</span><span style="color: #008000;">&#40;</span>MTbl<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// view DataTable</span>
DTbl<span style="color: #008000;">.</span><span style="color: #0000FF;">FQ_ViewTable</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;DataTable converted from MatrixTable&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p><a href="http://finaquant.com/wp-content/uploads/2013/05/MatrixTable_DataTable_MtoD.png" rel="lightbox[3452]" title="Converting a MatrixTable to a DataTable, and vice versa"><img src="http://finaquant.com/wp-content/uploads/2013/05/MatrixTable_DataTable_MtoD.png" alt="DataTable from MatrixTable" width="423" height="129" class="alignnone size-full wp-image-3449" /></a></p>
<span id="dprv_cp-v2.13" lang="en" xml:lang="en" class="notranslate" style="vertical-align:baseline; padding: 3px 3px 3px 3px; margin-top:2px; margin-bottom:2px; border-collapse:separate; line-height:16px;float:none; font-family: Tahoma, MS Sans Serif; font-size:13px;border:1px solid #bbbbbb;background:#FFFFFF none;display:inline-block;" title="certified 1 May 2013 21:10:25 UTC by Digiprove certificate P397573" ><a href="http://www.digiprove.com/prove_copyright.aspx?id=P397573%26guid=FTuGUUJfkE6jqhrqL490Lg" target="_blank" rel="copyright" style="height:16px; line-height: 16px; border:0px; padding:0px; margin:0px; float:none; display:inline; text-decoration: none; background:transparent none; line-height:normal; font-family: Tahoma, MS Sans Serif; font-style:normal; font-weight:normal; font-size:11px;"><img src="http://finaquant.com/wp-content/plugins/digiproveblog/dp_seal_trans_16x16.png" style="max-width:none !important;vertical-align:-3px; display:inline; border:0px; margin:0px; padding:0px; float:none; background:transparent none" border="0" alt=""/><span style="font-family: Tahoma, MS Sans Serif; font-style:normal; font-size:11px; font-weight:normal; color:#636363; border:0px; float:none; display:inline; text-decoration:none; letter-spacing:normal; padding:0px; padding-left:8px; vertical-align:1px;margin-bottom:2px" onmouseover="this.style.color='#A35353';" onmouseout="this.style.color='#636363';">Copyright&nbsp;secured&nbsp;by&nbsp;Digiprove&nbsp;&copy;&nbsp;2013&nbsp;Tunc&nbsp;Ali&nbsp;Kütükcüoglu</span></a><!--5E110C494DF539226B57C218EC8733933E4B137393366BDC9256B00AEAF890C8--></span>]]></content:encoded>
			<wfw:commentRss>http://finaquant.com/converting-a-matrixtable-to-a-datatable-and-vice-versa/3452/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Persistent Table Array</title>
		<link>http://finaquant.com/persistent-table-array/3330?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=persistent-table-array</link>
		<comments>http://finaquant.com/persistent-table-array/3330#comments</comments>
		<pubDate>Tue, 09 Apr 2013 14:00:28 +0000</pubDate>
		<dc:creator>tuncalik</dc:creator>
				<category><![CDATA[Calculation engine]]></category>
		<category><![CDATA[.net library]]></category>
		<category><![CDATA[calculation engine]]></category>
		<category><![CDATA[database]]></category>

		<guid isPermaLink="false">http://finaquant.com/?p=3330</guid>
		<description><![CDATA[PersistentTableArray and DataStore are new classes of the non-commercial .net library finaquant® protos (starting from release 1.04) that enable connection to relational databases like MySQL and Microsoft SQL in order to read/write complete tables from/to a database together with their &#8230; <a href="http://finaquant.com/persistent-table-array/3330">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p><em>PersistentTableArray</em> and <em>DataStore</em> are new classes of the non-commercial .net library <a title="finaquant® protos – free .net library with table functions" href="http://finaquant.com/products/finaquantprotos" target="_blank">finaquant® protos</a> (starting from release 1.04) that enable connection to relational databases like <em>MySQL</em> and <em>Microsoft SQL</em> in order to read/write complete tables from/to a database together with their instance information.</p>
<p>Each <em>table instance</em> is an element of a persistent table array. Following C# code example illustrates how separate cost tables for each <em>country</em> and <em>year</em> can be stored in a MySQL database with a persistent table array:</p>
<p><a href="http://finaquant.com/wp-content/uploads/2013/04/PersistentTableArray_CostTables.png" rel="lightbox[3330]" title="Cost tables for different countries and years"><img class="wp-image-3332  alignnone" title="Cost tables for different countries and years" alt="Cost tables for different countries and years" src="http://finaquant.com/wp-content/uploads/2013/04/PersistentTableArray_CostTables.png" width="550" height="144" /></a></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">FinaquantProtos</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Data.Odbc</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// Connection string for MySQL database: </span>
<span style="color: #008080; font-style: italic;">// Replace it with your own valid connection string on your computer</span>
<span style="color: #6666cc; font-weight: bold;">string</span> ConnStr <span style="color: #008000;">=</span> <span style="color: #666666;">@&quot;Driver={MySQL ODBC 5.2w Driver};Server=127.0.0.1;Database=testdb;uid=root;pwd=&quot;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// Data provider</span>
<span style="color: #008080; font-style: italic;">// You need to install ODBC driver for MySQL on your computer (win 32 bit version)</span>
<span style="color: #008080; font-style: italic;">// see: http://dev.mysql.com/downloads/connector/odbc/</span>
<span style="color: #6666cc; font-weight: bold;">string</span> DataProvider <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;System.Data.Odbc&quot;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// initiate DataStore object for database connection</span>
DataStore datstore <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DataStore<span style="color: #008000;">&#40;</span>Provider<span style="color: #008000;">:</span> DataProvider<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> 
&nbsp;
<span style="color: #008080; font-style: italic;">// open database connection</span>
datstore<span style="color: #008000;">.</span><span style="color: #0000FF;">OpenConnection</span><span style="color: #008000;">&#40;</span>ConnStr<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// initiate &quot;persistent table array&quot; objects</span>
PersistentTableArray PTblCost <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PersistentTableArray<span style="color: #008000;">&#40;</span>TableName<span style="color: #008000;">:</span> <span style="color: #666666;">&quot;costs&quot;</span>, dstore<span style="color: #008000;">:</span> datstore<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// Create TableRow objects as instance keys for tables</span>
TableRow trow1 <span style="color: #008000;">=</span> TableRow<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateTableRowWithElements</span><span style="color: #008000;">&#40;</span>InstanceRowFields, <span style="color: #666666;">&quot;Peru&quot;</span>, <span style="color: #FF0000;">2008</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
TableRow trow2 <span style="color: #008000;">=</span> TableRow<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateTableRowWithElements</span><span style="color: #008000;">&#40;</span>InstanceRowFields, <span style="color: #666666;">&quot;Peru&quot;</span>, <span style="color: #FF0000;">2012</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
TableRow trow3 <span style="color: #008000;">=</span> TableRow<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateTableRowWithElements</span><span style="color: #008000;">&#40;</span>InstanceRowFields, <span style="color: #666666;">&quot;Bolivia&quot;</span>, <span style="color: #FF0000;">2010</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// store each table instance separately into database (yes, just by assigning!)</span>
PTblCost<span style="color: #008000;">&#91;</span>trow1<span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> CostTblPeru2008<span style="color: #008000;">;</span>      <span style="color: #008080; font-style: italic;">// store CostTblPeru2008 with instance information in trow1</span>
PTblCost<span style="color: #008000;">&#91;</span>trow2<span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> CostTblPeru2012<span style="color: #008000;">;</span>
PTblCost<span style="color: #008000;">&#91;</span>trow3<span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> CostTblBolivia2010<span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>Note that CostTblPeru2008, CostTblPeru2012 and CostTblBolivia2010 are different instances of in-memory cost tables (of type MatrixTable) with the same field structure.<span id="more-3330"></span></p>
<p>All you need to do is prepare an empty database and assign the connection string in order to initiate the DataStore object. All necessary tables are automatically created and updated by PersistentTableArray or DataStore operations.</p>
<p>The statement <code>PTblCost[trow1]</code> reads a complete table with instance parameter <code>trow1</code> from the database, and returns an in-memory table of type <em>MatrixTable</em>:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;">MatrixTable CostTbl <span style="color: #008000;">=</span> PTblCost<span style="color: #008000;">&#91;</span>trow1<span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>You can view an element (instance) of a persistent table array <code>PTblCost</code> as follows:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;">MatrixTable<span style="color: #008000;">.</span><span style="color: #0000FF;">View_MatrixTable</span><span style="color: #008000;">&#40;</span>PTblCost<span style="color: #008000;">&#91;</span>trow1<span style="color: #008000;">&#93;</span>, <span style="color: #666666;">&quot;Cost Table, Peru - 2008&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p><a href="http://finaquant.com/wp-content/uploads/2013/04/PersistentTableArray_CostTablePeru.png" rel="lightbox[3330]" title="Cost Table, instance: Peru-2008"><img class="alignnone size-full wp-image-3340" title="Cost Table, instance: Peru-2008" alt="Cost Table, instance: Peru-2008" src="http://finaquant.com/wp-content/uploads/2013/04/PersistentTableArray_CostTablePeru.png" width="422" height="238" /></a><br />
Note that each attribute pair (country and year) identifies a single table instance. In this example we have only two attributes, but a table row as instance key of a persistent table array can have as many attributes as you need.</p>
<p>Following statement writes the in-memory table CostTbl into database together with the instance parameter trow1:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;">PTblCost<span style="color: #008000;">&#91;</span>trow1<span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> CostTbl<span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>If a table with the same field structure and instance parameter already exists in the database it will be overwritten (unless it is locked, see DataStore methods) just like in ordinary arrays.</p>
<h3>Persistent Table Array and DataStore</h3>
<p>Methods of <em>DataStore</em> class are responsible for all database operations for reading or writing complete tables.</p>
<p>A persistent table array assignment like <code>PTblCost[trow1] = CostTblPeru2008</code> triggers following method of the DataStore class:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;">PTblCost<span style="color: #008000;">.</span><span style="color: #0000FF;">dataStore</span><span style="color: #008000;">.</span><span style="color: #0000FF;">WriteTableInstance</span><span style="color: #008000;">&#40;</span>CostTblPeru2008, 
    PTblCost<span style="color: #008000;">.</span><span style="color: #0000FF;">TableName</span>, trow1<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>The <code>WriteTableInstance()</code> method of <em>DataStore</em> above writes the complete table <code>CostTblPeru2008</code> into the database together with the instance information <code>trow1</code> handling all required database operations behind the scenes.</p>
<p>Persistent Table Array takes this level of abstraction and simplification one step further: All database read or write operations are executed automatically during the ordinary use of table arrays.</p>
<h3>Why are table arrays needed?</h3>
<p>Table arrays are useful in cases in which we need to make calculations with certain set of input tables to obtain the desired set of output tables.</p>
<p>As an example, consider the price calculation below with in-memory tables:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">FinaquantProtos</span><span style="color: #008000;">;</span>
&nbsp;
PTblPrice <span style="color: #008000;">=</span>
    MatrixTable<span style="color: #008000;">.</span><span style="color: #0000FF;">MultiplySelectedKeyFigures</span><span style="color: #008000;">&#40;</span>PTblCost, PTblMargin <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span>,
    InputKeyFigTbl1<span style="color: #008000;">:</span> <span style="color: #666666;">&quot;costs&quot;</span>, InputKeyFigTbl2<span style="color: #008000;">:</span> <span style="color: #666666;">&quot;margin&quot;</span>, OutputKeyFig<span style="color: #008000;">:</span> <span style="color: #666666;">&quot;price&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>Assume you need to calculate product prices for every year with different margin and cost tables. That is, the field structure of margin and cost tables remain the same, but their entries and values may vary from year to year.</p>
<p>Let’s add another dimension to the equation: You have different margin and cost tables for each year and country.</p>
<p>Following code example demonstrates how you can calculate and store price tables for each country and year. You can find this demo function and more in the MS Visual Studio project file FinaquantProtosStarter (see page <a title="finaquant® protos – free .net library with table functions" href="http://finaquant.com/products/finaquantprotos" target="_blank">finaquant® protos</a> for free download).</p>
<span class="collapseomatic" id="id3324"  title="Demo function: Calculate price tables for each country and year">Demo function: Calculate price tables for each country and year</span><div id="target-id3324" class="collapseomatic_content">

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// Calculate price tables for each year and country with Persistent Table Arrays</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">FinaquantProtos</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Persistent_Table_Array<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
<span style="color: #008080; font-style: italic;">// required variables</span>
MetaData md<span style="color: #008000;">;</span>
MatrixTable CostTable, MarginTable1, MarginTable2<span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// call helper to create test tables</span>
Create_Cost_and_Margin_tables<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">out</span> md, <span style="color: #0600FF; font-weight: bold;">out</span> CostTable, <span style="color: #0600FF; font-weight: bold;">out</span> MarginTable1, <span style="color: #0600FF; font-weight: bold;">out</span> MarginTable2<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// Create separate tables for each country and year</span>
MatrixTable CostTblPeru2008 <span style="color: #008000;">=</span> CostTable <span style="color: #008000;">+</span> <span style="color: #FF0000;">0.08</span><span style="color: #008000;">;</span>     <span style="color: #008080; font-style: italic;">// add 0.08 to all key figures of CostTable</span>
MatrixTable MarginTblPeru2008 <span style="color: #008000;">=</span> MarginTable1 <span style="color: #008000;">+</span> <span style="color: #FF0000;">0.0008</span><span style="color: #008000;">;</span>
&nbsp;
MatrixTable CostTblPeru2012 <span style="color: #008000;">=</span> CostTable <span style="color: #008000;">+</span> <span style="color: #FF0000;">0.12</span><span style="color: #008000;">;</span>
MatrixTable MarginTblPeru2012 <span style="color: #008000;">=</span> MarginTable1 <span style="color: #008000;">+</span> <span style="color: #FF0000;">0.0012</span><span style="color: #008000;">;</span>
&nbsp;
MatrixTable CostTblBolivia2010 <span style="color: #008000;">=</span> CostTable <span style="color: #008000;">+</span> <span style="color: #FF0000;">0.10</span><span style="color: #008000;">;</span>
MatrixTable MarginTblBolivia2010 <span style="color: #008000;">=</span> MarginTable1 <span style="color: #008000;">+</span> <span style="color: #FF0000;">0.0010</span><span style="color: #008000;">;</span> 
&nbsp;
<span style="color: #008080; font-style: italic;">// view tables</span>
MatrixTable<span style="color: #008000;">.</span><span style="color: #0000FF;">View_MatrixTable</span><span style="color: #008000;">&#40;</span>CostTblPeru2008, <span style="color: #666666;">&quot;CostTblPeru2008&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
MatrixTable<span style="color: #008000;">.</span><span style="color: #0000FF;">View_MatrixTable</span><span style="color: #008000;">&#40;</span>MarginTblPeru2008, <span style="color: #666666;">&quot;MarginTblPeru2008&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// add new fields to MetaData</span>
MetaData<span style="color: #008000;">.</span><span style="color: #0000FF;">AddNewField</span><span style="color: #008000;">&#40;</span>md, <span style="color: #666666;">&quot;country&quot;</span>, FieldType<span style="color: #008000;">.</span><span style="color: #0000FF;">TextAttribute</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
MetaData<span style="color: #008000;">.</span><span style="color: #0000FF;">AddNewField</span><span style="color: #008000;">&#40;</span>md, <span style="color: #666666;">&quot;year&quot;</span>, FieldType<span style="color: #008000;">.</span><span style="color: #0000FF;">IntegerAttribute</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// define row field structure as key for table instance</span>
<span style="color: #008080; font-style: italic;">// Note: TableInstance = PersistentTableArray[InstanceKey]</span>
<span style="color: #0600FF; font-weight: bold;">var</span> RowFields <span style="color: #008000;">=</span> TextVector<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateVectorWithElements</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;country&quot;</span>, <span style="color: #666666;">&quot;year&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
TableFields InstanceRowFields <span style="color: #008000;">=</span> TableFields<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateTableFields</span><span style="color: #008000;">&#40;</span>RowFields, md<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// Database connection strings: replace them with your own connection strings on your computer</span>
<span style="color: #008080; font-style: italic;">// You need to install ODBC driver for MySQL on your computer (32 bit version)</span>
<span style="color: #008080; font-style: italic;">// see: http://dev.mysql.com/downloads/connector/odbc/ - mysql-connector-odbc-5.2.4-win32</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// MS SQL</span>
<span style="color: #6666cc; font-weight: bold;">string</span> ConnStr1 <span style="color: #008000;">=</span> <span style="color: #666666;">@&quot;Data Source=(local)\SQLEXPRESS;Initial Catalog=finaquantdb;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False&quot;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// MySQL</span>
<span style="color: #6666cc; font-weight: bold;">string</span> ConnStr2 <span style="color: #008000;">=</span> <span style="color: #666666;">@&quot;Driver={MySQL ODBC 5.2w Driver};Server=127.0.0.1;Database=finaquantdb;uid=root;pwd=&quot;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> Connections <span style="color: #008000;">=</span> <span style="color: #008000;">&#123;</span> ConnStr1, ConnStr2<span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// database software for each connection string</span>
<span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> DataBaseSW <span style="color: #008000;">=</span> <span style="color: #008000;">&#123;</span><span style="color: #666666;">&quot;MS SQL&quot;</span>, <span style="color: #666666;">&quot;MySQL&quot;</span><span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// Data providers for each connection string</span>
<span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> DataProviders <span style="color: #008000;">=</span> <span style="color: #008000;">&#123;</span> <span style="color: #666666;">&quot;System.Data.SqlClient&quot;</span>, <span style="color: #666666;">&quot;System.Data.Odbc&quot;</span><span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">//****************************************************************</span>
<span style="color: #008080; font-style: italic;">// Calculate product prices for each country and year</span>
<span style="color: #008080; font-style: italic;">//   and store each table instance separately</span>
<span style="color: #008080; font-style: italic;">//</span>
<span style="color: #008080; font-style: italic;">// Repeat operations for each connection string</span>
<span style="color: #008080; font-style: italic;">//****************************************************************</span>
&nbsp;
Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Calculate product prices for each country and year&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// Repeat same operations for each connection string</span>
<span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> Connections<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Database Software: &quot;</span> <span style="color: #008000;">+</span> DataBaseSW<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot; *******************&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Database Provider: &quot;</span> <span style="color: #008000;">+</span> DataProviders<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Connection String: &quot;</span> <span style="color: #008000;">+</span> Connections<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// TEST</span>
	<span style="color: #000000;">System.<span style="color: #0000FF;">Diagnostics</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Debug</span><span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Database Software: &quot;</span> <span style="color: #008000;">+</span> DataBaseSW<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot; *******************&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// declare persistent table arrays</span>
	PersistentTableArray PTblCost <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
	PersistentTableArray PTblMargin <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
	PersistentTableArray PTblPrice <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// initiate DataStore for database connection</span>
	DataStore datstore <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DataStore<span style="color: #008000;">&#40;</span>Provider<span style="color: #008000;">:</span> DataProviders<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> 
&nbsp;
	<span style="color: #0600FF; font-weight: bold;">try</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #008080; font-style: italic;">// open database connection</span>
		datstore<span style="color: #008000;">.</span><span style="color: #0000FF;">OpenConnection</span><span style="color: #008000;">&#40;</span>Connections<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">// initiate &quot;persistent table array&quot; objects</span>
		PTblCost <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PersistentTableArray<span style="color: #008000;">&#40;</span>TableName<span style="color: #008000;">:</span> <span style="color: #666666;">&quot;costs&quot;</span>, dstore<span style="color: #008000;">:</span> datstore<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		PTblMargin <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PersistentTableArray<span style="color: #008000;">&#40;</span>TableName<span style="color: #008000;">:</span> <span style="color: #666666;">&quot;margins&quot;</span>, dstore<span style="color: #008000;">:</span> datstore<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		PTblPrice <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PersistentTableArray<span style="color: #008000;">&#40;</span>TableName<span style="color: #008000;">:</span> <span style="color: #666666;">&quot;prices&quot;</span>, dstore<span style="color: #008000;">:</span> datstore<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">// Create TableRow objects as instance keys for tables</span>
		TableRow trow1 <span style="color: #008000;">=</span> TableRow<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateTableRowWithElements</span><span style="color: #008000;">&#40;</span>InstanceRowFields, <span style="color: #666666;">&quot;Peru&quot;</span>, <span style="color: #FF0000;">2008</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		TableRow trow2 <span style="color: #008000;">=</span> TableRow<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateTableRowWithElements</span><span style="color: #008000;">&#40;</span>InstanceRowFields, <span style="color: #666666;">&quot;Peru&quot;</span>, <span style="color: #FF0000;">2012</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		TableRow trow3 <span style="color: #008000;">=</span> TableRow<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateTableRowWithElements</span><span style="color: #008000;">&#40;</span>InstanceRowFields, <span style="color: #666666;">&quot;Bolivia&quot;</span>, <span style="color: #FF0000;">2010</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		TableRow trow4 <span style="color: #008000;">=</span> TableRow<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateTableRowWithElements</span><span style="color: #008000;">&#40;</span>InstanceRowFields, <span style="color: #666666;">&quot;Bolivia&quot;</span>, <span style="color: #FF0000;">2012</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">// store each table instance separately into database (yes, just by assigning!)</span>
		PTblCost<span style="color: #008000;">&#91;</span>trow1<span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> CostTblPeru2008<span style="color: #008000;">;</span>      <span style="color: #008080; font-style: italic;">// store CostTblPeru2008 with instance information in trow1</span>
		PTblCost<span style="color: #008000;">&#91;</span>trow2<span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> CostTblPeru2012<span style="color: #008000;">;</span>
		PTblCost<span style="color: #008000;">&#91;</span>trow3<span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> CostTblBolivia2010<span style="color: #008000;">;</span>
&nbsp;
		PTblMargin<span style="color: #008000;">&#91;</span>trow1<span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> MarginTblPeru2008<span style="color: #008000;">;</span>
		PTblMargin<span style="color: #008000;">&#91;</span>trow2<span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> MarginTblPeru2012<span style="color: #008000;">;</span>
		PTblMargin<span style="color: #008000;">&#91;</span>trow3<span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> MarginTblBolivia2010<span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">// now check database to see if these tables are written</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">// read table instances from database</span>
		<span style="color: #008080; font-style: italic;">// &quot;PTblCost[trow1]&quot; reads table named &quot;costs&quot; with instance information in trow1</span>
		MatrixTable<span style="color: #008000;">.</span><span style="color: #0000FF;">View_MatrixTable</span><span style="color: #008000;">&#40;</span>PTblCost<span style="color: #008000;">&#91;</span>trow1<span style="color: #008000;">&#93;</span>, <span style="color: #666666;">&quot;Cost Table, Peru - 2008&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		MatrixTable<span style="color: #008000;">.</span><span style="color: #0000FF;">View_MatrixTable</span><span style="color: #008000;">&#40;</span>PTblCost<span style="color: #008000;">&#91;</span>trow2<span style="color: #008000;">&#93;</span>, <span style="color: #666666;">&quot;Cost Table, Peru - 2012&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		MatrixTable<span style="color: #008000;">.</span><span style="color: #0000FF;">View_MatrixTable</span><span style="color: #008000;">&#40;</span>PTblMargin<span style="color: #008000;">&#91;</span>trow1<span style="color: #008000;">&#93;</span>, <span style="color: #666666;">&quot;Margin Table, Peru - 2008&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		MatrixTable<span style="color: #008000;">.</span><span style="color: #0000FF;">View_MatrixTable</span><span style="color: #008000;">&#40;</span>PTblCost<span style="color: #008000;">&#91;</span>trow3<span style="color: #008000;">&#93;</span>, <span style="color: #666666;">&quot;Cost Table, Bolivia - 2010&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		MatrixTable<span style="color: #008000;">.</span><span style="color: #0000FF;">View_MatrixTable</span><span style="color: #008000;">&#40;</span>PTblMargin<span style="color: #008000;">&#91;</span>trow3<span style="color: #008000;">&#93;</span>, <span style="color: #666666;">&quot;Margin Table, Bolivia - 2010&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">// calculate and store price tables for each instance (country-year)</span>
		PTblPrice<span style="color: #008000;">&#91;</span>trow1<span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span>
			MatrixTable<span style="color: #008000;">.</span><span style="color: #0000FF;">MultiplySelectedKeyFigures</span><span style="color: #008000;">&#40;</span>PTblCost<span style="color: #008000;">&#91;</span>trow1<span style="color: #008000;">&#93;</span>, PTblMargin<span style="color: #008000;">&#91;</span>trow1<span style="color: #008000;">&#93;</span> <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span>,
			InputKeyFigTbl1<span style="color: #008000;">:</span> <span style="color: #666666;">&quot;costs&quot;</span>, InputKeyFigTbl2<span style="color: #008000;">:</span> <span style="color: #666666;">&quot;margin&quot;</span>, OutputKeyFig<span style="color: #008000;">:</span> <span style="color: #666666;">&quot;price&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		PTblPrice<span style="color: #008000;">&#91;</span>trow2<span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span>
			MatrixTable<span style="color: #008000;">.</span><span style="color: #0000FF;">MultiplySelectedKeyFigures</span><span style="color: #008000;">&#40;</span>PTblCost<span style="color: #008000;">&#91;</span>trow2<span style="color: #008000;">&#93;</span>, PTblMargin<span style="color: #008000;">&#91;</span>trow2<span style="color: #008000;">&#93;</span> <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span>,
			InputKeyFigTbl1<span style="color: #008000;">:</span> <span style="color: #666666;">&quot;costs&quot;</span>, InputKeyFigTbl2<span style="color: #008000;">:</span> <span style="color: #666666;">&quot;margin&quot;</span>, OutputKeyFig<span style="color: #008000;">:</span> <span style="color: #666666;">&quot;price&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		PTblPrice<span style="color: #008000;">&#91;</span>trow3<span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span>
			MatrixTable<span style="color: #008000;">.</span><span style="color: #0000FF;">MultiplySelectedKeyFigures</span><span style="color: #008000;">&#40;</span>PTblCost<span style="color: #008000;">&#91;</span>trow3<span style="color: #008000;">&#93;</span>, PTblMargin<span style="color: #008000;">&#91;</span>trow3<span style="color: #008000;">&#93;</span> <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span>,
			InputKeyFigTbl1<span style="color: #008000;">:</span> <span style="color: #666666;">&quot;costs&quot;</span>, InputKeyFigTbl2<span style="color: #008000;">:</span> <span style="color: #666666;">&quot;margin&quot;</span>, OutputKeyFig<span style="color: #008000;">:</span> <span style="color: #666666;">&quot;price&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">// overwrite a table instance with itself</span>
		PTblPrice<span style="color: #008000;">&#91;</span>trow3<span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> PTblPrice<span style="color: #008000;">&#91;</span>trow3<span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">// coply a table instance to another one</span>
		PTblPrice<span style="color: #008000;">&#91;</span>trow4<span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> PTblPrice<span style="color: #008000;">&#91;</span>trow3<span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">// read price tables from database</span>
		MatrixTable<span style="color: #008000;">.</span><span style="color: #0000FF;">View_MatrixTable</span><span style="color: #008000;">&#40;</span>PTblPrice<span style="color: #008000;">&#91;</span>trow1<span style="color: #008000;">&#93;</span>, <span style="color: #666666;">&quot;Price Table, Peru - 2008&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		MatrixTable<span style="color: #008000;">.</span><span style="color: #0000FF;">View_MatrixTable</span><span style="color: #008000;">&#40;</span>PTblPrice<span style="color: #008000;">&#91;</span>trow2<span style="color: #008000;">&#93;</span>, <span style="color: #666666;">&quot;Price Table, Peru - 2012&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		MatrixTable<span style="color: #008000;">.</span><span style="color: #0000FF;">View_MatrixTable</span><span style="color: #008000;">&#40;</span>PTblPrice<span style="color: #008000;">&#91;</span>trow3<span style="color: #008000;">&#93;</span>, <span style="color: #666666;">&quot;Price Table, Bolivia - 2010&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		MatrixTable<span style="color: #008000;">.</span><span style="color: #0000FF;">View_MatrixTable</span><span style="color: #008000;">&#40;</span>PTblPrice<span style="color: #008000;">&#91;</span>trow4<span style="color: #008000;">&#93;</span>, <span style="color: #666666;">&quot;Price Table, Bolivia - 2012 (copy of Bolivia-2010)&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>Exception ex<span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Error in Case 1: &quot;</span> <span style="color: #008000;">+</span> ex<span style="color: #008000;">.</span><span style="color: #0000FF;">Message</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0600FF; font-weight: bold;">finally</span>
	<span style="color: #008000;">&#123;</span>
		Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Close database connection ...&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		datstore<span style="color: #008000;">.</span><span style="color: #0000FF;">CloseConnection</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// wait a bit before showing message box</span>
	<span style="color: #000000;">System.<span style="color: #0000FF;">Threading</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Thread</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Sleep</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">1000</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	MessageBox<span style="color: #008000;">.</span><span style="color: #0000FF;">Show</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Please close all table viewers and press OK to continue ...&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>   <span style="color: #008080; font-style: italic;">// for i</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p></div><br />
There are lots of similar applications in real life where you need to make instance calculations with selected set of input tables. Examples include financial planning (quarterly, yearly, for each region), dealer commissions (monthly, quarterly, yearly, for each product or contract group), estimations or forecasts and so on. In most cases, the required input data are selected with ad-hoc, customized or parameterized SQL statements.</p>
<p><em>Persistent Table Array</em> provides a framework for easily reading and writing complete tables associated with a calculation instance (without needing to care for the underlying database operations) as a calculation engine based on table functions.</p>
<p>Note that no method of the classes <em>PersistentTableArray</em> or <em>DataStore</em> will let you manipulate the entries of individual table instances. Table instances are read, written, overwritten or deleted as a whole in compliance with the philosophy of <a href="http://finaquant.com/help-pages-for-finaquant-products/what-is-a-table-function" title="What is a table function? Why do we need table functions?" target="_blank">table functions</a>.</p>
<h3>Conclusions</h3>
<p>Remember how we define a <a href="http://finaquant.com/help-pages-for-finaquant-products/what-is-a-calculation-engine-based-on-table-functions" title="What is a calculation engine based on table functions?" target="_blank">calculation engine</a> based on table functions (video: <a href="http://finaquant.com/what-is-a-calculation-engine-video/2348" target="_blank">What is a calculation engine? Comparisons with excel sheets (video)</a>):</p>
<p>Table function: Functions with in-memory tables as input and output parameters</p>
<ol>
<li>An extensive library of general-purpose table functions (like standard formulas in excel)</li>
<li>User-defined functions and formulas must be possible (like user-defined formulas in excel)</li>
<li>Ability to store multiple instances of tables in a generic database</li>
</ol>
<p>Up to release 1.03 <a title="finaquant® protos – free .net library with table functions" href="http://finaquant.com/products/finaquantprotos" target="_blank">finaquant® protos</a> already had the features 1 and 2 as listed above. But the last feature 3 was the missing link for a full-featured table calculation engine. </p>
<p>The new classes <em>Persistent Table Array</em> and <em>DataStore</em> with the new release 1.04 completes this missing link.</p>
<p>Persistent Table Array is also the necessary infrastructure of <em>Calculation Nodes and Networks</em> because every <a href="http://finaquant.com/next-step-worker-ant-ika-calculation-node/3238" title="Next step: Worker ant ika (calculation node)" target="_blank">worker ant</a> (as calculation node) should be able to read/write tables from/to a database.</p>
<h3>Related articles</h3>
<ul>
<li><a href="http://finaquant.com/testing-persistent-table-arrays-with-a-mysql-database/3404" target="_blank">Testing Persistent Table Arrays with a MySQL database</a></li>
<li><a href="http://finaquant.com/testing-persistent-table-array-with-a-microsoft-sql-database/3358" target="_blank">Testing Persistent Table Array with a Microsoft SQL database</a></li>
</ul>
<span id="dprv_cp-v2.13" lang="en" xml:lang="en" class="notranslate" style="vertical-align:baseline; padding: 3px 3px 3px 3px; margin-top:2px; margin-bottom:2px; border-collapse:separate; line-height:16px;float:none; font-family: Tahoma, MS Sans Serif; font-size:13px;border:1px solid #bbbbbb;background:#FFFFFF none;display:inline-block;" title="certified 9 April 2013 13:59:56 UTC by Digiprove certificate P390493" ><a href="http://www.digiprove.com/show_certificate.aspx?id=P390493%26guid=IHcXxolZeES32uXsLwYLiw" target="_blank" rel="copyright" style="height:16px; line-height: 16px; border:0px; padding:0px; margin:0px; float:none; display:inline; text-decoration: none; background:transparent none; line-height:normal; font-family: Tahoma, MS Sans Serif; font-style:normal; font-weight:normal; font-size:11px;"><img src="http://finaquant.com/wp-content/plugins/digiproveblog/dp_seal_trans_16x16.png" style="max-width:none !important;vertical-align:-3px; display:inline; border:0px; margin:0px; padding:0px; float:none; background:transparent none" border="0" alt=""/><span style="font-family: Tahoma, MS Sans Serif; font-style:normal; font-size:11px; font-weight:normal; color:#636363; border:0px; float:none; display:inline; text-decoration:none; letter-spacing:normal; padding:0px; padding-left:8px; vertical-align:1px;margin-bottom:2px" onmouseover="this.style.color='#A35353';" onmouseout="this.style.color='#636363';">Copyright&nbsp;secured&nbsp;by&nbsp;Digiprove&nbsp;&copy;&nbsp;2013&nbsp;Tunc&nbsp;Ali&nbsp;Kütükcüoglu</span></a><!--B59DCC8E24EDF42212C04B5A59674780A71B32EFF82FD00AE4220DB199C15142--></span>]]></content:encoded>
			<wfw:commentRss>http://finaquant.com/persistent-table-array/3330/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Testing Persistent Table Arrays with a MySQL database</title>
		<link>http://finaquant.com/testing-persistent-table-arrays-with-a-mysql-database/3404?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=testing-persistent-table-arrays-with-a-mysql-database</link>
		<comments>http://finaquant.com/testing-persistent-table-arrays-with-a-mysql-database/3404#comments</comments>
		<pubDate>Tue, 09 Apr 2013 13:00:50 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Calculation engine]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[table array]]></category>

		<guid isPermaLink="false">http://finaquant.com/?p=3404</guid>
		<description><![CDATA[MySQL is definitely one of the most popular database systems. It is free and open-source, and comparably easy to install and use. The all-in-one installation package makes the installation quite simple. This package also includes MySQL workbench for tasks like &#8230; <a href="http://finaquant.com/testing-persistent-table-arrays-with-a-mysql-database/3404">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p><a title="MySQL - The world's most popular open source database" href="http://www.mysql.com/" target="_blank">MySQL</a> is definitely one of the most popular database systems. It is free and open-source, and comparably easy to install and use.</p>
<p>The all-in-one <a title="MySQL downloads" href="http://dev.mysql.com/downloads/" target="_blank">installation package</a> makes the installation quite simple. This package also includes <a title="MySQL workbench" href="http://www.mysql.com/products/workbench/" target="_blank">MySQL workbench</a> for tasks like executing SQL statements and database administration with a nice graphical user interface (GUI).</p>
<p><a title="Persistent Table Array" href="http://finaquant.com/persistent-table-array/3330" target="_blank">PersistentTableArray</a> class of the non-commercial .net library <a title="finaquant® protos – free .net library with table functions" href="http://finaquant.com/products/finaquantprotos" target="_blank">finaquant® protos</a> can be connected with a relational database like MySQL (or Microsoft SQL) in order to read or write in-memory tables from/to the database together with their instance information.</p>
<p>You may find some interesting articles in internet if you search for <em>MySQL versus SQL Express</em>.</p>
<p><em>PersistentTableArray</em> and <em>DataStore</em> classes of finaquant® protos that are responsible for the database connection don&#8217;t need many widgets and gadgets of a sophisticated commercial database; they are based on most basic standard (ANSI) SQL statements for reading or writing complete tables. So technically, MySQL database is more than enough for persistent table arrays with the advantage that it doesn&#8217;t have the storage space limitations of <a title="Microsoft® SQL Server® 2012 Express" href="http://www.microsoft.com/en-us/download/details.aspx?id=29062#overview" target="_blank">Microsoft SQL Server Express</a>.</p>
<p>You will also need Microsoft Visual Studio (VS) to run the demo function for persistent table arrays included in the VS project file <em>FinaquantProtosStarter</em> that you can download at the product page of <a title="finaquant® protos – free .net library with table functions" href="http://finaquant.com/products/finaquantprotos" target="_blank">finaquant® protos</a>.</p>
<p>Initiating a persistent table array is about setting the correct database connection string and data provider to a <em>DataStore</em> object:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">FinaquantProtos</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Data.Odbc</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// initiate DataStore for database connection</span>
DataStore datstore <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DataStore<span style="color: #008000;">&#40;</span>Provider<span style="color: #008000;">:</span> <span style="color: #666666;">&quot;System.Data.Odbc&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008080; font-style: italic;">// open database connection</span>
<span style="color: #6666cc; font-weight: bold;">string</span> ConnStr <span style="color: #008000;">=</span> <span style="color: #666666;">@&quot;Driver={MySQL ODBC 5.2w Driver};Server=localhost;Database=finaquant;uid=root;pwd=MyPassword&quot;</span><span style="color: #008000;">;</span>
datstore<span style="color: #008000;">.</span><span style="color: #0000FF;">OpenConnection</span><span style="color: #008000;">&#40;</span>ConnStr<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// initiate a persistent table array</span>
<span style="color: #0600FF; font-weight: bold;">var</span> PTblCost <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PersistentTableArray<span style="color: #008000;">&#40;</span>TableName<span style="color: #008000;">:</span> <span style="color: #666666;">&quot;costs&quot;</span>, dstore<span style="color: #008000;">:</span> datstore<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p><span id="more-3404"></span></p>
<h3>The software ingredients you need for the test</h3>
<ul>
<li>A computer with a Windows operating system and .Net 4.0 framework (client version)</li>
<li>Finaquant Protos .net library (release 1.04 or higher)</li>
<li>Microsoft Visual Studio C# (2010 Express or higher versions)</li>
<li>MySQL database (all-in-one installation) and ODBC Driver for MySQL (32 bit version)</li>
<li>Visual Studio project file <em>FinaquantProtosStarter</em> including the related demo function</li>
</ul>
<h3>Steps</h3>
<p>Following steps explain all the preparations required for running the related demo function in <em>FinaquantProtosStarter</em>.</p>
<p>1) Install Microsoft Visual Studio C# 2010 Express(which is free) or a higher version.</p>
<p>For downloads visit: <a title="Microsoft Visual Studio" href="http://www.microsoft.com/visualstudio/eng/downloads" target="_blank">Microsoft Visual Studio</a></p>
<p>Select &#8220;install with a database&#8221; if you are given the option.</p>
<p>2) Download and install the latest release of <a title="finaquant® protos – free .net library with table functions" href="http://finaquant.com/products/finaquantprotos" target="_blank">finaquant® protos</a>.</p>
<p>3) Download and unzip the <a href="http://finaquant.com/download_wdm/28" title="Microsoft Visual Studio file FinaquantProtosStarter including demo functions for getting started with Finaquant Protos, version: 9 Apr 2013, R1.04">MS Visual Studio file FinaquantProtosStarter 1.04</a></p>
<p>You need to be registered and logged in at finaquant.com in in order to download this file.</p>
<p>4) Install MySQL all-in-one package (download at: <a title="MySQL downloads" href="http://dev.mysql.com/downloads/" target="_blank">MySQL downloads</a>)</p>
<p>In the installation user interface you can select what components you need. We recommend you to select:</p>
<ul>
<li>MySQL Server (all files)</li>
<li>Applications (all applications)</li>
<li>Connectors: You need only Connector/ODBC.</li>
</ul>
<p><a href="http://finaquant.com/wp-content/uploads/2013/04/PTArrayWithMySQL_MySQLInstallation.png" rel="lightbox[3404]" title="MySQL all-in-one installation"><img class="alignnone  wp-image-3410" title="MySQL all-in-one installation" alt="" src="http://finaquant.com/wp-content/uploads/2013/04/PTArrayWithMySQL_MySQLInstallation.png" width="259" height="200" /></a><br />
<strong>IMPORTANT:</strong> If you are working on a 64-bit computer you need to download and install 32-bit version of the ODBC connector separately, because even a 64-bit machine may need a 32-bit connector.</p>
<p><a title="Download ODBC drivers for MySQL" href="http://dev.mysql.com/downloads/connector/odbc/" target="_blank">Download ODBC Driver for MySQL</a> (32 bit version, mysql-connector-odbc-5.2.4-win32.msi).</p>
<p>5) Using MySQL Workbench create a new connection and a database named <em>finaquant</em></p>
<p><span class="collapseomatic" id="id6795"  title="See detail steps for help">See detail steps for help</span><div id="target-id6795" class="collapseomatic_content"><br />
In the main window you can click on <em>Manage Connection</em> to update or create database connections. You don’t need to change here anything; you can set a password to the default user <em>root</em> if you want.<br />
<a href="http://finaquant.com/wp-content/uploads/2013/04/PTArrayWithMySQL_ManageConnections.png" rel="lightbox[3404]" title="MySQL workbench main window"><img src="http://finaquant.com/wp-content/uploads/2013/04/PTArrayWithMySQL_ManageConnections-300x206.png" title="MySQL workbench main window" width="300" height="206" class="alignnone size-medium wp-image-3412" /></a></p>
<p>Select your local connection and click on <em>Open Connection to Start Querying</em> to open the SQL Editor window.</p>
<p>Right-click on an existing database (initially test only) and select <em>Create Schema</em> to create the new database named <em>finaquant</em>.<br />
<img src="http://finaquant.com/wp-content/uploads/2013/04/PTArrayWithMySQL_CreateSchema-300x190.png" title="Create a new database" width="300" height="190" class="alignnone size-medium wp-image-3414" /><br />
You have now your new database which is all you need for Persistent Table Array.<br />
</div>&nbsp;<br />
6) Set the database connection string in the demo function <code>Persistent_Table_Array()</code> in code file <em>Demo.cs</em> and run the method which calculates price tables for given instances of cost and margin tables.</p>
<p><span class="collapseomatic" id="id4211"  title="See detail steps for help">See detail steps for help</span><div id="target-id4211" class="collapseomatic_content"><br />
A) Find the demo function <code>public static void Persistent_Table_Array()</code> in code file <em>Demo.cs</em><br />
<img class="alignnone size-full wp-image-3376" title="Find demo function for persistent table arrays" alt="" src="http://finaquant.com/wp-content/uploads/2013/04/PersistentTableArray_FindDemoFunction.png" width="605" height="242" /></p>
<p>B) Paste the connection string in the demo function as updated string value for <code>ConnStr2</code>, and save code file Demo.cs<br />
<a href="http://finaquant.com/wp-content/uploads/2013/04/PTArrayWithMySQL_SetConnectionString.png" rel="lightbox[3404]" title="Update connection string for MySQL database"><img src="http://finaquant.com/wp-content/uploads/2013/04/PTArrayWithMySQL_SetConnectionString-300x54.png" title="Update connection string for MySQL database" width="300" height="54" class="alignnone size-medium wp-image-3416" /></a></p>
<p>C) Out-comment all other demo functions, un-comment <code>DemoFunctions.Persistent_Table_Array()</code> in the code file <em>Program.cs</em> and press F5 to run the demo function.</p>
<p><img class="alignnone size-full wp-image-3380" title="Run demo function" src="http://finaquant.com/wp-content/uploads/2013/04/PersistentTableArray_UnCommentDemoFunc.png" width="462" height="209" /></p>
<p>This demo function should calculate and display price tables for each instance (country and year) of cost and margin tables.<br />
<img class="alignnone size-full wp-image-3381" title="Price tables" alt="" src="http://finaquant.com/wp-content/uploads/2013/04/PersistentTableArray_LotsOfTableViewers.png" width="600" height="335" /><br />
</div>&nbsp;<br />
7) Check the database to see which table are created.</p>
<p>The table named <code>list_of_data_tables</code> has an entry for each instance of data tables.<br />
<a href="http://finaquant.com/wp-content/uploads/2013/04/PTArrayWithMySQL_TableList.png" rel="lightbox[3404]" title="List of all table instances stored in the database"><img src="http://finaquant.com/wp-content/uploads/2013/04/PTArrayWithMySQL_TableList-300x192.png" title="List of all table instances stored in the database" width="300" height="192" class="alignnone size-medium wp-image-3423" /></a></p>
<p>A single table of the database stores multiple instances of in-memory cost tables.<br />
<a href="http://finaquant.com/wp-content/uploads/2013/04/PTArrayWithMySQL_CostTblMultipleInstances1.png" rel="lightbox[3404]" title="Multiple table instances in a single database table"><img src="http://finaquant.com/wp-content/uploads/2013/04/PTArrayWithMySQL_CostTblMultipleInstances1-300x166.png" title="Multiple table instances in a single database table" width="300" height="166" class="alignnone size-medium wp-image-3420" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://finaquant.com/testing-persistent-table-arrays-with-a-mysql-database/3404/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Testing Persistent Table Array with a Microsoft SQL database</title>
		<link>http://finaquant.com/testing-persistent-table-array-with-a-microsoft-sql-database/3358?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=testing-persistent-table-array-with-a-microsoft-sql-database</link>
		<comments>http://finaquant.com/testing-persistent-table-array-with-a-microsoft-sql-database/3358#comments</comments>
		<pubDate>Tue, 09 Apr 2013 12:00:07 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Calculation engine]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[table array]]></category>

		<guid isPermaLink="false">http://finaquant.com/?p=3358</guid>
		<description><![CDATA[With the PersistentTableArray class of the non-commercial .net library finaquant® protos you can read and write in-memory tables from/to a relational database together with their instance information. If you have already installed Microsoft Visual Studio C# 2010 Express or a &#8230; <a href="http://finaquant.com/testing-persistent-table-array-with-a-microsoft-sql-database/3358">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>With the <a title="Persistent Table Array" href="http://finaquant.com/persistent-table-array/3330" target="_blank">PersistentTableArray</a> class of the non-commercial .net library <a title="finaquant® protos – free .net library with table functions" href="http://finaquant.com/products/finaquantprotos" target="_blank">finaquant® protos</a> you can read and write in-memory tables from/to a relational database together with their instance information.</p>
<p>If you have already installed <em>Microsoft Visual Studio C# 2010 Express</em> or a higher version on your computer it is quite easy to get started with persistent table arrays. The installation file of Visual Studio is packed with a free database <a title="Microsoft® SQL Server® 2012 Express" href="http://www.microsoft.com/en-us/download/details.aspx?id=29062#overview" target="_blank">SQL Server Express</a> you don’t need to install a database software separately.</p>
<p>You will need Visual Studio in any case to run the demo function for persistent table arrays in the Visual Studio project file <em>FinaquantProtosStarter</em> which you can download at the product page of <a title="finaquant® protos – free .net library with table functions" href="http://finaquant.com/products/finaquantprotos" target="_blank">finaquant® protos</a>.</p>
<p>Initiating a persistent table array is about setting the correct database connection string and data provider to a <em>DataStore</em> object:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">FinaquantProtos</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Data.SqlClient</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// initiate DataStore for database connection</span>
DataStore datstore <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DataStore<span style="color: #008000;">&#40;</span>Provider<span style="color: #008000;">:</span> <span style="color: #666666;">&quot;System.Data.SqlClient&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// open database connection</span>
<span style="color: #6666cc; font-weight: bold;">string</span> ConnStr <span style="color: #008000;">=</span> “<span style="color: #666666;">@&quot;Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Tuncali\Documents\finaquant.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True&quot;</span>
datstore<span style="color: #008000;">.</span><span style="color: #0000FF;">OpenConnection</span><span style="color: #008000;">&#40;</span>ConnStr<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// initiate a persistent table array</span>
<span style="color: #0600FF; font-weight: bold;">var</span> PTblCost <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PersistentTableArray<span style="color: #008000;">&#40;</span>TableName<span style="color: #008000;">:</span> <span style="color: #666666;">&quot;costs&quot;</span>, dstore<span style="color: #008000;">:</span> datstore<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p><span id="more-3358"></span></p>
<h3>The software ingredients you need for the test</h3>
<ul>
<li>A computer with a Windows operating system and .Net 4.0 framework (client version)</li>
<li>Finaquant Protos .net library (release 1.04 or higher)</li>
<li>Microsoft Visual Studio C# (2010 Express or higher versions) that comes with an MS SQL Server Express database</li>
<li>Visual Studio project file FinaquantProtosStarter with the related demo function</li>
</ul>
<h3>Steps</h3>
<p>Following steps explain all the preparations required for running the related demo function in <em>FinaquantProtosStarter</em>.</p>
<p>1) Install Microsoft Visual Studio C# 2010 Express(which is free) or a higher version.</p>
<p>For downloads visit: <a title="Microsoft Visual Studio" href="http://www.microsoft.com/visualstudio/eng/downloads" target="_blank">Microsoft Visual Studio</a></p>
<p>Select &#8220;install with a database&#8221; if you are given the option.</p>
<p>2) Download and install the latest release of <a title="finaquant® protos – free .net library with table functions" href="http://finaquant.com/products/finaquantprotos" target="_blank">finaquant® protos</a>.</p>
<p>3) Download and unzip the <a href="http://finaquant.com/download_wdm/28" title="Microsoft Visual Studio file FinaquantProtosStarter including demo functions for getting started with Finaquant Protos, version: 9 Apr 2013, R1.04">MS Visual Studio file FinaquantProtosStarter 1.04</a></p>
<p>You need to be registered and logged in at finaquant.com in in order to download this file.</p>
<p>4) Open FinaquantProtosStarter solution file (with Visual Studio) and create a new database named <em>finaquant</em>.</p>
<p><img class="alignnone size-full wp-image-3364" title="New database named finaquant" alt="" src="http://finaquant.com/wp-content/uploads/2013/04/PersistentTableArray_NewDatabase.png" width="392" height="170" /></p>
<p><span class="collapseomatic" id="id3756"  title="See detail steps for help">See detail steps for help</span><div id="target-id3756" class="collapseomatic_content"><br />
A) Open FinaquantProtosStarter solution file (with Visual Studio) by double-clicking on it.<br />
<img class="alignnone size-full wp-image-3365" title="Visual Studio solution file" alt="" src="http://finaquant.com/wp-content/uploads/2013/04/PersistentTableArray_SolutionFile.png" width="605" height="168" /></p>
<p><img class="alignnone size-full wp-image-3367" title="FinaquantProtosStarter opened" alt="" src="http://finaquant.com/wp-content/uploads/2013/04/PersistentTableArray_FPStarterOpened.png" width="605" height="217" /></p>
<p>B) Select <em>Database Explorer</em> from the menu &#8220;View&gt;Other Windows&#8221; to create a new database<br />
<img class="alignnone size-full wp-image-3369" title="Menu path for Database Explorer" alt="" src="http://finaquant.com/wp-content/uploads/2013/04/PersistentTableArray_MenuDatabaseExplorer.png" width="465" height="138" /></p>
<p>Persistent Table Array needs an initially empty and dedicated database.</p>
<p>The menu path can be slightly different in 2012 versions of Visual Studio, like:<br />
View&gt;Other Windows&gt;Data Sources, or View&gt;SQL Server Object Explorer</p>
<p>C) Press icon Connect to Database and …<br />
<img class="alignnone size-full wp-image-3370" title="Press button connect to database" alt="" src="http://finaquant.com/wp-content/uploads/2013/04/PersistentTableArray_ConnectToDatabase.png" width="321" height="154" /></p>
<p>… enter the name of the new database (finaquant) in the opening window. Then press OK without making any other changes.<br />
<img class="alignnone size-full wp-image-3371" title="Add database connection" alt="" src="http://finaquant.com/wp-content/uploads/2013/04/PersistentTableArray_AddConnection.png" width="382" height="444" /></p>
<p>Confirm the next window asking whether the database should be created with OK.</p>
<p>New database named finaquant is created:<br />
<img class="alignnone size-full wp-image-3364" title="New database named finaquant" alt="" src="http://finaquant.com/wp-content/uploads/2013/04/PersistentTableArray_NewDatabase.png" width="392" height="170" /></div>&nbsp;<br />
5) Set the database connection string in the demo function <code>Persistent_Table_Array()</code> in code file <em>Demo.cs</em> and run the method which calculates price tables for given instances of cost and margin tables.</p>
<p><span class="collapseomatic" id="id1882"  title="See detail steps for help">See detail steps for help</span><div id="target-id1882" class="collapseomatic_content"><br />
A) Find the demo function <code>public static void Persistent_Table_Array()</code> in code file <em>Demo.cs</em><br />
<img class="alignnone size-full wp-image-3376" title="Find demo function for persistent table arrays" alt="" src="http://finaquant.com/wp-content/uploads/2013/04/PersistentTableArray_FindDemoFunction.png" width="605" height="242" /></p>
<p>B) In order to get the connection string right-click on the database and select properties.<br />
<img class="alignnone size-full wp-image-3377" title="Database properties" alt="" src="http://finaquant.com/wp-content/uploads/2013/04/PersistentTableArray_DatabaseProperties.png" width="444" height="304" /></p>
<p>C) In the properties window right-click on the field value for connection string, and select <em>Select All</em>, press Control-C to store the connection string to clipboard.<br />
<img class="alignnone size-full wp-image-3378" title="Copy database connection string" alt="" src="http://finaquant.com/wp-content/uploads/2013/04/PersistentTableArray_CopyConnectionString.png" width="361" height="312" /></p>
<p>D) Paste the connection string in the demo function as updated string value for <code>ConnStr1</code>, and save code file Demo.cs<br />
<img class="alignnone size-full wp-image-3379" title="Update connection string in demo function" src="http://finaquant.com/wp-content/uploads/2013/04/PersistentTableArray_UpdateConnectionStringInDemoFunc.png" width="605" height="130" /></p>
<p>E) Out-comment all other demo functions, un-comment <code>DemoFunctions.Persistent_Table_Array()</code> in the code file <em>Program.cs</em> and press F5 to run the demo function.</p>
<p><img class="alignnone size-full wp-image-3380" title="Run demo function" src="http://finaquant.com/wp-content/uploads/2013/04/PersistentTableArray_UnCommentDemoFunc.png" width="462" height="209" /></p>
<p>This demo function should calculate and display price tables for each instance (country and year) of cost and margin tables.<br />
<img class="alignnone size-full wp-image-3381" title="Price tables" alt="" src="http://finaquant.com/wp-content/uploads/2013/04/PersistentTableArray_LotsOfTableViewers.png" width="600" height="335" /><br />
</div><br />
6) Check the database to see which table are created.<br />
<img class="alignnone size-full wp-image-3394" alt="Show table data" src="http://finaquant.com/wp-content/uploads/2013/04/PersistentTableArray_CheckDatabaseContent.png" width="466" height="337" /></p>
<p>The table named <code>list_of_data_tables</code> has an entry for each instance of data tables.<br />
<img class="alignnone size-full wp-image-3395" title="List of table instances" alt="" src="http://finaquant.com/wp-content/uploads/2013/04/PersistentTableArray_TableList.png" width="650" height="250" /></p>
]]></content:encoded>
			<wfw:commentRss>http://finaquant.com/testing-persistent-table-array-with-a-microsoft-sql-database/3358/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Next step: Worker ant ika (calculation node)</title>
		<link>http://finaquant.com/next-step-worker-ant-ika-calculation-node/3238?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=next-step-worker-ant-ika-calculation-node</link>
		<comments>http://finaquant.com/next-step-worker-ant-ika-calculation-node/3238#comments</comments>
		<pubDate>Fri, 01 Mar 2013 21:05:22 +0000</pubDate>
		<dc:creator>tuncalik</dc:creator>
				<category><![CDATA[Finaquant news]]></category>

		<guid isPermaLink="false">http://finaquant.com/?p=3238</guid>
		<description><![CDATA[Which societies were the first farmers? When and where did they emerge? Early civilizations in the Fertile Crescent of Middle East or in China about 9000 BC? Or maybe ancient people in Solomon Islands or in the Highlands of New &#8230; <a href="http://finaquant.com/next-step-worker-ant-ika-calculation-node/3238">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p><a href="http://finaquant.com/wp-content/uploads/2013/03/WorkerAntIka_H400.png" rel="lightbox[3238]" title="worker ant ika - designer: Jeff Cameron Collingwood"><img class="wp-image-3241 alignleft" style="border: 1px solid black;" title="worker ant ika - designer: Jeff Cameron Collingwood" src="http://finaquant.com/wp-content/uploads/2013/03/WorkerAntIka_H400-194x300.png" alt="worker ant ika  - designer: Jeff Cameron Collingwood" width="155" height="240" /></a>Which societies were the first farmers? When and where did they emerge?</p>
<p>Early civilizations in the Fertile Crescent of Middle East or in China about 9000 BC? Or maybe ancient people in Solomon Islands or in the Highlands of New Guinea?</p>
<p>No, <a title="Why are we studying social insects?" href="http://www.life.illinois.edu/social-insects/Social%20Insects.htm" target="_blank">social insects</a> like ants and termites were the first farmers on land; <a title="Leaf-cutter ants" href="http://www.mnn.com/earth-matters/animals/photos/7-animals-that-know-how-to-farm/leaf-cutter-ants" target="_blank">leaf-cutter ants</a> began cultivating a fungus species about 50 million years ago!</p>
<p><span style="color: #993300;">Video: Coevolution of the ant and fungi</span><br />
<iframe src="http://www.youtube.com/embed/R5piJCyHwtw" frameborder="0" width="420" height="315"></iframe></p>
<p>Watch also: <a title="Farming Ants Reveal Evolution Secrets " href="http://www.youtube.com/watch?v=JvtZPOcsKS4" target="_blank">Farming Ants Reveal Evolution Secrets</a></p>
<p>How about the domestication of animals? Which societies were the first breeders and shepherds?<span id="more-3238"></span></p>
<p>Dogs (originally wolves) were tamed and enslaved about 12&#8217;000 years ago. Other herding animals like sheep and cattle were domesticated around 8000 BC. See <a title="HISTORY OF THE DOMESTICATION OF ANIMALS " href="http://www.historyworld.net/wrldhis/plaintexthistories.asp?historyid=ab57" target="_blank">history of animal domestication</a></p>
<p>So, these ancient people were the first animal breeders?</p>
<p>Wrong! <a title="Farmer ants" href="http://www.mnn.com/earth-matters/animals/photos/7-animals-that-know-how-to-farm/farmer-ants" target="_blank">Farmer ants</a> keep domesticated <a title="What are Aphids?" href="http://www.wisegeek.org/what-are-aphids.htm" target="_blank">aphids</a> (small herbivorous insects) for their sugary honeydew since millions of years.</p>
<p><span style="color: #993300;">Video: Crazy Ant Farmers &#8211; Weird Nature &#8211; BBC animals</span></p>
<p><iframe width="640" height="360" src="http://www.youtube.com/embed/43id_NRajDo?feature=oembed" frameborder="0" allowfullscreen></iframe></p>
<p>In fact, with their workers, soldiers, baby sitters, site builders, leaf-cutters and fungus nurses ants were among the first industrial societies. Whereas we humans have education and indoctrination for the definition of roles in the economic life, ants have their genetic imprinting. Both systems work perfectly!</p>
<p>Due to all these wonderful traits ant &#8220;ika&#8221; (a worker ant named ika or shortly &#8220;ant ika&#8221;) is the nickname we have chosen for the upcoming feature for our .net libraries <a title="finaquant® protos – free .net library with table functions" href="http://finaquant.com/products/finaquantprotos" target="_blank">finaquant protos</a> and finaquant calcs, namely <em>calculation node</em>.</p>
<p>Our worker ant ika doesn’t live in forests, grasslands or deserts; it lives in the computers of our users.</p>
<p>We can summarize the advantages of <em>ant ika</em> as follows:</p>
<ul>
<li>Ant ika never complains or rebels; it works and toils tirelessly on the assigned duty for perfection.</li>
<li>Ant ika is never bored of routine work; it never questions its job, or looks for a change.</li>
<li>Ant ika can co-operate with other ants, and build highly efficient <a title="What is a superorganism? " href="http://curiosity.discovery.com/question/what-is-a-superorganism" target="_blank">superorganisms</a> (calculation networks in our case).</li>
<li>Ant ika have many legs (in fact 3 pairs) that can symbolize arrows for input and output tables.</li>
</ul>
<p>All these qualities make our worker ant ika an industrious individual of an industrial society. See this: <a title="INTERESTING FACTS ABOUT ANTS" href="http://lingolex.com/ants.htm" target="_blank">Interesting facts about ants</a></p>
<h2>What is a calculation node? (nickname: ant ika)</h2>
<p>A calculation node encapsulates a high-level table function (also called node function) with a number of input and output parameters. Note that all these parameters are tables without an exception.</p>
<p><img class="alignnone size-full wp-image-3259" title="worker ant ika - calculation node" src="http://finaquant.com/wp-content/uploads/2013/03/WorkerAntIka_CalcNode.png" alt="worker ant ika - calculation node" width="508" height="270" /></p>
<p>Once configured properly a calculation node fetches all the required inputs from the database, executes the assigned table function to generate output tables and writes them into the database.</p>
<p>Ant ika has the ability to calculate and store all the table valued parameters of multiple calculation instances. For example you may want to execute a table function for every month and country for calculating some price estimations.</p>
<p>No problem for ant ika! It fetches all the inputs from the databases you want, executes its table function and writes the results to the databases you want, together with the instance information month and country. Quite useful feature if you need periodic or multidimensional table calculations.</p>
<p>With the addition of calculation node as a new feature (presumably until May 2013) <a title="finaquant® protos – free .net library with table functions" href="http://finaquant.com/products/finaquantprotos" target="_blank">finaquant® protos</a> will be more than a library with table functions; it will become a real <a title="What is a calculation engine based on table functions?" href="http://finaquant.com/help-pages-for-finaquant-products/what-is-a-calculation-engine-based-on-table-functions" target="_blank">calculation engine</a> based on table functions.</p>
<p>The whole range of <a title="What is a table function? Why do we need table functions?" href="http://finaquant.com/help-pages-for-finaquant-products/what-is-a-table-function" target="_blank">table functions</a> in the library can be used to formulate high-level node functions.</p>
<p>What sort of analytical problems can be solved with calculation nodes?</p>
<p>Well, considering the holistic approach of finaquant libraries to numerical computation with its table valued parameters, the question must rather be asked the other way around: Which problems can&#8217;t be solved with calculation nodes?</p>
<p>Possible application areas include estimations and forecasts based on historical data, scenario analysis, simulations and optimizations, fee and commission calculations for provider-dealer networks, strategic financial planning, reporting and so on.</p>
<h2>Calculation networks as superorganisms</h2>
<p>As already mentioned above, calculation nodes (worker ants) can cooperate to build calculation networks.</p>
<p><img class="alignnone size-full wp-image-3261" title="Calculation network - a superorganism of worker ants" src="http://finaquant.com/wp-content/uploads/2013/03/WorkerAntIka_CalcNetwork.png" alt="Calculation network - a superorganism of worker ants" width="486" height="228" /></p>
<p>The output of a node can be input for another node in a calculation network -which is a sort of <em>function tree</em> with table valued parameters.</p>
<p>Calculation networks will be a feature of our (upcoming) commercial product finaquant® calcs. This is a relatively complex feature that will probably be required for only advanced scenarios.</p>
<p>So much to calculation nodes and networks. If you want to know more about the <em>secret life of ants</em><sup><a href="http://finaquant.com/next-step-worker-ant-ika-calculation-node/3238#footnote_0_3238" id="identifier_0_3238" class="footnote-link footnote-identifier-link" title="Secret life of ants: Phrase inspired by the masterful documentary film The Private Life of Plants presented by David Attenborough">1</a></sup> I strongly recommend you to watch this documentary film:<br />
<a title="ANTS - Nature's Secret Power (Full)" href="http://www.youtube.com/watch?v=Z-gIx7LXcQM" target="_blank">ANTS &#8211; Nature&#8217;s Secret Power (Full) </a><br />
<em>Written by: Tunc Ali Kütükcüoglu</em></p>
<h2>References and recommended readings</h2>
<ol>
<li><a href="http://rsj.e-contentmanagement.com/archives/vol/15/issue/2/review/222/first-farmers-the-origins-of-agricultural" title="First Farmers: The Origins of Agricultural Societies" target="_blank">First Farmers: The Origins of Agricultural Societies</a></li>
<li><a href="http://www.ditext.com/diamond/mistake.html" title="The Worst Mistake in the History of the Human Race by Jared Diamond" target="_blank">The Worst Mistake in the History of the Human Race</a> </li>
</ol>
<span id="dprv_cp-v2.13" lang="en" xml:lang="en" class="notranslate" style="vertical-align:baseline; padding: 3px 3px 3px 3px; margin-top:2px; margin-bottom:2px; border-collapse:separate; line-height:16px;float:none; font-family: Tahoma, MS Sans Serif; font-size:13px;border:1px solid #bbbbbb;background:#FFFFFF none;display:inline-block;" title="certified 1 March 2013 21:11:22 UTC by Digiprove certificate P378605" ><a href="http://www.digiprove.com/show_certificate.aspx?id=P378605%26guid=CT2UG142OkywceALd74gDg" target="_blank" rel="copyright" style="height:16px; line-height: 16px; border:0px; padding:0px; margin:0px; float:none; display:inline; text-decoration: none; background:transparent none; line-height:normal; font-family: Tahoma, MS Sans Serif; font-style:normal; font-weight:normal; font-size:11px;"><img src="http://finaquant.com/wp-content/plugins/digiproveblog/dp_seal_trans_16x16.png" style="max-width:none !important;vertical-align:-3px; display:inline; border:0px; margin:0px; padding:0px; float:none; background:transparent none" border="0" alt=""/><span style="font-family: Tahoma, MS Sans Serif; font-style:normal; font-size:11px; font-weight:normal; color:#636363; border:0px; float:none; display:inline; text-decoration:none; letter-spacing:normal; padding:0px; padding-left:8px; vertical-align:1px;margin-bottom:2px" onmouseover="this.style.color='#A35353';" onmouseout="this.style.color='#636363';">Copyright&nbsp;secured&nbsp;by&nbsp;Digiprove&nbsp;&copy;&nbsp;2013&nbsp;Tunc&nbsp;Ali&nbsp;Kütükcüoglu</span></a><!--7461032DB47460A3B7E116EA687E7F2E95038521FDB51E2B75AD49DE0ED1861A--></span><ol class="footnotes"><li id="footnote_0_3238" class="footnote">Secret life of ants: Phrase inspired by the masterful documentary film The Private Life of Plants presented by David Attenborough</li></ol>]]></content:encoded>
			<wfw:commentRss>http://finaquant.com/next-step-worker-ant-ika-calculation-node/3238/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Table data as input to estimation functions in R</title>
		<link>http://finaquant.com/table-data-as-input-to-estimation-functions-in-r/3082?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=table-data-as-input-to-estimation-functions-in-r</link>
		<comments>http://finaquant.com/table-data-as-input-to-estimation-functions-in-r/3082#comments</comments>
		<pubDate>Tue, 19 Feb 2013 17:17:37 +0000</pubDate>
		<dc:creator>tuncalik</dc:creator>
				<category><![CDATA[Calculation engine]]></category>
		<category><![CDATA[integration]]></category>
		<category><![CDATA[predictive model]]></category>
		<category><![CDATA[R]]></category>

		<guid isPermaLink="false">http://finaquant.com/?p=3082</guid>
		<description><![CDATA[As mentioned earlier in related articles, functions written in other languages like R, matlab or ILNumerics can be integrated with table functions using the high-level constructs like function router1 and subtable transformer2. Following example scenarios demonstrate how different estimation functions &#8230; <a href="http://finaquant.com/table-data-as-input-to-estimation-functions-in-r/3082">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>As mentioned earlier in related articles, functions written in other languages like R, matlab or ILNumerics can be integrated with table functions using the high-level constructs like <a href="http://finaquant.com/function-router/2802" title="Function Router" target="_blank">function router</a><sup><a href="http://finaquant.com/table-data-as-input-to-estimation-functions-in-r/3082#footnote_0_3082" id="identifier_0_3082" class="footnote-link footnote-identifier-link" title="A function router applies selected table (or matrix) functions on selected subtables of an input table.">1</a></sup> and <a href="http://finaquant.com/table-functions-for-transforming-subtables/2711" title="Table functions for transforming subtables" target="_blank">subtable transformer</a><sup><a href="http://finaquant.com/table-data-as-input-to-estimation-functions-in-r/3082#footnote_1_3082" id="identifier_1_3082" class="footnote-link footnote-identifier-link" title="A subtable transformer applies the same table (or matrix) function on every subtable of an input table.">2</a></sup>.</p>
<p>Following example scenarios demonstrate how different estimation functions written with the popular free and open-source <a href="http://www.r-project.org/" title="The R Project for Statistical Computing" target="_blank">language R</a> can be applied on selected parts (i.e. subtables) of an input table containing some historical data for price estimations.</p>
<p>You can find all the relevant code in R and C# at the <a href="http://finaquant.com/download" title="Download" target="_blank">download page</a>, including the C# code for generating test data.</p>
<h2>Price estimators in R</h2>
<p>For the example scenarios below we have three estimation functions in R, all based on Linear Regression (LR) of the global indicators <em>market index</em> and <em>oil price</em> for delivering price predictions.</p>
<p><img src="http://finaquant.com/wp-content/uploads/2013/02/TableDataAndR_PricePredictor.png" alt="Price predictor based on Linear Regression (LR)" title="Price predictor based on Linear Regression (LR)" width="499" height="159" class="alignnone size-full wp-image-3088" /></p>
<ol>
<li><em>PriceEstimatorA:</em> Estimated product price based on estimated market index and oil price</li>
<li><em>PriceEstimatorB:</em> Estimated product price based on estimated market index only</li>
<li><em>PriceEstimatorC:</em> Estimated product price based on estimated oil price only</li>
</ol>
<span class="collapseomatic" id="id5098"  title="R code for estimation functions A, B, C">R code for estimation functions A, B, C</span><div id="target-id5098" class="collapseomatic_content">

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080;"># help function for displaying variables</span>
display_variable <span style="color: #008000;">=</span> function<span style="color: #008000;">&#40;</span>v, vname<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> 
print<span style="color: #008000;">&#40;</span>paste<span style="color: #008000;">&#40;</span>vname,<span style="color: #666666;">' ='</span><span style="color: #008000;">&#41;</span>,quote <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">FALSE</span><span style="color: #008000;">&#41;</span>
print<span style="color: #008000;">&#40;</span>v,quote <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">FALSE</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008080;"># Price Estimation Function A</span>
<span style="color: #008080;"># Estimation with 1st order Linear Regression</span>
<span style="color: #008080;"># based on historical market index and oil price</span>
<span style="color: #008080;">#</span>
<span style="color: #008080;"># Columns of input matrix HistoricalData:</span>
<span style="color: #008080;"># market index, oil price, product price</span>
<span style="color: #008080;">#</span>
<span style="color: #008080;"># MarketIndex: Estimated market index for the next year</span>
<span style="color: #008080;"># OilPrice: Estimated oil price for the next year</span>
EstimatedProductPriceA <span style="color: #008000;">=</span> function<span style="color: #008000;">&#40;</span>HistoricalData, MarketIndex, OilPrice<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> 
<span style="color: #008080;"># historical market index and oil price</span>
RowCount <span style="color: #008000;">=</span> nrow<span style="color: #008000;">&#40;</span>HistoricalData<span style="color: #008000;">&#41;</span>
X_train <span style="color: #008000;">=</span> rbind<span style="color: #008000;">&#40;</span>matrix<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">1</span>,<span style="color: #FF0000;">1</span>,RowCount<span style="color: #008000;">&#41;</span>, t<span style="color: #008000;">&#40;</span>HistoricalData<span style="color: #008000;">&#91;</span>,<span style="color: #FF0000;">1</span><span style="color: #008000;">:</span><span style="color: #FF0000;">2</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
&nbsp;
<span style="color: #008080;"># hostorical product prices</span>
Y_train <span style="color: #008000;">=</span> matrix<span style="color: #008000;">&#40;</span>HistoricalData<span style="color: #008000;">&#91;</span>,<span style="color: #FF0000;">3</span><span style="color: #008000;">&#93;</span>, RowCount, <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080;"># optimal coefficient vector to minimize MSE</span>
Bopt <span style="color: #008000;">=</span> solve<span style="color: #008000;">&#40;</span>X_train <span style="color: #008000;">%*%</span> t<span style="color: #008000;">&#40;</span>X_train<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">%*%</span> X_train <span style="color: #008000;">%*%</span> Y_train
&nbsp;
<span style="color: #008080;"># get estimated product price</span>
X_test <span style="color: #008000;">=</span> matrix<span style="color: #008000;">&#40;</span>c<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">1.0</span>, MarketIndex, OilPrice<span style="color: #008000;">&#41;</span>, nrow<span style="color: #008000;">=</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span>
Y_test <span style="color: #008000;">=</span> X_test <span style="color: #008000;">%*%</span> Bopt
<span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span>Y_test<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008080;"># Price Estimator B</span>
<span style="color: #008080;"># Estimation with 1st order Linear Regression</span>
<span style="color: #008080;"># based on historical market index only</span>
<span style="color: #008080;">#</span>
<span style="color: #008080;"># Columns of input matrix HistoricalData:</span>
<span style="color: #008080;"># market index, product price</span>
<span style="color: #008080;">#</span>
<span style="color: #008080;"># MarketIndex: Estimated market index for the next year</span>
EstimatedProductPriceB <span style="color: #008000;">=</span> function<span style="color: #008000;">&#40;</span>HistoricalData, MarketIndex<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> 
<span style="color: #008080;"># historical market index and oil price</span>
RowCount <span style="color: #008000;">=</span> nrow<span style="color: #008000;">&#40;</span>HistoricalData<span style="color: #008000;">&#41;</span>
X_train <span style="color: #008000;">=</span> rbind<span style="color: #008000;">&#40;</span>matrix<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">1</span>,<span style="color: #FF0000;">1</span>,RowCount<span style="color: #008000;">&#41;</span>, t<span style="color: #008000;">&#40;</span>HistoricalData<span style="color: #008000;">&#91;</span>,<span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
&nbsp;
<span style="color: #008080;"># hostorical product prices</span>
Y_train <span style="color: #008000;">=</span> matrix<span style="color: #008000;">&#40;</span>HistoricalData<span style="color: #008000;">&#91;</span>,<span style="color: #FF0000;">2</span><span style="color: #008000;">&#93;</span>, RowCount, <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080;"># optimal coefficient vector to minimize MSE</span>
Bopt <span style="color: #008000;">=</span> solve<span style="color: #008000;">&#40;</span>X_train <span style="color: #008000;">%*%</span> t<span style="color: #008000;">&#40;</span>X_train<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">%*%</span> X_train <span style="color: #008000;">%*%</span> Y_train
&nbsp;
<span style="color: #008080;"># get estimated product price</span>
X_test <span style="color: #008000;">=</span> matrix<span style="color: #008000;">&#40;</span>c<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">1.0</span>, MarketIndex<span style="color: #008000;">&#41;</span>, nrow<span style="color: #008000;">=</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span>
Y_test <span style="color: #008000;">=</span> X_test <span style="color: #008000;">%*%</span> Bopt
<span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span>Y_test<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008080;"># Price Estimator C</span>
<span style="color: #008080;"># Estimation with 1st order Linear Regression</span>
<span style="color: #008080;"># based on historical oil price only</span>
<span style="color: #008080;">#</span>
<span style="color: #008080;"># Columns of matrix HistoricalData:</span>
<span style="color: #008080;"># oil price, product price</span>
<span style="color: #008080;">#</span>
<span style="color: #008080;"># OilPrice: Estimated oil price for the next year</span>
EstimatedProductPriceC <span style="color: #008000;">=</span> function<span style="color: #008000;">&#40;</span>HistoricalData, OilPrice<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> 
<span style="color: #008080;"># historical market index and oil price</span>
RowCount <span style="color: #008000;">=</span> nrow<span style="color: #008000;">&#40;</span>HistoricalData<span style="color: #008000;">&#41;</span>
X_train <span style="color: #008000;">=</span> rbind<span style="color: #008000;">&#40;</span>matrix<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">1</span>,<span style="color: #FF0000;">1</span>,RowCount<span style="color: #008000;">&#41;</span>, t<span style="color: #008000;">&#40;</span>HistoricalData<span style="color: #008000;">&#91;</span>,<span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
&nbsp;
<span style="color: #008080;"># hostorical product prices</span>
Y_train <span style="color: #008000;">=</span> matrix<span style="color: #008000;">&#40;</span>HistoricalData<span style="color: #008000;">&#91;</span>,<span style="color: #FF0000;">2</span><span style="color: #008000;">&#93;</span>, RowCount, <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080;"># optimal coefficient vector to minimize MSE</span>
Bopt <span style="color: #008000;">=</span> solve<span style="color: #008000;">&#40;</span>X_train <span style="color: #008000;">%*%</span> t<span style="color: #008000;">&#40;</span>X_train<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">%*%</span> X_train <span style="color: #008000;">%*%</span> Y_train
&nbsp;
<span style="color: #008080;"># get estimated product price</span>
X_test <span style="color: #008000;">=</span> matrix<span style="color: #008000;">&#40;</span>c<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">1.0</span>, OilPrice<span style="color: #008000;">&#41;</span>, nrow<span style="color: #008000;">=</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span>
Y_test <span style="color: #008000;">=</span> X_test <span style="color: #008000;">%*%</span> Bopt
<span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span>Y_test<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
HistoricalData <span style="color: #008000;">=</span> matrix<span style="color: #008000;">&#40;</span>c<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">1.48</span>, <span style="color: #FF0000;">94.38</span>, <span style="color: #FF0000;">2.89</span>, <span style="color: #FF0000;">0.91</span>, <span style="color: #FF0000;">91.68</span>, <span style="color: #FF0000;">1.83</span>, <span style="color: #FF0000;">1.27</span>, <span style="color: #FF0000;">85.61</span>, <span style="color: #FF0000;">2.74</span><span style="color: #008000;">&#41;</span>, nrow<span style="color: #008000;">=</span><span style="color: #FF0000;">3</span>, byrow<span style="color: #008000;">=</span><span style="color: #0600FF; font-weight: bold;">TRUE</span><span style="color: #008000;">&#41;</span>
print<span style="color: #008000;">&#40;</span><span style="color: #666666;">'HistoricalData'</span><span style="color: #008000;">&#41;</span>
print<span style="color: #008000;">&#40;</span>HistoricalData<span style="color: #008000;">&#41;</span>
&nbsp;
MarketIndex <span style="color: #008000;">=</span> <span style="color: #FF0000;">1.2</span>
OilPrice <span style="color: #008000;">=</span> <span style="color: #FF0000;">100</span>
&nbsp;
<span style="color: #008080;"># method A</span>
EstPrdPrice <span style="color: #008000;">=</span> EstimatedProductPriceA<span style="color: #008000;">&#40;</span>HistoricalData, MarketIndex, OilPrice<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
display_variable<span style="color: #008000;">&#40;</span>EstPrdPrice,<span style="color: #666666;">'Estimated product price with estimator A'</span><span style="color: #008000;">&#41;</span>
&nbsp;
<span style="color: #008080;"># method B</span>
EstPrdPrice <span style="color: #008000;">=</span> EstimatedProductPriceB<span style="color: #008000;">&#40;</span>HistoricalData<span style="color: #008000;">&#91;</span>,c<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">1</span>,<span style="color: #FF0000;">3</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>, MarketIndex<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
display_variable<span style="color: #008000;">&#40;</span>EstPrdPrice,<span style="color: #666666;">'Estimated product price with estimator B'</span><span style="color: #008000;">&#41;</span>
&nbsp;
<span style="color: #008080;"># method C</span>
EstPrdPrice <span style="color: #008000;">=</span> EstimatedProductPriceC<span style="color: #008000;">&#40;</span>HistoricalData<span style="color: #008000;">&#91;</span>,c<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">2</span>,<span style="color: #FF0000;">3</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>, OilPrice<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
display_variable<span style="color: #008000;">&#40;</span>EstPrdPrice,<span style="color: #666666;">'Estimated product price with estimator C'</span><span style="color: #008000;">&#41;</span></pre></td></tr></table></div>

<p></div>&nbsp;</p>
<p>Note that the details of the estimator functions (and whether they make sense in reality) are unimportant for the scenarios below. We will focus on the big picture and integration: How can we apply selected price predictors in R on selected subtables of an input table?<span id="more-3082"></span></p>
<p>You may see the related articles listed below for more information about estimations based on Linear Regression (LR):</p>
<ul>
<li><a href="http://finaquant.com/predictive-modelling-with-linear-regression/520" title="Predictive Modelling with Linear Regression – 1" target="_blank">Predictive Modelling with Linear Regression – 1</a></li>
<li><a href="http://finaquant.com/predictive-modelling-with-linear-regression-2/653" title="Predictive Modelling with Linear Regression – 2" target="_blank">Predictive Modelling with Linear Regression – 2</a></li>
</ul>
<h2>Historical data</h2>
<p>Following tables with historical data are used as input for price estimations:</p>
<ol>
<li>PriceTable: Historical prices of primary products per <em>country</em> and <em>product</em></li>
<li>IndicatorsTable: Historical values of global indicators <em>market index</em> and <em>oil price</em></li>
</ol>
<p><a href="http://finaquant.com/wp-content/uploads/2013/02/TableDataAndR_HistoricalData.png" rel="lightbox[3082]" title="Historical data required for price estimations"><img src="http://finaquant.com/wp-content/uploads/2013/02/TableDataAndR_HistoricalData-300x235.png" alt="Historical data required for price estimations" title="Historical data required for price estimations" width="300" height="235" class="alignnone size-medium wp-image-3100" /></a></p>
<h2>Initializing R functions in C#</h2>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// initialize R connection</span>
<span style="color: #0600FF; font-weight: bold;">var</span> envPath <span style="color: #008000;">=</span> Environment<span style="color: #008000;">.</span><span style="color: #0000FF;">GetEnvironmentVariable</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;PATH&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">var</span> rBinPath <span style="color: #008000;">=</span> <span style="color: #666666;">@&quot;C:\Program Files\R\R-2.14.1\bin\x64&quot;</span><span style="color: #008000;">;</span>  <span style="color: #008080; font-style: italic;">// check this path on your computer</span>
Environment<span style="color: #008000;">.</span><span style="color: #0000FF;">SetEnvironmentVariable</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;PATH&quot;</span>, envPath <span style="color: #008000;">+</span> <span style="color: #000000;">System.<span style="color: #0000FF;">IO</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Path</span><span style="color: #008000;">.</span><span style="color: #0000FF;">PathSeparator</span> <span style="color: #008000;">+</span> rBinPath<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
REngine r_engine <span style="color: #008000;">=</span> REngine<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateInstance</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;RDotNet&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
r_engine<span style="color: #008000;">.</span><span style="color: #0000FF;">Initialize</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// initialize price estimation functions in R</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// price estimator A</span>
<span style="color: #6666cc; font-weight: bold;">string</span> funcstr <span style="color: #008000;">=</span> <span style="color: #666666;">@&quot;EstProdPriceFuncA &lt;- function(HistData, EstMarketInd, EstOilPrice) { 
		RowCount = nrow(HistData);
		X_train = rbind(matrix(1,1,RowCount), t(HistData[,1:2]));
		Y_train = matrix(HistData[,3], RowCount, 1);
		Bopt = solve(X_train %*% t(X_train)) %*% X_train %*% Y_train;
		X_test = matrix(c(1.0, EstMarketInd, EstOilPrice), nrow=1);
		Y_test = X_test %*% Bopt;
		return (Y_test);
		}&quot;</span><span style="color: #008000;">;</span>
Function EstProdPriceFuncA <span style="color: #008000;">=</span> r_engine<span style="color: #008000;">.</span><span style="color: #0000FF;">Evaluate</span><span style="color: #008000;">&#40;</span>funcstr<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">AsFunction</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// price estimator B</span>
funcstr <span style="color: #008000;">=</span> <span style="color: #666666;">@&quot;EstProdPriceFuncB &lt;- function(HistData, EstMarketInd) { 
	RowCount = nrow(HistData);
	X_train = rbind(matrix(1,1,RowCount), t(HistData[,1]));
	Y_train = matrix(HistData[,2], RowCount, 1);
	Bopt = solve(X_train %*% t(X_train)) %*% X_train %*% Y_train;
	X_test = matrix(c(1.0, EstMarketInd), nrow=1);
	Y_test = X_test %*% Bopt;
	return (Y_test);
	}&quot;</span><span style="color: #008000;">;</span>
Function EstProdPriceFuncB <span style="color: #008000;">=</span> r_engine<span style="color: #008000;">.</span><span style="color: #0000FF;">Evaluate</span><span style="color: #008000;">&#40;</span>funcstr<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">AsFunction</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// price estimator C</span>
funcstr <span style="color: #008000;">=</span> <span style="color: #666666;">@&quot;EstProdPriceFuncC &lt;- function(HistData, EstOilPrice) { 
	RowCount = nrow(HistData);
	X_train = rbind(matrix(1,1,RowCount), t(HistData[,1]));
	Y_train = matrix(HistData[,2], RowCount, 1);
	Bopt = solve(X_train %*% t(X_train)) %*% X_train %*% Y_train;
	X_test = matrix(c(1.0, EstOilPrice), nrow=1);
	Y_test = X_test %*% Bopt;
	return (Y_test);
	}&quot;</span><span style="color: #008000;">;</span>
Function EstProdPriceFuncC <span style="color: #008000;">=</span> r_engine<span style="color: #008000;">.</span><span style="color: #0000FF;">Evaluate</span><span style="color: #008000;">&#40;</span>funcstr<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">AsFunction</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<h2>Integration of R with table functions in C#</h2>
<p>See <a href="http://finaquant.com/calling-user-defined-functions-in-r-from-within-c/3060" title="Calling user-defined functions in R from within c#" target="_blank">related article</a> for the installation of <a href="http://rdotnet.codeplex.com/" title="R.NET" target="_blank">R.NET</a> that we use here for the integration of C# and R. </p>
<p>Function routers and subtable transformers expect delegate table functions as input parameters in the following format:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// Generic delegate table function</span>
<span style="color: #008080; font-style: italic;">// InputTbl: Input table</span>
<span style="color: #008080; font-style: italic;">// OtherParameters: Other parameters of any type</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">delegate</span> MatrixTable TransformTableFunc_OP<span style="color: #008000;">&#40;</span>MatrixTable InputTbl, 
	<span style="color: #0600FF; font-weight: bold;">params</span> <span style="color: #6666cc; font-weight: bold;">Object</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> OtherParameters<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>The initialized REngine and estimation functions are passed to the enveloping table functions (with the signature above) as additional parameters. The R functions are then called within the enveloping table functions. You may find the code examples below for these table functions:</p>
<span class="collapseomatic" id="id2961"  title="C# code for table function PriceEstimatorA()">C# code for table function PriceEstimatorA()</span><div id="target-id2961" class="collapseomatic_content"></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">FinaquantProtos</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">RDotNet</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// Table function for price estimation, estimator A (LR based on global market index and oil price)</span>
<span style="color: #008080; font-style: italic;">// Input parameters:</span>
<span style="color: #008080; font-style: italic;">// InputTbl: Historical product prices with fields year and price</span>
<span style="color: #008080; font-style: italic;">// OtherParameters[0]: Table of historical indicators with fields year, market_index and oil_price</span>
<span style="color: #008080; font-style: italic;">// OtherParameters[1]: REngine</span>
<span style="color: #008080; font-style: italic;">// OtherParameters[2]: R Function</span>
<span style="color: #008080; font-style: italic;">// OtherParameters[3]: Estimated market index for next year (double)</span>
<span style="color: #008080; font-style: italic;">// OtherParameters[4]: Estimated oil price for next year (double)</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> MatrixTable PriceEstimatorA<span style="color: #008000;">&#40;</span>MatrixTable InputTbl, <span style="color: #0600FF; font-weight: bold;">params</span> <span style="color: #6666cc; font-weight: bold;">Object</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> OtherParameters<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #008080; font-style: italic;">// get all input parameters</span>
	MatrixTable HistoricalProductPrices <span style="color: #008000;">=</span> InputTbl<span style="color: #008000;">;</span>
	MatrixTable HistoricalIndicators <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>MatrixTable<span style="color: #008000;">&#41;</span>OtherParameters<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
	REngine engine <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>REngine<span style="color: #008000;">&#41;</span>OtherParameters<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
	Function EstProdPriceFuncA <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>Function<span style="color: #008000;">&#41;</span>OtherParameters<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">2</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
	<span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> EstMarketInd <span style="color: #008000;">=</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#41;</span> OtherParameters<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">3</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
	<span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> EstOilPrice <span style="color: #008000;">=</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#41;</span> OtherParameters<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">4</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// combine tables</span>
	MatrixTable HistoricalDataTbl <span style="color: #008000;">=</span> MatrixTable<span style="color: #008000;">.</span><span style="color: #0000FF;">CombineTables</span><span style="color: #008000;">&#40;</span>HistoricalIndicators, HistoricalProductPrices<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// get matrix of key figures from table</span>
	KeyMatrix HistData <span style="color: #008000;">=</span> HistoricalDataTbl<span style="color: #008000;">.</span><span style="color: #0000FF;">KeyFigValues</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// convert data types of input parameters from C# to R</span>
	NumericMatrix HistDataR <span style="color: #008000;">=</span> engine<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateNumericMatrix</span><span style="color: #008000;">&#40;</span>HistData<span style="color: #008000;">.</span><span style="color: #0000FF;">toArray</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	engine<span style="color: #008000;">.</span><span style="color: #0000FF;">SetSymbol</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;HistDataR&quot;</span>, HistDataR<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	NumericVector EstMarketIndR <span style="color: #008000;">=</span> engine<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateNumericVector</span><span style="color: #008000;">&#40;</span>EstMarketInd<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	engine<span style="color: #008000;">.</span><span style="color: #0000FF;">SetSymbol</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;EstMarketIndR&quot;</span>, EstMarketIndR<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	NumericVector EstOilPriceR <span style="color: #008000;">=</span> engine<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateNumericVector</span><span style="color: #008000;">&#40;</span>EstOilPrice<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	engine<span style="color: #008000;">.</span><span style="color: #0000FF;">SetSymbol</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;EstOilPriceR&quot;</span>, EstOilPriceR<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// call function in R from c#</span>
	NumericMatrix EstProductPrice <span style="color: #008000;">=</span> engine<span style="color: #008000;">.</span><span style="color: #0000FF;">Evaluate</span><span style="color: #008000;">&#40;</span>
	<span style="color: #666666;">@&quot;EstProductPrice &lt;- EstProdPriceFuncA(HistDataR,EstMarketIndR,EstOilPriceR)&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">AsNumericMatrix</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// return a single-element table with estimated product price</span>
	<span style="color: #6666cc; font-weight: bold;">double</span> EstProdPrice <span style="color: #008000;">=</span> EstProductPrice<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// return a single-element table with estimated product price</span>
	TableFields tf <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> TableFields<span style="color: #008000;">&#40;</span>InputTbl<span style="color: #008000;">.</span><span style="color: #0000FF;">metaData</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	tf<span style="color: #008000;">.</span><span style="color: #0000FF;">AddNewField</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;product_price&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	MatrixTable ResultTbl <span style="color: #008000;">=</span> MatrixTable<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateTableWithElements_A</span><span style="color: #008000;">&#40;</span>tf, EstProdPrice<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF; font-weight: bold;">return</span> ResultTbl<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p></div>
<div style="clear: both"></div>
<span class="collapseomatic" id="id1710"  title="C# code for table function PriceEstimatorB()">C# code for table function PriceEstimatorB()</span><div id="target-id1710" class="collapseomatic_content"></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// Table function for price estimation, estimator B (LR based on global market index only)</span>
<span style="color: #008080; font-style: italic;">// Input parameters:</span>
<span style="color: #008080; font-style: italic;">// InputTbl: Historical product prices with fields year and price</span>
<span style="color: #008080; font-style: italic;">// OtherParameters[0]: Table of historical indicators with fields year, market_index and oil_price</span>
<span style="color: #008080; font-style: italic;">// OtherParameters[1]: REngine</span>
<span style="color: #008080; font-style: italic;">// OtherParameters[2]: R Function</span>
<span style="color: #008080; font-style: italic;">// OtherParameters[3]: Estimated market index for next year (double)</span>
<span style="color: #008080; font-style: italic;">// OtherParameters[4]: Estimated oil price for next year (double)</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> MatrixTable PriceEstimatorB<span style="color: #008000;">&#40;</span>MatrixTable InputTbl, <span style="color: #0600FF; font-weight: bold;">params</span> <span style="color: #6666cc; font-weight: bold;">Object</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> OtherParameters<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #008080; font-style: italic;">// get all input parameters</span>
	MatrixTable HistoricalProductPrices <span style="color: #008000;">=</span> InputTbl<span style="color: #008000;">;</span>
	MatrixTable HistoricalIndicators <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>MatrixTable<span style="color: #008000;">&#41;</span>OtherParameters<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
	REngine engine <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>REngine<span style="color: #008000;">&#41;</span>OtherParameters<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
	Function EstProdPriceFuncB <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>Function<span style="color: #008000;">&#41;</span>OtherParameters<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">2</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
	<span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> EstMarketInd <span style="color: #008000;">=</span> <span style="color: #008000;">&#123;</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#41;</span>OtherParameters<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">3</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
	<span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> EstOilPrice <span style="color: #008000;">=</span> <span style="color: #008000;">&#123;</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#41;</span>OtherParameters<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">4</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// combine tables</span>
	MatrixTable HistoricalDataTbl <span style="color: #008000;">=</span> MatrixTable<span style="color: #008000;">.</span><span style="color: #0000FF;">CombineTables</span><span style="color: #008000;">&#40;</span>HistoricalIndicators, HistoricalProductPrices<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// exclude column oil_price from table</span>
	HistoricalDataTbl <span style="color: #008000;">=</span> MatrixTable<span style="color: #008000;">.</span><span style="color: #0000FF;">ExcludeColumns</span><span style="color: #008000;">&#40;</span>HistoricalDataTbl, 
		TextVector<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateVectorWithElements</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;oil_price&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// get matrix of key figures from table</span>
	KeyMatrix HistData <span style="color: #008000;">=</span> HistoricalDataTbl<span style="color: #008000;">.</span><span style="color: #0000FF;">KeyFigValues</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// convert data types of input parameters from C# to R</span>
	NumericMatrix HistDataR <span style="color: #008000;">=</span> engine<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateNumericMatrix</span><span style="color: #008000;">&#40;</span>HistData<span style="color: #008000;">.</span><span style="color: #0000FF;">toArray</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	engine<span style="color: #008000;">.</span><span style="color: #0000FF;">SetSymbol</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;HistDataR&quot;</span>, HistDataR<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	NumericVector EstMarketIndR <span style="color: #008000;">=</span> engine<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateNumericVector</span><span style="color: #008000;">&#40;</span>EstMarketInd<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	engine<span style="color: #008000;">.</span><span style="color: #0000FF;">SetSymbol</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;EstMarketIndR&quot;</span>, EstMarketIndR<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	NumericVector EstOilPriceR <span style="color: #008000;">=</span> engine<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateNumericVector</span><span style="color: #008000;">&#40;</span>EstOilPrice<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	engine<span style="color: #008000;">.</span><span style="color: #0000FF;">SetSymbol</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;EstOilPriceR&quot;</span>, EstOilPriceR<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// call function in R from c#</span>
	NumericMatrix EstProductPrice <span style="color: #008000;">=</span> engine<span style="color: #008000;">.</span><span style="color: #0000FF;">Evaluate</span><span style="color: #008000;">&#40;</span>
	<span style="color: #666666;">@&quot;EstProductPrice &lt;- EstProdPriceFuncB(HistDataR,EstMarketIndR)&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">AsNumericMatrix</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// return a single-element table with estimated product price</span>
	<span style="color: #6666cc; font-weight: bold;">double</span> EstProdPrice <span style="color: #008000;">=</span> EstProductPrice<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// return a single-element table with estimated product price</span>
	TableFields tf <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> TableFields<span style="color: #008000;">&#40;</span>InputTbl<span style="color: #008000;">.</span><span style="color: #0000FF;">metaData</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	tf<span style="color: #008000;">.</span><span style="color: #0000FF;">AddNewField</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;product_price&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	MatrixTable ResultTbl <span style="color: #008000;">=</span> MatrixTable<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateTableWithElements_A</span><span style="color: #008000;">&#40;</span>tf, EstProdPrice<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF; font-weight: bold;">return</span> ResultTbl<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p></div>
<div style="clear: both"></div>
<span class="collapseomatic" id="id4605"  title="C# code for table function PriceEstimatorC()">C# code for table function PriceEstimatorC()</span><div id="target-id4605" class="collapseomatic_content">

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// Table function for price estimation, estimator C (LR based on global oil price only)</span>
<span style="color: #008080; font-style: italic;">// Input parameters:</span>
<span style="color: #008080; font-style: italic;">// InputTbl: Historical product prices with fields year and price</span>
<span style="color: #008080; font-style: italic;">// OtherParameters[0]: Table of historical indicators with fields year, market_index and oil_price</span>
<span style="color: #008080; font-style: italic;">// OtherParameters[1]: REngine</span>
<span style="color: #008080; font-style: italic;">// OtherParameters[2]: R Function</span>
<span style="color: #008080; font-style: italic;">// OtherParameters[3]: Estimated market index for next year (double)</span>
<span style="color: #008080; font-style: italic;">// OtherParameters[4]: Estimated oil price for next year (double)</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> MatrixTable PriceEstimatorC<span style="color: #008000;">&#40;</span>MatrixTable InputTbl, <span style="color: #0600FF; font-weight: bold;">params</span> <span style="color: #6666cc; font-weight: bold;">Object</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> OtherParameters<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #008080; font-style: italic;">// get all input parameters</span>
	MatrixTable HistoricalProductPrices <span style="color: #008000;">=</span> InputTbl<span style="color: #008000;">;</span>
	MatrixTable HistoricalIndicators <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>MatrixTable<span style="color: #008000;">&#41;</span>OtherParameters<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
	REngine engine <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>REngine<span style="color: #008000;">&#41;</span>OtherParameters<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
	Function EstProdPriceFuncC <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>Function<span style="color: #008000;">&#41;</span>OtherParameters<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">2</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
	<span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> EstMarketInd <span style="color: #008000;">=</span> <span style="color: #008000;">&#123;</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#41;</span>OtherParameters<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">3</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
	<span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> EstOilPrice <span style="color: #008000;">=</span> <span style="color: #008000;">&#123;</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#41;</span>OtherParameters<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">4</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// combine tables</span>
	MatrixTable HistoricalDataTbl <span style="color: #008000;">=</span> MatrixTable<span style="color: #008000;">.</span><span style="color: #0000FF;">CombineTables</span><span style="color: #008000;">&#40;</span>HistoricalIndicators, HistoricalProductPrices<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// exclude column market_index from table</span>
	HistoricalDataTbl <span style="color: #008000;">=</span> MatrixTable<span style="color: #008000;">.</span><span style="color: #0000FF;">ExcludeColumns</span><span style="color: #008000;">&#40;</span>HistoricalDataTbl,
		TextVector<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateVectorWithElements</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;market_index&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// get matrix of key figures from table</span>
	KeyMatrix HistData <span style="color: #008000;">=</span> HistoricalDataTbl<span style="color: #008000;">.</span><span style="color: #0000FF;">KeyFigValues</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// convert data types of input parameters from C# to R</span>
	NumericMatrix HistDataR <span style="color: #008000;">=</span> engine<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateNumericMatrix</span><span style="color: #008000;">&#40;</span>HistData<span style="color: #008000;">.</span><span style="color: #0000FF;">toArray</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	engine<span style="color: #008000;">.</span><span style="color: #0000FF;">SetSymbol</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;HistDataR&quot;</span>, HistDataR<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	NumericVector EstMarketIndR <span style="color: #008000;">=</span> engine<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateNumericVector</span><span style="color: #008000;">&#40;</span>EstMarketInd<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	engine<span style="color: #008000;">.</span><span style="color: #0000FF;">SetSymbol</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;EstMarketIndR&quot;</span>, EstMarketIndR<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	NumericVector EstOilPriceR <span style="color: #008000;">=</span> engine<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateNumericVector</span><span style="color: #008000;">&#40;</span>EstOilPrice<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	engine<span style="color: #008000;">.</span><span style="color: #0000FF;">SetSymbol</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;EstOilPriceR&quot;</span>, EstOilPriceR<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// call function in R from c#</span>
	NumericMatrix EstProductPrice <span style="color: #008000;">=</span> engine<span style="color: #008000;">.</span><span style="color: #0000FF;">Evaluate</span><span style="color: #008000;">&#40;</span>
	<span style="color: #666666;">@&quot;EstProductPrice &lt;- EstProdPriceFuncC(HistDataR,EstOilPriceR)&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">AsNumericMatrix</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// return a single-element table with estimated product price</span>
	<span style="color: #6666cc; font-weight: bold;">double</span> EstProdPrice <span style="color: #008000;">=</span> EstProductPrice<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// return a single-element table with estimated product price</span>
	TableFields tf <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> TableFields<span style="color: #008000;">&#40;</span>InputTbl<span style="color: #008000;">.</span><span style="color: #0000FF;">metaData</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	tf<span style="color: #008000;">.</span><span style="color: #0000FF;">AddNewField</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;product_price&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	MatrixTable ResultTbl <span style="color: #008000;">=</span> MatrixTable<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateTableWithElements_A</span><span style="color: #008000;">&#40;</span>tf, EstProdPrice<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF; font-weight: bold;">return</span> ResultTbl<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p></div>&nbsp;</p>
<h3>Scenario 1: Applying PriceEstimatorA on a single subtable for country-product pair &#8220;Utopia&#8221; and &#8220;bread&#8221;</h3>
<p>This example is designed rather for testing if the table function <em>PriceEstimatorA()</em> which envelopes the corresponding R function works as expected.</p>
<p><img src="http://finaquant.com/wp-content/uploads/2013/02/TableDataAndR_SubtableUtopiaBread.png" alt="Subtable for Utopia and bread" title="Subtable for Utopia and bread" width="313" height="347" class="alignnone size-full wp-image-3119" /></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// apply table function on a single subtable for country-product pair &quot;Utopia&quot;, &quot;bread&quot;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// filter TemperatureTable with condition table to obtain a subtable</span>
<span style="color: #0600FF; font-weight: bold;">var</span> CondTblFields <span style="color: #008000;">=</span> TableFields<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateEmptyTableFields</span><span style="color: #008000;">&#40;</span>md<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
TableFields<span style="color: #008000;">.</span><span style="color: #0000FF;">AddNewField</span><span style="color: #008000;">&#40;</span>CondTblFields, <span style="color: #666666;">&quot;country&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
TableFields<span style="color: #008000;">.</span><span style="color: #0000FF;">AddNewField</span><span style="color: #008000;">&#40;</span>CondTblFields, <span style="color: #666666;">&quot;product&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">var</span> CondTbl <span style="color: #008000;">=</span> MatrixTable<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateTableWithElements_A</span><span style="color: #008000;">&#40;</span>CondTblFields,
	<span style="color: #666666;">&quot;Utopia&quot;</span>, <span style="color: #666666;">&quot;bread&quot;</span>
	<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">var</span> SubTable <span style="color: #008000;">=</span> MatrixTable<span style="color: #008000;">.</span><span style="color: #0000FF;">FilterTableA</span><span style="color: #008000;">&#40;</span>PriceTable, CondTbl<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// exclude fields country and product from subtable</span>
SubTable <span style="color: #008000;">=</span> MatrixTable<span style="color: #008000;">.</span><span style="color: #0000FF;">ExcludeColumns</span><span style="color: #008000;">&#40;</span>SubTable,
	TextVector<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateVectorWithElements</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;country&quot;</span>, <span style="color: #666666;">&quot;product&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// view subtable</span>
<span style="color: #008080; font-style: italic;">// MatrixTable.View_MatrixTable(SubTable, &quot;Subtable of PriceTable (Utopia, bread)&quot;);</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// execute user-defined table function to calculate expected energy consumption (Sedrun, 2012)</span>
MatrixTable EstimatedPriceTbl <span style="color: #008000;">=</span> PriceEstimatorA<span style="color: #008000;">&#40;</span>SubTable, GlobalIndTable, 
	r_engine, EstProdPriceFuncA, <span style="color: #FF0000;">1.2</span>, <span style="color: #FF0000;">90.0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// view result table</span>
MatrixTable<span style="color: #008000;">.</span><span style="color: #0000FF;">View_MatrixTable</span><span style="color: #008000;">&#40;</span>EstimatedPriceTbl, <span style="color: #666666;">&quot;Estimated price for subtable (Utopia, bread)&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>Result table:<br />
<img src="http://finaquant.com/wp-content/uploads/2013/02/TableDataAndR_UtopiaBreadResultTbl.png" alt="Result table for subtable Utopia and bread" title="Result table for subtable Utopia and bread" width="261" height="87" class="alignnone size-full wp-image-3121" /></p>
<p>Yes, the table function PriceEstimatorA() works as expected.</p>
<h3>Scenario 2: Applying PriceEstimatorA on every subtable of price table</h3>
<p>In this example, the same prediction function is applied for all countries and products. </p>
<p>Applying the same table (or matrix) function on all subtables of an input table&#8230; This is what a <a href="http://finaquant.com/table-functions-for-transforming-subtables/2711" title="Table functions for transforming subtables" target="_blank">subtable transformer</a> is created for.</p>
<p><img src="http://finaquant.com/wp-content/uploads/2013/02/TableDataAndR_SubtableTransformer.png" alt="Applying same estimation function on all subtables" title="Applying same estimation function on all subtables" width="602" height="283" class="alignnone size-full wp-image-3128" /></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// Apply the same table function on all subtables of Price Table</span>
<span style="color: #008080; font-style: italic;">// subtable transformer</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// define subtable fields</span>
<span style="color: #0600FF; font-weight: bold;">var</span> SubTblFields <span style="color: #008000;">=</span> TableFields<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateEmptyTableFields</span><span style="color: #008000;">&#40;</span>md<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
TableFields<span style="color: #008000;">.</span><span style="color: #0000FF;">AddNewField</span><span style="color: #008000;">&#40;</span>SubTblFields, <span style="color: #666666;">&quot;year&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
TableFields<span style="color: #008000;">.</span><span style="color: #0000FF;">AddNewField</span><span style="color: #008000;">&#40;</span>SubTblFields, <span style="color: #666666;">&quot;product_price&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #6666cc; font-weight: bold;">double</span> EstMarketIndex <span style="color: #008000;">=</span> <span style="color: #FF0000;">1.2</span><span style="color: #008000;">;</span>
<span style="color: #6666cc; font-weight: bold;">double</span> EstOilPrice <span style="color: #008000;">=</span> <span style="color: #FF0000;">100.0</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// execute subtable transformer</span>
MatrixTable ResultTbl <span style="color: #008000;">=</span> MatrixTable<span style="color: #008000;">.</span><span style="color: #0000FF;">TransformSubTables</span><span style="color: #008000;">&#40;</span>PriceTable, SubTblFields,
	PriceEstimatorA, GlobalIndTable, r_engine, EstProdPriceFuncA, EstMarketIndex, EstOilPrice<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// view result table</span>
MatrixTable<span style="color: #008000;">.</span><span style="color: #0000FF;">View_MatrixTable</span><span style="color: #008000;">&#40;</span>MatrixTable<span style="color: #008000;">.</span><span style="color: #0000FF;">Round</span><span style="color: #008000;">&#40;</span>ResultTbl,<span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span>, 
	<span style="color: #666666;">&quot;Estimator-A applied for all countries and products&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>Result table:<br />
<img src="http://finaquant.com/wp-content/uploads/2013/02/TableDataAndR_SubtableTransformerResult.png" alt="Result table for 2. scenario" title="Result table for 2. scenario" width="382" height="327" class="alignnone size-full wp-image-3126" /></p>
<h3>Scenario 3: Applying different price estimators to different countries and products</h3>
<p>This is where the fabulous <a href="http://finaquant.com/function-router/2802" title="Function Router" target="_blank">function router</a> comes into play.</p>
<p>What we want to do is summarized in the following table:<br />
<img src="http://finaquant.com/wp-content/uploads/2013/02/TableDataAndR_FunctionTableS3.png" alt="Function table, 3. scenario" title="Function table, 3. scenario" width="452" height="140" class="alignnone size-full wp-image-3131" /></p>
<p>In the result table we want to see explicitly which estimation function is used to generate each estimation.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// Function Router and R</span>
<span style="color: #008080; font-style: italic;">// extend MetaData</span>
md<span style="color: #008000;">.</span><span style="color: #0000FF;">AddNewField</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;estimation_method&quot;</span>, FieldType<span style="color: #008000;">.</span><span style="color: #0000FF;">TextAttribute</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
md<span style="color: #008000;">.</span><span style="color: #0000FF;">AddNewField</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;country_cell&quot;</span>, FieldType<span style="color: #008000;">.</span><span style="color: #0000FF;">TextAttribute</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
md<span style="color: #008000;">.</span><span style="color: #0000FF;">AddNewField</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;product_cell&quot;</span>, FieldType<span style="color: #008000;">.</span><span style="color: #0000FF;">TextAttribute</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// create condition matrix table</span>
CondTblFields <span style="color: #008000;">=</span> TableFields<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateEmptyTableFields</span><span style="color: #008000;">&#40;</span>md<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
TableFields<span style="color: #008000;">.</span><span style="color: #0000FF;">AddNewField</span><span style="color: #008000;">&#40;</span>CondTblFields, <span style="color: #666666;">&quot;country_cell&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
TableFields<span style="color: #008000;">.</span><span style="color: #0000FF;">AddNewField</span><span style="color: #008000;">&#40;</span>CondTblFields, <span style="color: #666666;">&quot;product_cell&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">var</span> CondMatTbl <span style="color: #008000;">=</span> MatrixTable<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateTableWithElements_A</span><span style="color: #008000;">&#40;</span>CondTblFields,
	<span style="color: #666666;">&quot;Utopia&quot;</span>, <span style="color: #666666;">&quot;bread, cheese&quot;</span>,      <span style="color: #008080; font-style: italic;">// estimator A</span>
	<span style="color: #666666;">&quot;Utopia&quot;</span>, <span style="color: #666666;">&quot;butter, honig&quot;</span>,      <span style="color: #008080; font-style: italic;">// estimator B</span>
	<span style="color: #666666;">&quot;Ignoria&quot;</span>, <span style="color: #666666;">&quot;ALL&quot;</span>,               <span style="color: #008080; font-style: italic;">// estimator C</span>
	<span style="color: #666666;">&quot;Euphoria&quot;</span>, <span style="color: #666666;">&quot;ALL&quot;</span>               <span style="color: #008080; font-style: italic;">// estimator A</span>
	<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// create associated table to see the estimation method in the result table</span>
<span style="color: #0600FF; font-weight: bold;">var</span> AssocTblFields <span style="color: #008000;">=</span> TableFields<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateEmptyTableFields</span><span style="color: #008000;">&#40;</span>md<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
TableFields<span style="color: #008000;">.</span><span style="color: #0000FF;">AddNewField</span><span style="color: #008000;">&#40;</span>AssocTblFields, <span style="color: #666666;">&quot;estimation_method&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">var</span> AssocTbl <span style="color: #008000;">=</span> MatrixTable<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateTableWithElements_A</span><span style="color: #008000;">&#40;</span>AssocTblFields,
	<span style="color: #666666;">&quot;estimator A&quot;</span>,
	<span style="color: #666666;">&quot;estimator B&quot;</span>,
	<span style="color: #666666;">&quot;estimator C&quot;</span>,
	<span style="color: #666666;">&quot;estimator A&quot;</span>
	<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008080; font-style: italic;">// array for delegate functions; assign a function for each row of condition matrix table</span>
<span style="color: #0600FF; font-weight: bold;">var</span> MyTableFuncList <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> TransformTableFunc_OP<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">4</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
MyTableFuncList<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> PriceEstimatorA<span style="color: #008000;">;</span>
MyTableFuncList<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> PriceEstimatorB<span style="color: #008000;">;</span>
MyTableFuncList<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">2</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> PriceEstimatorC<span style="color: #008000;">;</span>
MyTableFuncList<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">3</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> PriceEstimatorA<span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// array for other parameters</span>
<span style="color: #008080; font-style: italic;">// pass same parameters for all subtables excluding R Function (estimators A, B, C)</span>
EstMarketIndex <span style="color: #008000;">=</span> <span style="color: #FF0000;">1.2</span><span style="color: #008000;">;</span>
EstOilPrice <span style="color: #008000;">=</span> <span style="color: #FF0000;">100.0</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">var</span> OtherParametersList <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&#91;</span><span style="color: #FF0000;">4</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
OtherParametersList<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#123;</span> GlobalIndTable, r_engine, EstProdPriceFuncA, EstMarketIndex, EstOilPrice <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
OtherParametersList<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#123;</span> GlobalIndTable, r_engine, EstProdPriceFuncB, EstMarketIndex, EstOilPrice <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
OtherParametersList<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">2</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#123;</span> GlobalIndTable, r_engine, EstProdPriceFuncC, EstMarketIndex, EstOilPrice <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
OtherParametersList<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">3</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#123;</span> GlobalIndTable, r_engine, EstProdPriceFuncA, EstMarketIndex, EstOilPrice <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// call function router C (with condition cell table)</span>
ResultTbl <span style="color: #008000;">=</span> MatrixTable<span style="color: #008000;">.</span><span style="color: #0000FF;">FunctionRouterC</span><span style="color: #008000;">&#40;</span>PriceTable, SubTblFields,
	CondMatTbl, MyTableFuncList, AssocTbl, FirstMatchOnly<span style="color: #008000;">:</span> <span style="color: #0600FF; font-weight: bold;">true</span>, IgnoreHierarchy<span style="color: #008000;">:</span> <span style="color: #0600FF; font-weight: bold;">true</span>,
	ErrorIfConditionNotRelevant<span style="color: #008000;">:</span> <span style="color: #0600FF; font-weight: bold;">false</span>, OtherParametersList<span style="color: #008000;">:</span> OtherParametersList<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// round all key figures to 2 digits after decimal point</span>
ResultTbl <span style="color: #008000;">=</span> MatrixTable<span style="color: #008000;">.</span><span style="color: #0000FF;">Round</span><span style="color: #008000;">&#40;</span>ResultTbl, <span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// view result table</span>
MatrixTable<span style="color: #008000;">.</span><span style="color: #0000FF;">View_MatrixTable</span><span style="color: #008000;">&#40;</span>ResultTbl, <span style="color: #666666;">&quot;Result table: Apply different estimators for different subtables&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>Result table:<br />
<img src="http://finaquant.com/wp-content/uploads/2013/02/TableDataAndR_ResultTblS3.png" alt="Result table, 3. scenario" title="Result table, 3. scenario" width="443" height="327" class="alignnone size-full wp-image-3134" /></p>
<h3>Scenario 4: Applying all price estimators for all countries and products</h3>
<p>In this last scenario we want to apply all available estimation functions on all subtables of the price table in order to compare the estimation results. This is again a task for the function router.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// Function Router and R</span>
<span style="color: #008080; font-style: italic;">// Apply all price estimators to all countries and products</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// create condition table (not condition matrix table with condition cells!)</span>
CondTblFields <span style="color: #008000;">=</span> TableFields<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateEmptyTableFields</span><span style="color: #008000;">&#40;</span>md<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
TableFields<span style="color: #008000;">.</span><span style="color: #0000FF;">AddNewField</span><span style="color: #008000;">&#40;</span>CondTblFields, <span style="color: #666666;">&quot;country&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
CondTbl <span style="color: #008000;">=</span> MatrixTable<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateTableWithElements_A</span><span style="color: #008000;">&#40;</span>CondTblFields,
	<span style="color: #666666;">&quot;ALL&quot;</span>,                          <span style="color: #008080; font-style: italic;">// estimator A</span>
	<span style="color: #666666;">&quot;ALL&quot;</span>,                          <span style="color: #008080; font-style: italic;">// estimator B</span>
	<span style="color: #666666;">&quot;ALL&quot;</span>                           <span style="color: #008080; font-style: italic;">// estimator C</span>
	<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008080; font-style: italic;">// create associated table to see the estimation method in the result table</span>
AssocTbl <span style="color: #008000;">=</span> MatrixTable<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateTableWithElements_A</span><span style="color: #008000;">&#40;</span>AssocTblFields,
	<span style="color: #666666;">&quot;estimator A&quot;</span>,
	<span style="color: #666666;">&quot;estimator B&quot;</span>,
	<span style="color: #666666;">&quot;estimator C&quot;</span>
	<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008080; font-style: italic;">// array for delegate functions; assign a function for each row of condition matrix table</span>
MyTableFuncList <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> TransformTableFunc_OP<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">3</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
MyTableFuncList<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> PriceEstimatorA<span style="color: #008000;">;</span>
MyTableFuncList<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> PriceEstimatorB<span style="color: #008000;">;</span>
MyTableFuncList<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">2</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> PriceEstimatorC<span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// array for other parameters</span>
<span style="color: #008080; font-style: italic;">// pass same parameters for all subtables excluding R Function (estimators A, B, C)</span>
EstMarketIndex <span style="color: #008000;">=</span> <span style="color: #FF0000;">1.2</span><span style="color: #008000;">;</span>
EstOilPrice <span style="color: #008000;">=</span> <span style="color: #FF0000;">100.0</span><span style="color: #008000;">;</span>
&nbsp;
OtherParametersList <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&#91;</span><span style="color: #FF0000;">3</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
OtherParametersList<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#123;</span> GlobalIndTable, r_engine, EstProdPriceFuncA, EstMarketIndex, EstOilPrice <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
OtherParametersList<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#123;</span> GlobalIndTable, r_engine, EstProdPriceFuncB, EstMarketIndex, EstOilPrice <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
OtherParametersList<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">2</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#123;</span> GlobalIndTable, r_engine, EstProdPriceFuncC, EstMarketIndex, EstOilPrice <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// call function router A (with condition table)</span>
ResultTbl <span style="color: #008000;">=</span> MatrixTable<span style="color: #008000;">.</span><span style="color: #0000FF;">FunctionRouterA</span><span style="color: #008000;">&#40;</span>PriceTable, SubTblFields,
	CondTbl, MyTableFuncList, AssocTbl, 
	JokerMatchesAllvalues<span style="color: #008000;">:</span> <span style="color: #0600FF; font-weight: bold;">true</span>, TextJoker<span style="color: #008000;">:</span> <span style="color: #666666;">&quot;ALL&quot;</span>, NumJoker<span style="color: #008000;">:</span> <span style="color: #FF0000;">0</span>,
	FirstMatchOnly<span style="color: #008000;">:</span> <span style="color: #0600FF; font-weight: bold;">false</span>, OtherParametersList<span style="color: #008000;">:</span> OtherParametersList<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// round all key figures to 2 digits after decimal point</span>
ResultTbl <span style="color: #008000;">=</span> MatrixTable<span style="color: #008000;">.</span><span style="color: #0000FF;">Round</span><span style="color: #008000;">&#40;</span>ResultTbl, <span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// view result table</span>
MatrixTable<span style="color: #008000;">.</span><span style="color: #0000FF;">View_MatrixTable</span><span style="color: #008000;">&#40;</span>ResultTbl, <span style="color: #666666;">&quot;Result table: Apply all estimators for all subtables&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>Result table:<br />
<img src="http://finaquant.com/wp-content/uploads/2013/02/TableDataAndR_ResultTblS4.png" alt="Result table, 4. scenario" title="Result table, 4. scenario" width="423" height="450" class="alignnone size-full wp-image-3137" /></p>
<h2>Conclusions</h2>
<p>In this article we demonstrated how selected functions in R can be applied on selected parts (subtables) of input tables using flexible constructs like subtable transformer and function router. In that sense, these constructs can be used to integrate R with data tables containing any number of attributes and key figures.</p>
<p>The same (or similar) integration approach can be used for other computing languages like matlab.</p>
<span id="dprv_cp-v2.13" lang="en" xml:lang="en" class="notranslate" style="vertical-align:baseline; padding: 3px 3px 3px 3px; margin-top:2px; margin-bottom:2px; border-collapse:separate; line-height:16px;float:none; font-family: Tahoma, MS Sans Serif; font-size:13px;border:1px solid #bbbbbb;background:#FFFFFF none;display:inline-block;" title="certified 19 February 2013 17:17:50 UTC by Digiprove certificate P375169" ><a href="http://www.digiprove.com/show_certificate.aspx?id=P375169%26guid=S4Mio_Mm3k6llq78N3_PpA" target="_blank" rel="copyright" style="height:16px; line-height: 16px; border:0px; padding:0px; margin:0px; float:none; display:inline; text-decoration: none; background:transparent none; line-height:normal; font-family: Tahoma, MS Sans Serif; font-style:normal; font-weight:normal; font-size:11px;"><img src="http://finaquant.com/wp-content/plugins/digiproveblog/dp_seal_trans_16x16.png" style="max-width:none !important;vertical-align:-3px; display:inline; border:0px; margin:0px; padding:0px; float:none; background:transparent none" border="0" alt=""/><span style="font-family: Tahoma, MS Sans Serif; font-style:normal; font-size:11px; font-weight:normal; color:#636363; border:0px; float:none; display:inline; text-decoration:none; letter-spacing:normal; padding:0px; padding-left:8px; vertical-align:1px;margin-bottom:2px" onmouseover="this.style.color='#A35353';" onmouseout="this.style.color='#636363';">Copyright&nbsp;secured&nbsp;by&nbsp;Digiprove&nbsp;&copy;&nbsp;2013&nbsp;Tunc&nbsp;Ali&nbsp;Kütükcüoglu</span></a><!--B9C71303C3CD87F5DABD4211CCEDB117AD810402E89EED498AFAFA979864E130--></span><ol class="footnotes"><li id="footnote_0_3082" class="footnote">A function router applies selected table (or matrix) functions on selected subtables of an input table.</li><li id="footnote_1_3082" class="footnote">A subtable transformer applies the same table (or matrix) function on every subtable of an input table.</li></ol>]]></content:encoded>
			<wfw:commentRss>http://finaquant.com/table-data-as-input-to-estimation-functions-in-r/3082/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Calling user-defined functions in R from within c#</title>
		<link>http://finaquant.com/calling-user-defined-functions-in-r-from-within-c/3060?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=calling-user-defined-functions-in-r-from-within-c</link>
		<comments>http://finaquant.com/calling-user-defined-functions-in-r-from-within-c/3060#comments</comments>
		<pubDate>Sun, 17 Feb 2013 17:52:48 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Calculation engine]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[R]]></category>

		<guid isPermaLink="false">http://finaquant.com/?p=3060</guid>
		<description><![CDATA[In order to integrate R with high-level table constructs like function router1 or subtable transformer2 it might be desirable to call user-defined functions in R from within c#. This can be done quite easily with the help of the open-source &#8230; <a href="http://finaquant.com/calling-user-defined-functions-in-r-from-within-c/3060">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>In order to integrate R with high-level table constructs like <a href="http://finaquant.com/function-router/2802" title="Function Router">function router</a><sup><a href="http://finaquant.com/calling-user-defined-functions-in-r-from-within-c/3060#footnote_0_3060" id="identifier_0_3060" class="footnote-link footnote-identifier-link" title="A function router applies selected table (or matrix) functions on selected subtables of an input table.">1</a></sup> or <a href="http://finaquant.com/table-functions-for-transforming-subtables/2711" title="Table functions for transforming subtables">subtable transformer</a><sup><a href="http://finaquant.com/calling-user-defined-functions-in-r-from-within-c/3060#footnote_1_3060" id="identifier_1_3060" class="footnote-link footnote-identifier-link" title="A subtable transformer applies the same table (or matrix) function on every subtable of an input table.">2</a></sup> it might be desirable to call user-defined functions in R from within c#.</p>
<p>This can be done quite easily with the help of the open-source codeplex library  <a href="http://rdotnet.codeplex.com/" title="R.NET">R.NET</a>; following steps show how.</p>
<p>You may also want to see the related forum topic:<br />
<a href="http://finaquant.com/forum/finaquant-protos/how-can-a-function-router-call-matrix-functions-written-in-r">How can a function router call matrix functions written in R?</a><span id="more-3060"></span></p>
<p>1) Open an existing or new project in Microsoft Visual Studio</p>
<p>2) Install <a href="http://rdotnet.codeplex.com/" title="R.NET" target="_blank">R.NET</a> (NuGet)</p>
<p>Installation is quite easy:<br />
Visual Studio (2012) > TOOLS > Library Package Manager > Package Manager Console<br />
type: PM> &#8220;Install-Package R.NET&#8221;</p>
<p>3) Initialize a function in R and call it from C#<br />
See <a href="http://rdotnet.codeplex.com/documentation" title="R.NET documentation" target="_blank">R.NET documentation</a> for data types in R and R.NET.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">RDotNet</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// initialize R connection</span>
<span style="color: #0600FF; font-weight: bold;">var</span> envPath <span style="color: #008000;">=</span> Environment<span style="color: #008000;">.</span><span style="color: #0000FF;">GetEnvironmentVariable</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;PATH&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// check the version and path on your computer</span>
<span style="color: #0600FF; font-weight: bold;">var</span> rBinPath <span style="color: #008000;">=</span> <span style="color: #666666;">@&quot;C:Program FilesRR-2.14.1binx64&quot;</span><span style="color: #008000;">;</span>
&nbsp;
Environment<span style="color: #008000;">.</span><span style="color: #0000FF;">SetEnvironmentVariable</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;PATH&quot;</span>, envPath 
	<span style="color: #008000;">+</span> <span style="color: #000000;">System.<span style="color: #0000FF;">IO</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Path</span><span style="color: #008000;">.</span><span style="color: #0000FF;">PathSeparator</span> <span style="color: #008000;">+</span> rBinPath<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// initialite REngine object</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>REngine engine <span style="color: #008000;">=</span> REngine<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateInstance</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;RDotNet&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #008080; font-style: italic;">// Initializes settings.</span>
	engine<span style="color: #008000;">.</span><span style="color: #0000FF;">Initialize</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// define your function in R as multi-line string</span>
	<span style="color: #6666cc; font-weight: bold;">string</span> myfuncstr <span style="color: #008000;">=</span> <span style="color: #666666;">@&quot;matrix_mult &lt;- function(a,b){
	c = a %*% b;
	return(c);
	}&quot;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// embed your R function here</span>
	Function matrix_mult <span style="color: #008000;">=</span> engine<span style="color: #008000;">.</span><span style="color: #0000FF;">Evaluate</span><span style="color: #008000;">&#40;</span>myfuncstr<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">AsFunction</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// create input matrices</span>
        NumericMatrix a <span style="color: #008000;">=</span> engine<span style="color: #008000;">.</span><span style="color: #0000FF;">Evaluate</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">@&quot;a &lt;- matrix(c(1,4,2,5,3,6), nrow=2)&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">AsNumericMatrix</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        NumericMatrix b <span style="color: #008000;">=</span> engine<span style="color: #008000;">.</span><span style="color: #0000FF;">Evaluate</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">@&quot;b &lt;- matrix(c(1,4,2,5,3,6), nrow=3)&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">AsNumericMatrix</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// call your R function from c#</span>
	NumericMatrix c <span style="color: #008000;">=</span> engine<span style="color: #008000;">.</span><span style="color: #0000FF;">Evaluate</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">@&quot;c &lt;- matrix_mult(a,b)&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">AsNumericMatrix</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// ...</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<ol class="footnotes"><li id="footnote_0_3060" class="footnote">A function router applies selected table (or matrix) functions on selected subtables of an input table.</li><li id="footnote_1_3060" class="footnote">A subtable transformer applies the same table (or matrix) function on every subtable of an input table.</li></ol>]]></content:encoded>
			<wfw:commentRss>http://finaquant.com/calling-user-defined-functions-in-r-from-within-c/3060/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>finaquant products</title>
		<link>http://finaquant.com/finaquant-products/3048?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=finaquant-products</link>
		<comments>http://finaquant.com/finaquant-products/3048#comments</comments>
		<pubDate>Thu, 14 Feb 2013 23:48:22 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Calculation engine]]></category>

		<guid isPermaLink="false">http://finaquant.com/?p=3048</guid>
		<description><![CDATA[Uncommercial: finaquant® protos (available now) Commercial: finaquant® calcs (upcoming: July 2013) Uncommercial calculation engine based on table functions - Non-commercial &#038; free .net library - Table calculations without any sql-based database programming - Analytical table functions with in-memory tables as &#8230; <a href="http://finaquant.com/finaquant-products/3048">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<table style="width: auto; border: 0; padding: 0px 0px;">
<tbody>
<tr>
<td style="font-size: 9pt; line-height: 9pt; width: 310px; padding: 4px;"><u>Uncommercial:</u> <a href="http://finaquant.com/products/finaquantprotos" title="finaquant® protos – free .net library with table functions">finaquant® protos</a> (available now)</td>
<td style="font-size: 9pt; line-height: 9pt; width: 310px; padding: 4px;"><u>Commercial:</u> finaquant® calcs (upcoming: July 2013)</td>
</tr>
<tr>
<td style="width: 310px; padding: 4px;"><a href="http://finaquant.com/products/finaquantprotos" title="finaquant® protos – free .net library with table functions"><img src="http://finaquant.com/wp-content/uploads/2013/02/FinaquantProtosTigerW300.png" title="finaquant® protos - uncommercial .net library with table functions" width="300" height="221" class="alignnone size-full wp-image-3040" /></a></td>
<td style="width: 310px; padding: 4px;"><img src="http://finaquant.com/wp-content/uploads/2013/02/FinaquantCalcsTigerW300.png" title="finaquant® calcs - commercial calculation engine based on table functions (.net library)" width="300" height="221" class="alignnone size-full wp-image-3039" /></td>
</tr>
<tr>
<td style="font-size: 10pt; line-height: 16pt; width: 310px; padding: 4px;"><font color="red">Uncommercial calculation engine based on table functions</font><br />
- Non-commercial &#038; free .net library<br />
- Table calculations without any sql-based database programming<br />
- Analytical table functions with in-memory tables as input and output parameters (like standard formulas in excel)<br />
- A framework for user-defined table functions (like user-defined formulas in excel)<br />
- Ability to calculate and store all the table valued parameters of multiple calculation instances in a relational database like MS SQL or MySQL &#8211; <a href="http://finaquant.com/products/finaquantprotos" title="finaquant® protos – free .net library with table functions">more info &#038; downloads</a></td>
<td style="font-size: 10pt; line-height: 16pt; width: 310px; padding: 4px;"><font color="red">Commercial calculation engine based on table functions</font><br />
- All the features and functions of the non-commercial finaquant® protos, plus <a href="http://finaquant.com/next-step-worker-ant-ika-calculation-node/3238" title="Next step: Worker ant ika (calculation node)" target="_blank">Calculation Nodes and Networks</a> for enhanced calculation automation, performance and modularity<br />
- Integration of table and matrix computations for applications like scenario analysis and estimations based on historical data<br />
- Can be used for applications like business analytics and intelligence, financial planning, fee and commission calculations, simulations and optimizations with table data, reporting &#8211; <a href="http://finaquant.com/products" title="Overview to upcoming finaquant products" target="_blank">more info</a>
</td>
</tr>
</tbody>
</table>
<span id="dprv_cp-v2.13" lang="en" xml:lang="en" class="notranslate" style="vertical-align:baseline; padding: 3px 3px 3px 3px; margin-top:2px; margin-bottom:2px; border-collapse:separate; line-height:16px;float:none; font-family: Tahoma, MS Sans Serif; font-size:13px;border:1px solid #bbbbbb;background:#FFFFFF none;display:inline-block;" title="certified 14 February 2013 23:50:47 UTC by Digiprove certificate P373800" ><a href="http://www.digiprove.com/prove_compliance.aspx?id=P373800%26guid=VEXz8NAJv0yRffV9UWGsPg" target="_blank" rel="copyright" style="height:16px; line-height: 16px; border:0px; padding:0px; margin:0px; float:none; display:inline; text-decoration: none; background:transparent none; line-height:normal; font-family: Tahoma, MS Sans Serif; font-style:normal; font-weight:normal; font-size:11px;"><img src="http://finaquant.com/wp-content/plugins/digiproveblog/dp_seal_trans_16x16.png" style="max-width:none !important;vertical-align:-3px; display:inline; border:0px; margin:0px; padding:0px; float:none; background:transparent none" border="0" alt=""/><span style="font-family: Tahoma, MS Sans Serif; font-style:normal; font-size:11px; font-weight:normal; color:#636363; border:0px; float:none; display:inline; text-decoration:none; letter-spacing:normal; padding:0px; padding-left:8px; vertical-align:1px;margin-bottom:2px" onmouseover="this.style.color='#A35353';" onmouseout="this.style.color='#636363';">Copyright&nbsp;secured&nbsp;by&nbsp;Digiprove&nbsp;&copy;&nbsp;2013&nbsp;Tunc&nbsp;Ali&nbsp;Kütükcüoglu</span></a><!--F45E7F0E0BA5215FE0409CD12BE9CF14911F4EA2594CBA37AA75268EC61279C7--></span>]]></content:encoded>
			<wfw:commentRss>http://finaquant.com/finaquant-products/3048/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic page generated in 3.264 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2013-06-13 19:01:28 -->
