View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
ekoniec1 ekoniec1 is offline
external usenet poster
 
Posts: 1
Default Problem Copying DataTable into Worksheet

Hey all,

I am having some problems using the Excel.Range.Value2 property to copy data
from a DataTable into a Worksheet.

excelRange = excelWorksheet.get_Range( "A2", System.Type.Missing );
excelRange = excelRange.get_Resize( dt.Rows.Count, dt.Columns.Count );

object[,] tableData = new Object[ dt.Rows.Count, dt.Columns.Count ];
for( int i = 0; i < dt.Rows.Count; i++ )
for( int j = 0; j < dt.Columns.Count; j++ )
tableData[ i, j ] = dt.Rows[ i ][ j ].ToString();

excelRange.Value2 = tableData;

The above code fails with the following exception thrown:

System.Runtime.InteropServices.COMException (0x800A03EC): Exception
from HRESULT: 0x800A03EC at
System.RuntimeType.ForwardCallToInvokeMember(Strin g memberName,
BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData&
msgData) at Excel.Range.set_Value2(Object )

The DataTable that I am copying is fairly large in size (~8000 rows, 5
columns), with some large entries. When I change the following line:

tableData[ i, j ] = dt.Rows[ i ][ j ].ToString();
to
tableData[ i, j ] = "str";

No exception is thrown and the code functions as intended. Any help on this
would be greatly appreciated.

Thanks in advance,
Eric Konieczny