473,379 Members | 1,174 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Launching Excel client side

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
Nov 19 '05 #1
4 2272
There are a variety of ways of launching Excel on the client and server
side.

This article covers most of your options and supplies sample code for them
all:
http://SteveOrr.net/Articles/ExcelExport.aspx

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net

"Staticbob" <ja*********@nospampls.bluestone.plc.uk> wrote in message
news:63**********************************@microsof t.com...
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

Nov 19 '05 #2
Thanks Steve,

I had already studied your site and was trying to implement the 1st
option as I would really like to use a template as a master and create
a new file from it, throwing in some data from arrays.

I have tried the code listed above but am now getting this error . . .

Thanks,
Bob
Server Error in '/' Application.
--------------------------------------------------------------------------------

Server execution failed
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.Runtime.InteropServices.COMException: Server
execution failed

Source Error:

An unhandled exception was generated during the execution of the
current web request. Information regarding the origin and location of
the exception can be identified using the exception stack trace below.
Stack Trace:
[COMException (0x80080005): Server execution failed]
HPT.HPTReports.btn_excel_Click(Object sender, EventArgs e) in
c:\inetpub\wwwroot\HPT\HPTReports.aspx.vb:205
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108

System.Web.UI.WebControls.Button.System.Web.UI.IPo stBackEventHandler.RaisePostBackEvent(String
eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler
sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData)
+33
System.Web.UI.Page.ProcessRequestMain() +1277

Nov 19 '05 #3
I suggest you follow the security related instructions in the article.

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net
<sw***********@hotmail.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...
Thanks Steve,

I had already studied your site and was trying to implement the 1st
option as I would really like to use a template as a master and create
a new file from it, throwing in some data from arrays.

I have tried the code listed above but am now getting this error . . .

Thanks,
Bob
Server Error in '/' Application.
--------------------------------------------------------------------------------

Server execution failed
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.Runtime.InteropServices.COMException: Server
execution failed

Source Error:

An unhandled exception was generated during the execution of the
current web request. Information regarding the origin and location of
the exception can be identified using the exception stack trace below.
Stack Trace:
[COMException (0x80080005): Server execution failed]
HPT.HPTReports.btn_excel_Click(Object sender, EventArgs e) in
c:\inetpub\wwwroot\HPT\HPTReports.aspx.vb:205
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108

System.Web.UI.WebControls.Button.System.Web.UI.IPo stBackEventHandler.RaisePostBackEvent(String
eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler
sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData)
+33
System.Web.UI.Page.ProcessRequestMain() +1277

Nov 19 '05 #4
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

Nov 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: cv | last post by:
Hi all, I have to copy two set of data from 2 files(notepad/excel) say, products and their corresponding prices to list/textarea/table. I should be able to retrieve the product and corresponding...
2
by: Fred | last post by:
Hi. How do I import while mapping an excel table to an access table please??? I've searched around and all I can find is a software product or code that does the same thing as the access...
6
by: sebastien | last post by:
Hi I have an asp.net application that use Session variables. What I want to do is to launch an second instance of this application by a link inside the first application. The problem is: when I...
6
by: Steve Richter | last post by:
I am getting error in a vbscript: ActiveX component cant create object: Excel.Application. The vbscript code is: Dim objExcel Set objExcel = CreateObject("Excel.Application") I am pretty...
8
by: jack | last post by:
Access is denied. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it...
6
by: Mark Rae | last post by:
Hi, My client has asked me to provide a "quick and dirty" way to export the contents of a DataGrid to both Excel for analysis and Word for editing and printing, so I'm investigating client-side...
2
by: SpamUsHereInstead | last post by:
Hi, I have Googled the internet beyond belief for a solution to this and keep running into obstacles: I have written an ASP application using our IIS server which retrieves data and displays...
2
by: Keith | last post by:
Hi all, I'd like to ask if I use "Dim ExelApp AS New Excel.Application" for my web application where I need to open up a multiiple sheets Excel template file on the server, write some data and...
2
by: kj | last post by:
Executive summary: Is there a way for the client side JavaScript to tell the browser to use MS Excel to view some data? The question: I'm changing an webapp from standard CGI to pure...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.