473,385 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Open Excel from .NET

Hello, I have a question, how can I open Microsoft Excel from .NET. I only
need to open a new file in Excel and paste some information and set the
Microsoft Excel as the enabled aplication, so the user can continue working
in Excel and he'll save the information I pasted. I tried this but doesn't
open Excel at all, but it does save the file c:\test.xls with the value
"This is column B row 2" in colum B and row 2:

Dim xlApp As Interop.Excel.Application

Dim xlBook As Interop.Excel.Workbook

Dim xlSheet As Interop.Excel.Worksheet

xlApp = CType(CreateObject("Excel.Application"), Interop.Excel.Application)

xlBook = CType(xlApp.Workbooks.Add, Interop.Excel.Workbook)

xlSheet = CType(xlBook.Worksheets(1), Interop.Excel.Worksheet)

' Place some text in the second row of the sheet.

xlSheet.Cells(2, 2) = "This is column B row 2"

' Show the sheet.

xlSheet.Application.Visible = True

' Save the sheet to C:\Test.xls directory.

xlSheet.SaveAs("C:\Test.xls")

Thanks in advance
Jennyfer
Nov 21 '05 #1
3 7405
Jan
Hi Jennyfer,

Are you sure you want to use automation for such a simple task? If you use
automation your application will be linked to a specific version of MS Excel
Object Library.

Instead, you can generate Excel-compatible file and use
System.Diagnostics.Process.Start method to run Excel or other associated
application:

Dim fileName As String = "Test.csv"

Dim sw As StreamWriter = New StreamWriter(fileName)

sw.WriteLine(",")

sw.WriteLine("," & Chr(34) & "This is column B row 2" & Chr(34))

sw.close()

System.Diagnostics.Process.Start(fileName)

If you don't want to use CSV files or want more advanced formatting you can
use our ExcelLite Free component to generate native XLS files. You can use
it in a commercial applications; it is only limited to max 150 rows per
sheet (http://www.gemboxsoftware.com/ExcelLiteFree.htm).

Regards,
Jan
"Jennyfer Barco" <pd*****@nospam.wdsinc.com> wrote in message
news:uL**************@TK2MSFTNGP15.phx.gbl...
Hello, I have a question, how can I open Microsoft Excel from .NET. I only
need to open a new file in Excel and paste some information and set the
Microsoft Excel as the enabled aplication, so the user can continue
working in Excel and he'll save the information I pasted. I tried this but
doesn't open Excel at all, but it does save the file c:\test.xls with the
value "This is column B row 2" in colum B and row 2:

Dim xlApp As Interop.Excel.Application

Dim xlBook As Interop.Excel.Workbook

Dim xlSheet As Interop.Excel.Worksheet

xlApp = CType(CreateObject("Excel.Application"),
Interop.Excel.Application)

xlBook = CType(xlApp.Workbooks.Add, Interop.Excel.Workbook)

xlSheet = CType(xlBook.Worksheets(1), Interop.Excel.Worksheet)

' Place some text in the second row of the sheet.

xlSheet.Cells(2, 2) = "This is column B row 2"

' Show the sheet.

xlSheet.Application.Visible = True

' Save the sheet to C:\Test.xls directory.

xlSheet.SaveAs("C:\Test.xls")

Thanks in advance
Jennyfer

Nov 21 '05 #2
Hello Jennyfer,

Set the xlApp.Visible property to True.

BTW, why do you use the
xlApp = CType(CreateObject("Excel.Application"),

Interop.Excel.Application)

Why not

~
xlApp = New Interop.Excel.Application
~

?

Roman
Nov 21 '05 #3
Hi Jennyfer,

When you open programatically an Office application, assuming that your code
releases all COM references to the Application and the objects that you have
created, the application will be destroyed after the last reference is
released. In .NET is quite easy to get into the opposite problem, see

Office application does not quit after automation from Visual Studio .NET
client
http://support.microsoft.com/default...b;en-us;317109)

but since your intention is to keep the app open, you should use
Application.UserControl = True to increase artificially the number of COM
references. I am assuming that your code Application.Visible = True works
fine but the app is closed so fast that you can´t even see it. You can
verify it inserting a MsgBox just after making it visible.

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com

"Jennyfer Barco" <pd*****@nospam.wdsinc.com> escribió en el mensaje
news:uL**************@TK2MSFTNGP15.phx.gbl...
Hello, I have a question, how can I open Microsoft Excel from .NET. I only
need to open a new file in Excel and paste some information and set the
Microsoft Excel as the enabled aplication, so the user can continue
working in Excel and he'll save the information I pasted. I tried this but
doesn't open Excel at all, but it does save the file c:\test.xls with the
value "This is column B row 2" in colum B and row 2:

Dim xlApp As Interop.Excel.Application

Dim xlBook As Interop.Excel.Workbook

Dim xlSheet As Interop.Excel.Worksheet

xlApp = CType(CreateObject("Excel.Application"),
Interop.Excel.Application)

xlBook = CType(xlApp.Workbooks.Add, Interop.Excel.Workbook)

xlSheet = CType(xlBook.Worksheets(1), Interop.Excel.Worksheet)

' Place some text in the second row of the sheet.

xlSheet.Cells(2, 2) = "This is column B row 2"

' Show the sheet.

xlSheet.Application.Visible = True

' Save the sheet to C:\Test.xls directory.

xlSheet.SaveAs("C:\Test.xls")

Thanks in advance
Jennyfer

Nov 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Allison Bailey | last post by:
Hi Folks, I'm a brand new Python programmer, so please point me in the right direction if this is not the best forum for this question.... I would like to open an existing MS Excel spreadsheet...
1
by: Lize | last post by:
Hi, I'm writing an ASP application to open an excel workbook, then run a macro stored in the excel file, which produces outputs that will be displayed back onto my ASP application. Now the...
1
by: Jim | last post by:
This should really be simple, but I can't figure it out. I have some VB that exports a table in an Excel format ("C:\NewReport.xls"). After the export is done, I simply want to have some code...
1
by: Bon | last post by:
Hello all I create a form with three buttons in MS Access 2000. They are Open Excel Template, Save Draft and Save Final. When I click the Open Excel Template button, the Excel template will be...
6
by: Michael Groeger | last post by:
Hi, I have an aspx page which generates an excel document and transfers it to the browser as attachment. Normally, once the document is transferred the open save dialog prompts to open or save...
3
by: Agnes | last post by:
Dim dsExcelExport As New System.Data.DataSet Dim daExcelExport As New System.Data.SqlClient.SqlDataAdapter Dim Excel As New Excel.Application Dim strExcelFile As String Dim strFileName As...
8
by: shenkel55 | last post by:
I'm using Access and Excel 2003. Using either the import wizard or code, I have the same problem. This problem only happens with Excel files automatically generated by Corp IT. If I try to do an...
7
by: Peter | last post by:
ASP.NET 2.0 I am trying to open a Word document and Excel document from a dialog web page, what's the best way to do that? I have tried the following: Response.Clear();...
22
by: robertgregson | last post by:
Using C#, .NET3.5, Visual Studio 2008 and WCF on Windows VISTA SP1, I have written a service, service host (as a C# console application) and a client. The service uses...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.