By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,930 Members | 1,458 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,930 IT Pros & Developers. It's quick & easy.

Open Excel from .NET

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.