Torna al Thread
Imports System.Data
Imports System.Data.Common
Imports System.Data.SqlClient
Imports System.IO
Imports System.Text
Imports System.Data.OleDb
Imports Excel = Microsoft.Office.Interop.Excel
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim i As Integer
Dim strLine As String, filePath, fileName, fileExcel
Dim objFileStream As FileStream
Dim objStreamWriter As StreamWriter
Dim nRandom As Random = New Random(DateTime.Now.Millisecond)
Dim cnn As SqlConnection = New SqlConnection(con_string)
'Create a pseudo-random file name.
fileExcel = "Foglio " & nRandom.Next().ToString() & ".xls"
'Set a virtual folder to save the file.
'Make sure that you change the application name to match your folder.
filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
fileName = filePath & "\" & fileExcel
'Use FileStream to create the .xls file.
objFileStream = New FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write)
objStreamWriter = New StreamWriter(objFileStream)
'Use a DataReader to connect to the Pubs database.
cnn.Open()
Dim sql As String = "select * from info"
Dim cmd As SqlCommand = New SqlCommand(sql, cnn)
Dim dr As SqlDataReader
dr = cmd.ExecuteReader()
'Enumerate the field names and records that are used to build the file.
For i = 0 To dr.FieldCount - 1
strLine = strLine & dr.GetName(i).ToString & Chr(9)
Next
'Write the field name information to file.
objStreamWriter.WriteLine(strLine)
'Reinitialize the string for data.
strLine = ""
'Enumerate the database that is used to populate the file.
While dr.Read()
For i = 0 To dr.FieldCount - 1
strLine = strLine & dr.GetValue(i) & Chr(9)
Next
objStreamWriter.WriteLine(strLine)
strLine = ""
End While
'Clean up.
dr.Close()
cnn.Close()
objStreamWriter.Close()
objFileStream.Close()
System.Diagnostics.Process.Start(filename,filePath)