Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Field information argument of <code>OpenText()</code> for CSV should be a two-dimensional array of <code>System.Object</code> elements, i.e. <code>obj[,]</code>, like <code>fieldInfo</code>value in the snippet below (<a href="http://msdn.microsoft.com/en-us/library/office/microsoft.office.interop.excel.workbooks.opentext" rel="nofollow">Excel interop details are available here</a>):</p> <pre><code>open Microsoft.Office.Interop.Excel [&lt;EntryPoint&gt;] let main argv = let fieldInfo: obj[,] = Array2D.zeroCreate 2 2 fieldInfo.[0,0] &lt;- box 1; fieldInfo.[0,1] &lt;- box XlColumnDataType.xlTextFormat; fieldInfo.[1,0] &lt;- box 2; fieldInfo.[1,1] &lt;- box XlColumnDataType.xlGeneralFormat; let app = new ApplicationClass(Visible=true) app.Workbooks.OpenText(@"C:\test.txt", StartRow=1, DataType=XlTextParsingType.xlDelimited, TextQualifier=XlTextQualifier.xlTextQualifierNone, Comma=true, FieldInfo=fieldInfo) 0 </code></pre> <p>Now, if you put into <code>c:\test.txt</code> file something like <code>abc,123</code> it will be parsed into the workbook without problems.</p> <p><em>EDIT:</em> <code>FieldInfo</code> can be initialized in more succinct way; instead of 5 code lines above the following array expression will do the same:</p> <pre><code>let fieldInfo: obj[] = [|[|1; int XlColumnDataType.xlTextFormat|]; [|2; int XlColumnDataType.xlGeneralFormat|]|] </code></pre> <p>Original run-time exception you've encountered has nothing to do with type of array elements as <code>XlColumnDataType</code> is just an enumeration, so for Excel they are integers. What Excel does not like is that you feed it a F# jugged array of type <code>int[][]</code>. If you just force <strong><code>fieldInfo</code></strong> type in your original snippet from F#-inferred <strong><code>int[][]</code></strong> to explicit <strong><code>obj[]</code></strong> this small change will make it work.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload