Mombu the Programming Forum

Go Back   Mombu the Programming Forum > Programming > C#/Excel interop - performance tips?
User Name
Password
REGISTER NOW! Mark Forums Read




Reply
1 5th April 22:59
xing
External User
 
Posts: 1
Default C#/Excel interop - performance tips?



I am writing a C# application which reads data from an Excel workbook and writes the process result back to another Excel workbook. Currently I am using a one-cell range object to locate one cell by row & col id for each read and write. (i.e. everytime, I use the range object to refer to one cell and operates on its Value2 property)

Effectively I guess for each read and/or write, it creates a new Range object(???) and issue a COM call

Therefore I wonder is there any easier way to block read / write. For example, is there a way to issue a command say "Read all the cells in a range (e.g. 1000 x 100) and return their Value2 in a collection-type object"? (Unfortunately I don't have the control of the sorce Excel workbook, so cannot create named range in that workbook.
  Reply With Quote


 


2 7th April 08:41
aaron queenan
External User
 
Posts: 1
Default C#/Excel interop - performance tips?



If you get the Range object for a group of cells, the Value2 member returns
an array containing the Value2 members for each cell.

Regards,
Aaron Queenan.


writes the process result back to another Excel workbook. Currently I am
using a one-cell range object to locate one cell by row & col id for each
read and write. (i.e. everytime, I use the range object to refer to one cell
and operates on its Value2 property).


object(???) and issue a COM call.


example, is there a way to issue a command say "Read all the cells in a
range (e.g. 1000 x 100) and return their Value2 in a collection-type
object"? (Unfortunately I don't have the control of the sorce Excel
workbook, so cannot create named range in that workbook.)
  Reply With Quote
3 11th April 14:46
xing
External User
 
Posts: 1
Default C#/Excel interop - performance tips?


Thanks. It looks OK when reading from Value2, is there a way to "batch write" as well? It seems that if I assign an array object back to the Value2 object, only the first cell is updated

The following code can read the data successfully, but only the first cell is updated when write back
-------------------------------------------------------------------------------
Excel.Range oRng = (Excel.Range)oWS.get_Range("A1", "B4")

object[,] data = (object[,])oRng.Value2

for(int i=1; i<=data.GetUpperBound(0); i++

for(int j = 1; j <= data.GetUpperBound(1); j++

Console.WriteLine("(" + i + "," + j + ") = " + data[i,j])
data[i,j] = "NEW" + data[i,j]

oRng.Value2 = data;
  Reply With Quote
4 25th August 18:33
ch46
programming
Mombu User
 
Join Date: Aug 2008
Location: CA, USA
Posts: 1
ch46 is on a distinguished road
Default Re: C#/Excel interop - performance tips?


Quote:
Originally Posted by xing
Thanks. It looks OK when reading from Value2, is there a way to "batch write" as well? It seems that if I assign an array object back to the Value2 object, only the first cell is updated

The following code can read the data successfully, but only the first cell is updated when write back
-------------------------------------------------------------------------------
Excel.Range oRng = (Excel.Range)oWS.get_Range("A1", "B4")

object[,] data = (object[,])oRng.Value2

for(int i=1; i<=data.GetUpperBound(0); i++

for(int j = 1; j <= data.GetUpperBound(1); j++

Console.WriteLine("(" + i + "," + j + ") = " + data[i,j])
data[i,j] = "NEW" + data[i,j]

oRng.Value2 = data;
I see the same thing when I try to assign an array to a range's Value2 property. Only the first value in the array is assigned to all cells in range.
ch46 is offline   Reply With Quote


 


Reply


Thread Tools
Display Modes




666