ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Problem Copying DataTable into Worksheet (https://www.excelbanter.com/excel-programming/368197-problem-copying-datatable-into-worksheet.html)

ekoniec1

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



All times are GMT +1. The time now is 10:44 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com