Launching Excel client side

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 & ":D" & 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
 
S

Steve C. Orr [MVP, MCSD]

I suggest you follow the security related instructions in the article.
 
S

sweeney_jones

Steve, thanks again.

I take it you are referring to these instructions ?

"For starters, you need Excel installed on the server. To give ASP.NET
the permissions it needs to use Excel, you might need to add the line
<identity impersonate="true"/> to your web.config file or configure
your app to run under an appropriate user account. For this code to
work, you also might need to grant write privileges to your Web
directory for this account (IUSR_machinename if you use identity
impersonation)."

Well all this is done apart from the last point, IUSR_machinename.
Should this refer to the clients machine name or the server ? My
problem is that I will have unlimited amounts of users ?

Anyway, I now have the code creating the excel file and saving it
correctly, I just cab't open it in the browser window now using
Response.Redirect("Filename"), It says Page unavailable, even though I
have just used that string variable in the save as line.

Thanks
Bob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top