Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Open Excel from SAP

Hai to all,

Does anyone know how to open the excel when i click the button in SAP. And also that excel should open with the tables in SQL Server ie:(select * from Information_schema.tables).

Regards,

Anitha

Former Member
Former Member replied

Hi Anitha,

The code below will genetare excel file and after generating that you can open the file

using System.diagnostics.process.start("ur excel file path")

Dim i As Integer

Dim strLine As String, filePath, fileName, fileExcel, link

Dim objFileStream As FileStream

Dim objStreamWriter As StreamWriter

Dim nRandom As Random = New Random(DateTime.Now.Millisecond)

Dim fs As Object, myFile As Object

Dim cnn As SqlConnection = New SqlConnection("server=(local);database=DBNAME;" & _

"Integrated Security=SSPI")

fileExcel = "My.xls"

filePath = (Give the path of file where you want to save)

fileName = filePath & "\" & fileExcel

objFileStream = New FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write)

objStreamWriter = New StreamWriter(objFileStream)

cnn.Open()

Dim sql As String = "SELECT * FROM OITM"

Dim cmd As SqlCommand = New SqlCommand(sql, cnn)

Dim dr As SqlDataReader

dr = cmd.ExecuteReader()

For i = 0 To dr.FieldCount - 1

strLine = strLine & dr.GetName(i).ToString & Chr(9)

Next

objStreamWriter.WriteLine(strLine)

strLine = ""

While dr.Read()

For i = 0 To dr.FieldCount - 1

strLine = strLine & dr.GetValue(i) & Chr(9)

Next

objStreamWriter.WriteLine(strLine)

strLine = ""

End While

dr.Close()

cnn.Close()

objStreamWriter.Close()

objFileStream.Close()

Hope this helps you

regards

Vishnu

0 View this answer in context

Helpful Answer

by
Not what you were looking for? View more on this topic or Ask a question