Problem Copying DataTable into Worksheet

E

ekoniec1

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(String 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
 
S

sam

I am having a similar problem. My code does the same thing. It was working
fine until today. THe code has not changed in months.

Did you solve this problem?

~S
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top