Question Saving excel worksheet file using SaveFileDialog.

skibrahim

New member
Joined
Oct 7, 2012
Messages
4
Location
India
Programming Experience
3-5
I created an Excel File which will serve as a database using the following code :

Imports Excel = Microsoft.Office.Interop.Excel
Imports System.Data.SqlClient


Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
xlApp = New Excel.Application
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
xlWorkSheet.Cells(1, 1) = "Name"
xlWorkSheet.Cells(1, 2) = "Institute Roll No."
xlWorkSheet.Cells(1, 3) = "Marks"
xlWorkSheet.SaveAs("D:\database.xlsx")
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
MsgBox("Excel file created , you can find the file in D:\")
End Sub


It works and the file is created in D:\
What i want to know is how to use the SaveFileDialog to save the file at any location i want...?
This is my first time using vb.net and I'm a total newbie...so any help will be appreciated.
 
First up, there's no point importing the SqlClient namespace because that's for SQL Server specifically. Also, are you aware that using Office Interop will require your users to have Excel installed? Unless you switch to late-binding, they'll also have to have the same version of Excel installed as you do. If you were to use a proper but simple database, e.g. Access or SQL Server CE, then the user wouldn't need any thing else installed. Also, a spreadsheet is NOT a database. If you want a database then you should use a database. Spreadsheets have their uses but using them as a database is an abuse.

Assuming that your users will all have Excel installed and that's the way that you want to go, you simply display a SaveFileDialog and then get the selected file path from its FileName property. You'll probably want to do a bit of extra configuration so you should learn how by reading the MSDN documentation for the SaveFileDialog class.
 
Back
Top