Solved

how to save an Excel file with Visual basic .NET

Posted on 2011-02-15
5
524 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
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…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

623 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