Solved

how to save an Excel file with Visual basic .NET

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

920 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

15 Experts available now in Live!

Get 1:1 Help Now