Home All Groups Group Topic Archive Search About

How to assign value to Excel 2000 worksheet cell

Author
15 Feb 2005 12:27 PM
Dudi Nissan

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.

Author
15 Feb 2005 2:29 PM
Paul Clement
On Tue, 15 Feb 2005 04:27:07 -0800, "Dudi Nissan" <DudiNis***@discussions.microsoft.com> wrote:

¤ 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 ~~~ pclem***@ameritech.net
Microsoft MVP (Visual Basic)
Are all your drivers up to date? click for free checkup

Author
15 Feb 2005 2:51 PM
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.

Show quoteHide quote
"Paul Clement" wrote:

> On Tue, 15 Feb 2005 04:27:07 -0800, "Dudi Nissan" <DudiNis***@discussions.microsoft.com> wrote:
>
> ¤ 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 ~~~ pclem***@ameritech.net
> Microsoft MVP (Visual Basic)
>
Author
15 Feb 2005 7:39 PM
Paul Clement
On Tue, 15 Feb 2005 06:51:04 -0800, "Dudi Nissan" <DudiNis***@discussions.microsoft.com> wrote:

¤ 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 ~~~ pclem***@ameritech.net
Microsoft MVP (Visual Basic)
Author
16 Feb 2005 7:53 AM
Dudi Nissan
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.


Show quoteHide quote
"Paul Clement" wrote:

> On Tue, 15 Feb 2005 06:51:04 -0800, "Dudi Nissan" <DudiNis***@discussions.microsoft.com> wrote:
>
> ¤ 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 ~~~ pclem***@ameritech.net
> Microsoft MVP (Visual Basic)
>
Author
16 Feb 2005 10:47 AM
Dudi Nissan
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.


Show quoteHide quote
"Dudi Nissan" wrote:

> 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" wrote:
>
> > On Tue, 15 Feb 2005 06:51:04 -0800, "Dudi Nissan" <DudiNis***@discussions.microsoft.com> wrote:
> >
> > ¤ 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 ~~~ pclem***@ameritech.net
> > Microsoft MVP (Visual Basic)
> >
Author
16 Feb 2005 12:31 PM
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
----------------------------------------------------------


Show quoteHide quote
"Dudi Nissan" <DudiNis***@discussions.microsoft.com> wrote in message
news:532567BF-83E7-45F1-A123-153ACCC429DE@microsoft.com...
> 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" wrote:
>
>> 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" wrote:
>>
>> > On Tue, 15 Feb 2005 06:51:04 -0800, "Dudi Nissan"
>> > <DudiNis***@discussions.microsoft.com> wrote:
>> >
>> > ¤ 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 ~~~ pclem***@ameritech.net
>> > Microsoft MVP (Visual Basic)
>> >
Author
16 Feb 2005 2:07 PM
Paul Clement
On Wed, 16 Feb 2005 02:47:03 -0800, "Dudi Nissan" <DudiNis***@discussions.microsoft.com> wrote:

¤ 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 ~~~ pclem***@ameritech.net
Microsoft MVP (Visual Basic)

Bookmark and Share