Solved

how to save an Excel file with Visual basic .NET

Posted on 2011-02-15
5
500 Views
Last Modified: 2012-05-11
Right know Im using this code to open an existing excel file to modiy it, and save it with other name

        Dim oSheet As Object
        Dim xlTemp As Application

        xlTemp = New Application
        xlTemp.Workbooks.Open("C:\plantilla1.xls")
        oSheet = xlTemp.Worksheets(1)
        oSheet.Range("A1").Value = "Last Name"
        oSheet.Range("B1").Value = "First Name"
        oSheet.Range("A1:B1").Font.Bold = True
        oSheet.Range("A2").Value = "Doe"
        oSheet.Range("B2").Value = "John"
        xlTemp.SaveWorkspace("c:\test.xls")
        xlTemp.Quit()

This works ok, but I would like to know how to save the file with other name using a classic "save as file dialog" windows with VB

is that possible???

An also I have another problem with my code as you can see the excel remains on the process list example
I woul like to know how to remove the excel process after I close the application by code

0
Comment
Question by:altariamx2003
5 Comments
 
LVL 53

Accepted Solution

by:
Dhaest earned 250 total points
ID: 34904261
There are already lots of questions about this ...

You must be sure that you close the workbook and excel. Set all object back to nothing and even use the garbage collector.

See this vb.net article on experts-exchange

Killing EXCEL objects from memory in VB.NET
http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/A_3260-Killing-EXCEL-objects-from-memory-in-vb-NET.html?sfQueryTermInfo=1+10+30+excel+process+run
Dim oSheet As Object
        Dim xlTemp As Application

        xlTemp = New Application
        xlTemp.Workbooks.Open("C:\plantilla1.xls")
        oSheet = xlTemp.Worksheets(1)
        oSheet.Range("A1").Value = "Last Name"
        oSheet.Range("B1").Value = "First Name"
        oSheet.Range("A1:B1").Font.Bold = True
        oSheet.Range("A2").Value = "Doe"
        oSheet.Range("B2").Value = "John"
        xlTemp.SaveWorkspace("c:\test.xls")
        oSheet.Close()
        oSheet = nothing
        xlTemp.Quit()


        System.Runtime.InteropServices.Marshal.ReleaseComObject(Excel)
        Excel = Nothing

' call the garbage collector
GC.Collect()
GC.WaitForPendingFinalizers() 

' if nothing else helps, remove all the processes with the name excel.exe 
Dim pro() As Process = System.Diagnostics.Process.GetProcessesByName("EXCEL")
For Each p As Process In pro
   p.Kill()
Next

Open in new window

0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 34905366
this is another reason why I prefer to use a component like http://www.aspose.com/categories/.net-components/aspose.cells-for-.net/default.aspx to handle my Excel files
0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 250 total points
ID: 34905977
Regarding your primary question, replace the following line

xlTemp.SaveWorkspace("c:\test.xls")

with this code

            Dim sv As New SaveFileDialog
            sv.AddExtension = True
            sv.CheckPathExists = True
            sv.DefaultExt = ".xls"
            sv.Filter = "Excel Files|*.xls"
            sv.OverwritePrompt = True
            sv.Title = "Save Excel File As..."
            If sv.ShowDialog = Windows.Forms.DialogResult.OK Then
                xlTemp.SaveWorkspace(sv.FileName)
            End If
            sv.Dispose()






@emoreau
Do you work for aspose? :-)
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 34906131
>>Do you work for aspose? :-)

No I don't. But I have been using their almost unique product on many projects (as I am a consultant) for many years now with great success.
0
 

Author Closing Comment

by:altariamx2003
ID: 34907800
YOU GUYS ARE AMAZING!!!!!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now