In continuation from here

There are two way to save the file:

  1. Save
  2. Save As

When you do a simple Save then the original file is saved with the changes that you made. Whereas in the Save As a copy of the original file is saved.

See the code snippets below.


Save

Imports Excel = Microsoft.Office.Interop.Excel

Public Class Form1
    '~~> Define your Excel Objects
    Dim xlApp As New Excel.Application
    Dim xlWorkBook As Excel.Workbook
    Dim xlWorkSheet As Excel.Worksheet

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        '~~> Opens Source Workbook. Change path and filename as applicable
        xlWorkBook = xlApp.Workbooks.Open("C:\Tutorial\Sample.xlsx")

        '~~> Display Excel
        xlApp.Visible = True

        '~~> Do some work

        '~~> Save the file
        xlWorkBook.Save()

        '~~> Close the file
        xlWorkBook.Close()
    End Sub
End Class

Save As

When you do a Save As, you have to specify the new file name and the path for the file. Additionally you can also specify a password to protect the file so that the user has to supply a password to open the file. You can also specify if the file is Write-Protected so that the user has to enter the password before modifying it.

So a typical code snippet could be like this:

Imports Excel = Microsoft.Office.Interop.Excel

Public Class Form1
    '~~> Define your Excel Objects
    Dim xlApp As New Excel.Application
    Dim xlWorkBook As Excel.Workbook
    Dim xlWorkSheet As Excel.Worksheet

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim MyPassword As String = "Password"

        '~~> Opens Source Workbook. Change path and filename as applicable
        xlWorkBook = xlApp.Workbooks.Open("C:\Tutorial\Sample.xlsx")

        '~~> Display Excel
        xlApp.Visible = True

        '~~> Do some work

        '~~> Save As file
        xlWorkBook.SaveAs(Filename:="C:\SampleNew.xlsx", _
                          FileFormat:=Excel.XlFileFormat.xlOpenXMLWorkbook, _
                          Password:=MyPassword, _
                          WriteResPassword:=MyPassword, _
                          ReadOnlyRecommended:=True, CreateBackup:=False)

        '~~> Close the file
        xlWorkBook.Close()
    End Sub
End Class

These are the file formats in Excel 2007/2010:

  1. 50 = xlExcel12 (Excel Binary Workbook in 2007-2010 with or without macro’s, xlsb)
  2. 51 = xlOpenXMLWorkbook (without macro’s in 2007-2010, xlsx)
  3. 52 = xlOpenXMLWorkbookMacroEnabled (with or without macro’s in 2007-2010, xlsm)
  4. 56 = xlExcel8 (97-2003 format in Excel 2007-2010, xls)