Solved

how to save an Excel file with Visual basic .NET

Posted on 2011-02-15
5
511 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 70

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 70

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

762 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