How to assign value to Excel 2000 worksheet cell

G

Guest

Hello,
I am writing c# web application with Excel 2000.
My code is:

Excel.Application EXL;
EXL = new Excel.Application();
Excel.Worksheet WSheet = new Excel.WorksheetClass();
string FileName = MapPath(".") + "\\..xls";
WSheet = (Excel.Worksheet)EXL.Workbooks.Open(FileName, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing).Worksheets.get_Item(1);
Excel.Range rng = WSheet.get_Range("H12", Type.Missing);
string H12 = "H12: " + rng.Value2.ToString(); // Works fine

How can I assign value to cell?
rng.Cells[6, 3] = "a"; --> return the following error:
error: cannot assign '"a"' into 'rng.Cells[6,3]'

Thank you.
 
P

Paul Clement

¤ Hello,
¤ I am writing c# web application with Excel 2000.
¤ My code is:
¤
¤ Excel.Application EXL;
¤ EXL = new Excel.Application();
¤ Excel.Worksheet WSheet = new Excel.WorksheetClass();
¤ string FileName = MapPath(".") + "\\..xls";
¤ WSheet = (Excel.Worksheet)EXL.Workbooks.Open(FileName, Type.Missing,
¤ Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
¤ Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
¤ Type.Missing).Worksheets.get_Item(1);
¤ Excel.Range rng = WSheet.get_Range("H12", Type.Missing);
¤ string H12 = "H12: " + rng.Value2.ToString(); // Works fine
¤
¤ How can I assign value to cell?
¤ rng.Cells[6, 3] = "a"; --> return the following error:
¤ error: cannot assign '"a"' into 'rng.Cells[6,3]'

See if the following helps:

http://support.microsoft.com/default.aspx?scid=kb;en-us;302084


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 
G

Guest

Hi Paul,
I read the article.
Still I get an error message.
According to my code, how can I assign value to the cell?
Thank you.
 
P

Paul Clement

¤ Hi Paul,
¤ I read the article.
¤ Still I get an error message.
¤ According to my code, how can I assign value to the cell?
¤ Thank you.
¤

The example I've seen implements the value2 property in C#. Perhaps that is what is missing from
your assignment statement.


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 
G

Guest

Hello Paul,
As described in the example, I change the code and still get the following
error:
error: cannot assign 'saNames' into 'WSheet.get_Range("F4","F5".Value2'

Excel.Application EXL;
EXL = new Excel.Application();
Excel.Worksheet WSheet = new Excel.WorksheetClass();
string FileName = MapPath(".") + "\\..xls";
WSheet = (Excel.Worksheet)EXL.Workbooks.Open(FileName, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing).Worksheets.get_Item(1);
Excel.Range rng = WSheet.get_Range("H12", Type.Missing);
string H12 = "H12: " + rng.Value2.ToString();

string[,] saNames = new string[1,2];
saNames[0, 0] = "John";
saNames[0, 1] = "Smith";

WSheet.get_Range("F4", "F5").Value2 = saNames;

Thank you.
 
G

Guest

Hello Paul,
I found what was the problem.
When I run this code at windows form application and assign value to
worksheet cell, I get "Do you want to save changes..." dialog box.
At my web application this dialog box doesn’t appear!
I should run my application at web!
So, my new question is how can I prevent from Excel to ask me to "... save
changes..." after assign value to worksheet cell?
Thank you.


Dudi Nissan said:
Hello Paul,
As described in the example, I change the code and still get the following
error:
error: cannot assign 'saNames' into 'WSheet.get_Range("F4","F5".Value2'

Excel.Application EXL;
EXL = new Excel.Application();
Excel.Worksheet WSheet = new Excel.WorksheetClass();
string FileName = MapPath(".") + "\\..xls";
WSheet = (Excel.Worksheet)EXL.Workbooks.Open(FileName, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing).Worksheets.get_Item(1);
Excel.Range rng = WSheet.get_Range("H12", Type.Missing);
string H12 = "H12: " + rng.Value2.ToString();

string[,] saNames = new string[1,2];
saNames[0, 0] = "John";
saNames[0, 1] = "Smith";

WSheet.get_Range("F4", "F5").Value2 = saNames;

Thank you.


Paul Clement said:
¤ Hi Paul,
¤ I read the article.
¤ Still I get an error message.
¤ According to my code, how can I assign value to the cell?
¤ Thank you.
¤

The example I've seen implements the value2 property in C#. Perhaps that is what is missing from
your assignment statement.


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 
A

Alvin Bruney [MVP]

Dudi:
For web applications you should not automate excel. Instead, you should use
the Office Web Components for that kind of functionality. Microsoft
specifically warns about this in their docs because of performance and
scalability issues. The Office Web Components are available free on the
office download site.

--
Regards,
Alvin Bruney [Microsoft MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ http://www.lulu.com/owc
----------------------------------------------------------


Dudi Nissan said:
Hello Paul,
I found what was the problem.
When I run this code at windows form application and assign value to
worksheet cell, I get "Do you want to save changes..." dialog box.
At my web application this dialog box doesn't appear!
I should run my application at web!
So, my new question is how can I prevent from Excel to ask me to "... save
changes..." after assign value to worksheet cell?
Thank you.


Dudi Nissan said:
Hello Paul,
As described in the example, I change the code and still get the
following
error:
error: cannot assign 'saNames' into 'WSheet.get_Range("F4","F5".Value2'

Excel.Application EXL;
EXL = new Excel.Application();
Excel.Worksheet WSheet = new Excel.WorksheetClass();
string FileName = MapPath(".") + "\\..xls";
WSheet = (Excel.Worksheet)EXL.Workbooks.Open(FileName, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing).Worksheets.get_Item(1);
Excel.Range rng = WSheet.get_Range("H12", Type.Missing);
string H12 = "H12: " + rng.Value2.ToString();

string[,] saNames = new string[1,2];
saNames[0, 0] = "John";
saNames[0, 1] = "Smith";

WSheet.get_Range("F4", "F5").Value2 = saNames;

Thank you.


Paul Clement said:
On Tue, 15 Feb 2005 06:51:04 -0800, "Dudi Nissan"

¤ Hi Paul,
¤ I read the article.
¤ Still I get an error message.
¤ According to my code, how can I assign value to the cell?
¤ Thank you.
¤

The example I've seen implements the value2 property in C#. Perhaps
that is what is missing from
your assignment statement.


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 
P

Paul Clement

¤ Hello Paul,
¤ I found what was the problem.
¤ When I run this code at windows form application and assign value to
¤ worksheet cell, I get "Do you want to save changes..." dialog box.
¤ At my web application this dialog box doesn’t appear!
¤ I should run my application at web!
¤ So, my new question is how can I prevent from Excel to ask me to "... save
¤ changes..." after assign value to worksheet cell?
¤ Thank you.
¤

If you set the DisplayAlerts property of the Application object to False this will prevent the
dialog from displaying and will simply execute the default behavior of the dialog. Just remember to
set DisplayAlerts back to True before terminating Excel.

In addition, Alvin makes a good point. Because of the limited threading model of Office applications
and their UI, Microsoft does not support automation from unattended processes such as web apps,
services and windows services.


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 

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