Excel Files

  • Thread starter Thread starter rn5a
  • Start date Start date
R

rn5a

Which namespace do I have to import to work with MS-Excel files?

I have a Excel file named 'Procs.xls' in which there is some data in
'Sheet1'. What I would like to do is by clicking a Button in an ASPX
page, I want the data existing in 'Sheet1' to automatically get copied
in 'Sheet2' in 'Procs.xls'.

Can someone please give me some idea on how do I go about it?
 
Which namespace do I have to import to work with MS-Excel files?

Firstly, don't even think about trying to use Office Automation in ASP.NET
apps - Microsoft strongly advises against it, and won't actually support any
solution which uses it:
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757#kb2
I have a Excel file named 'Procs.xls' in which there is some data in
'Sheet1'. What I would like to do is by clicking a Button in an ASPX
page, I want the data existing in 'Sheet1' to automatically get copied
in 'Sheet2' in 'Procs.xls'.

Can someone please give me some idea on how do I go about it?

Fortunately, you have a couple of much better options:

1) Use ADO.NET
Excel can be treated as an ADO.NET datasource:
http://www.google.co.uk/search?sour...rls=GGLG,GGLG:2006-28,GGLG:en&q=ADO.NET+Excel

2) Use a 3rd-party product like this:
http://www.aspose.com/Products/Aspose.Cells/Default.aspx

A third alternative would be to save the Excel file in XML format, and then
read it using the XML namespace:
http://www.google.co.uk/search?sour...=GGLG,GGLG:2006-28,GGLG:en&q=Excel+XML+schema
 
Mark, thanks for your suggestions.

Please have a look at the following code:

---------------
Dim tempws As Worksheet
tempws = ThisWorkbook.Worksheets.Add
tempws.Name = "MySheet"
Worksheets("Sheet1").Select()
---------------

ASP.NET generates an error saying

Type 'Worksheet' is not defined.

pointing to the line where the variable 'tempws' is dimmed. I even
tried this:

---------------
Dim tempws As Excel.Worksheet
---------------

But this too generates the

Type 'Excel.Worksheet' is not defined.

error.

How do I overcome this error?
 
Mark, thanks for your suggestions.

Maybe you'd like to follow them...? ;-)
Please have a look at the following code:
Dim tempws As Worksheet
ASP.NET generates an error saying
Type 'Worksheet' is not defined.
How do I overcome this error?

You overcome it by not trying to use Office Automation with ASP.NET, as I
advised you... It is not designed to work this way.

Please re-read (or even read for the first time) the following link:
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757#kb2

Then implement one of the suggestions i.e. ADO.NET, Aspose or XML...
 
You don't ever do it this way. You don't want to try to instantiate excel
itself on the server. It's way too problematic and has too manu issues. Just
treat Excel like a datasource and you'll be able to work with it just like
you would any other database (well, almost). There are third-party controls
that will let you read/write excel files without having to actually use
excel. You might want to try one of these if you absolutely have to access
the excel file in this fashion as it's much safer to do it than to try to
work with excel on the server.


--

Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006
 
Back
Top