G
Guest
Guys,
I have this code that loads some data from a SQL view into a datatable, then
into arrays, I can then export it to a predefined Excel template that creates
lots of fancy charts and stuff from the raw data.
This code WORKS FINE on my local machine when developing, but obviously when
I come to deploy it tries to launch excel on the server, where it is not
installed.
How do I modify this code so that it launches Excel on the client and
exports the data. Or is there a better way to do this ?
--
Thanks in advance
Bob
Imports System.Data.SqlClient
Imports Microsoft.Office.Interop
Private Sub btn_excel_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btn_excel.Click
'Get data into datatable.
Dim Qid As Integer = CInt(Session("QID"))
Dim cnn As New SqlConnection("Data Source=BLS5TEST2;Initial
Catalog=HPT;User Id=HPT;Password=kibby1;")
Dim cmd As New SqlDataAdapter("SELECT * FROM vw_answers WHERE Qid =
'" & Qid & "' and Staffname <> '" & User.Identity.Name & "'", cnn)
Dim tlcmd As New SqlDataAdapter("SELECT * FROM vw_answers WHERE Qid
= '" & Qid & "' and Staffname = '" & User.Identity.Name & "'", cnn)
'Fill the dataset here.
Dim ds As New DataSet
cmd.Fill(ds, "TeamAnswers")
tlcmd.Fill(ds, "LeaderAnswers")
Dim RowNbr As Int32 = 0
Dim Answers(ds.Tables(0).Rows.Count) As Object
Dim Leader(ds.Tables(1).Rows.Count) As Object
'get Teamanswers into array
For Each Rw As DataRow In ds.Tables(0).Rows
Answers(RowNbr) = Rw.ItemArray()
RowNbr += 1
Next Rw
'get Leaderanswers into array
RowNbr = 0
For Each lRw As DataRow In ds.Tables(1).Rows
Leader(RowNbr) = lRw.ItemArray()
RowNbr += 1
Next lRw
'Get data into EXCEL
Dim xl As Excel.Application
Try
xl = GetObject(, "Excel.Application")
Catch ex As Exception
xl = New Excel.Application
End Try
Dim wkbk As Excel.Workbook
Dim wkst As Excel.Worksheet
Dim wksttl As Excel.Worksheet
xl.Visible = False
xl.DisplayAlerts = False
wkbk = xl.Workbooks.Open("\\bls2mbr25\HPT\HPT Questionnaire
Results.xlt")
wkst = wkbk.Sheets("rawdata")
'Export Team Answers
Dim x As Integer
Dim y As Integer
Dim range As String
For x = 0 To Answers.GetUpperBound(0)
y = x + 2
range = "A" & y & "" & y
wkst.Range(range).Value = Answers(x)
Next
'Export TeamLeader Answers
For x = 0 To Leader.GetUpperBound(0)
y = x + 2
range = "E" & y & ":G" & y
wkst.Range(range).Value = Leader(x)
Next
'Tidy up
Answers = Nothing
Leader = Nothing
ds.Dispose()
ds = Nothing
wkbk.Sheets(1).activate()
xl.Visible = True
xl.DisplayAlerts = True
End Sub
I have this code that loads some data from a SQL view into a datatable, then
into arrays, I can then export it to a predefined Excel template that creates
lots of fancy charts and stuff from the raw data.
This code WORKS FINE on my local machine when developing, but obviously when
I come to deploy it tries to launch excel on the server, where it is not
installed.
How do I modify this code so that it launches Excel on the client and
exports the data. Or is there a better way to do this ?
--
Thanks in advance
Bob
Imports System.Data.SqlClient
Imports Microsoft.Office.Interop
Private Sub btn_excel_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btn_excel.Click
'Get data into datatable.
Dim Qid As Integer = CInt(Session("QID"))
Dim cnn As New SqlConnection("Data Source=BLS5TEST2;Initial
Catalog=HPT;User Id=HPT;Password=kibby1;")
Dim cmd As New SqlDataAdapter("SELECT * FROM vw_answers WHERE Qid =
'" & Qid & "' and Staffname <> '" & User.Identity.Name & "'", cnn)
Dim tlcmd As New SqlDataAdapter("SELECT * FROM vw_answers WHERE Qid
= '" & Qid & "' and Staffname = '" & User.Identity.Name & "'", cnn)
'Fill the dataset here.
Dim ds As New DataSet
cmd.Fill(ds, "TeamAnswers")
tlcmd.Fill(ds, "LeaderAnswers")
Dim RowNbr As Int32 = 0
Dim Answers(ds.Tables(0).Rows.Count) As Object
Dim Leader(ds.Tables(1).Rows.Count) As Object
'get Teamanswers into array
For Each Rw As DataRow In ds.Tables(0).Rows
Answers(RowNbr) = Rw.ItemArray()
RowNbr += 1
Next Rw
'get Leaderanswers into array
RowNbr = 0
For Each lRw As DataRow In ds.Tables(1).Rows
Leader(RowNbr) = lRw.ItemArray()
RowNbr += 1
Next lRw
'Get data into EXCEL
Dim xl As Excel.Application
Try
xl = GetObject(, "Excel.Application")
Catch ex As Exception
xl = New Excel.Application
End Try
Dim wkbk As Excel.Workbook
Dim wkst As Excel.Worksheet
Dim wksttl As Excel.Worksheet
xl.Visible = False
xl.DisplayAlerts = False
wkbk = xl.Workbooks.Open("\\bls2mbr25\HPT\HPT Questionnaire
Results.xlt")
wkst = wkbk.Sheets("rawdata")
'Export Team Answers
Dim x As Integer
Dim y As Integer
Dim range As String
For x = 0 To Answers.GetUpperBound(0)
y = x + 2
range = "A" & y & "" & y
wkst.Range(range).Value = Answers(x)
Next
'Export TeamLeader Answers
For x = 0 To Leader.GetUpperBound(0)
y = x + 2
range = "E" & y & ":G" & y
wkst.Range(range).Value = Leader(x)
Next
'Tidy up
Answers = Nothing
Leader = Nothing
ds.Dispose()
ds = Nothing
wkbk.Sheets(1).activate()
xl.Visible = True
xl.DisplayAlerts = True
End Sub