I have a code here made in VB.NET. The code enables the program to query from MS ACCESS database and save it to an Excel File(.xls) and prompts the user if he/she wants to open the file. The code works well yet I have a problem when the file has been opened. The columns are not auto-fitted to its contents which is making the file so messy and I also wanna allow the user to have an option to print the file. Is there any way to solve my problem? If you have any clarification, feel free to ask.
If (Not Directory.Exists("C:\Sales Monitoring Report")) Then
Directory.CreateDirectory("C:\Sales Monitoring Report")
End If
System.IO.File.Delete("C:\Sales Monitoring Report\Transaction.xls")
Dim createExcelFile = "SELECT ORNumber, UserID, TransactionID, Vatable, Tax, Amount, TransactionDate, Status INTO [Excel 12.0;HDR=YES;DATABASE=C:\Sales Monitoring Report\Transaction.xls].[Sheet1] FROM tbl_transaction"
ExecNonQuery(createExcelFile)
If MessageBox.Show("Do you want to open the file?", "Open File", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
Dim excelFile As New Excel.Application
Dim excelWorkBook As Excel.Workbook
excelWorkBook = excelFile.Workbooks.Open("C:\Sales Monitoring Report\Transaction.xls")
excelFile.Visible = True
excelWorkBook.Activate()
End If