tags:

views:

83

answers:

3

Hello,

So far all the Excel stuff revolves around opening a file, writing values to it and saving it.

Is there a way to update the data in the Excel (while it's opened) automatically?

Thanks!

P.S. I am looking for more information with respect to Microsoft.Office.Interop.Excel;

A: 

You could use a Timer and have it restart for a periodic update or to do whatever. This links describes the concept in detail. http://www.ehow.com/how_5052033_insert-timer-ms-excel.html
The way it would work in your case is to periodically allow you to get data from an external resource and then you could write some VBA code (Or a Macro) to update your worksheet.
Just like a stock ticker.

Romain Hippeau
No, restarting is not an option. Because there is a 3rd party that's reading the values off.
I did not mean restart excel, I meant to restart the Timer once it had finished the trigger.
Romain Hippeau
@user303907 updated my post.
Romain Hippeau
A: 

Why not access excel via ADO and treat it like a database? http://www.connectionstrings.com/excel

Doobi
Because my external DDE application requires it the Excel application to be running constantly.
+1  A: 

Not sure how you're writing the data, but for reading data from Excel, this is what I do:

select the Range I need (can be from one cell to several rows and columns - it's a single cell here):

Excel.Range cell = m_worksheet.get_Range(_CellName(end, column), _CellName(end, column));

then get the values you need from the selected cell:

string value = cell.Value2.ToString().Substring(...);

you can also manipulate the colors if there are any:

if(Convert.ToInt32(cell.Interior.ColorIndex) == 6) //we have yellow background

be careful with colors if your users have Excel 2007, though. You'll have to use the RGB codes.

Also, while processign the Excel file, it's locked for other applications, I think. You'll have to check that for the 3rd party readign the values.

And for _CellName:

private string _CellName(int row, int col)
 {
      string result = "";
      if (col > 26) // for columns like 'AA' 
      {
           result = ((char)((char)(col / 26) + 'A' - 1)).ToString(); //get the first letter
      }
      result += ((char)((char)(col % 26) + 'A' - 1)).ToString(); //get the second/only letter
      return result + Convert.ToString(row);
 }
Rox
@Rox,What is this _CellName? I can't seem to find it.
Sorry for not mentioning it, I've used it all the time and forgot it was written by us: it returns the name of the cell in the Excel style format - "A1" for row 1, column 1. Will add the code to the answer.
Rox