Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 553
  • Last Modified:

how to save an Excel file with Visual basic .NET

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
altariamx2003
Asked:
altariamx2003
2 Solutions
 
DhaestCommented:
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
 
Éric MoreauSenior .Net ConsultantCommented:
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
 
CodeCruiserCommented:
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
 
Éric MoreauSenior .Net ConsultantCommented:
>>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
 
altariamx2003Author Commented:
YOU GUYS ARE AMAZING!!!!!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now