LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #3   Report Post  
Posted to microsoft.public.dotnet.framework.interop,microsoft.public.dotnet.languages.vb,microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default question about opening SQL results in Excel from ASP.NET via XML

Thanks for your reply. That's a lot of code, and it involves
hard-coding an XSLT file specific to your dataset. My application is an
Intranet-only app that runs ad-hoc queries. I suppose I could modify it
to iterate through the result set columns and build the XSLT on the
fly, but would automating Excel from a Web site on a secure Intranet be
bad? I would really prefer just specifying the correct format parameter
in the workbooks.open method...

Ken wrote:
Hi,

I would not automate excel from a website. This is probably the best
method

http://support.microsoft.com/kb/319180

Ken
------------------------------------------

"Mad Scientist Jr" wrote:

From an asp.net web page I want the user to open the results of a SQL

query in Excel, as automatically as possible (ie not having to loop
through columns, rows, in code).

For this, dataset.writexml works great
(got the code from
http://forums.devx.com/archive/index.php/t-57273.html )

The only question I have is, when Excel opens up, it isn't the view I
would prefer. It opens as a read-only workbook, I would prefer as an
xml list.

If I look at the workbooks.open method

exc.Workbooks.Open(filename:=Server.MapPath("") &
"\my_sql_results.xml")

there are additional parameters
Open (Filename As String, [UpdateLinks As Object], [ReadOnly As
Object], [Password As Object], etc...

that I haven't found any documentation on, but might allow me to
specify the "xml list" format. I am guessing the "format" parameter
would be it, but I don't know what to pass in. Can anyone help?

Thanks...


PS If I try opening the XML file locally from Excel, it prompts:

Open XML
Please select how you would like to open this file:
(x) As an XML list
( ) As a read-only workbook
( ) Use the XML Source task pane
[OK] [Cancel] [Help]

I choose the first option and click OK and Excel then prompts:

Microsoft Office Excel
The specified XML source does not refer to a schema. Excel will create
a
schema based on the XML source data.
[ ] In the future, do not show this message.
[OK] [Help]

I click OK and it opens in Excel in the format I want.

PPS Here is the full code from
http://forums.devx.com/archive/index.php/t-57273.html

01-15-2003, 04:04 PM
Here's an example. The variable "ds1" in the code below is a DataSet
filled
with a single DataTable (I used the SQL Server sample pubs database
Authors
table to test this) . I haven't tested it with multiple DataTables.

You need to add a COM reference to the Microsoft Excel Object Library.
This
sample used the Microsoft Excel 10.0 Object Library -- a version which
can
read XML files. As Constance noted, you may need to write some other
file
type to import the data into earlier versions of Excel.

' create a DataSet
Dim ds As New ds1()

' choose a file name for the output
Dim filename As String = "c:\authors_data.xml"

' open the connection and fill the DataSet
' Sample used "SELECT * FROM Authors" as the query
Me.SqlConnection1.Open()
Me.SqlDataAdapter1.Fill(ds)

' delete any existing file
If File.Exists(filename) Then
File.Delete(filename)
End If

' save the DataSet in its default XML format
ds.WriteXml(filename)

' clean up
Me.SqlConnection1.Close()
Me.SqlDataAdapter1.Dispose()
Me.SqlConnection1.Dispose()

' create an Excel Application object and make it visible
Dim exc As New Excel.Application()
exc.Visible = True

' open the saved file
exc.Workbooks.Open(filename:=filename)

' show it
exc.ActiveWindow.Visible = True

This uses Excel's defaults for the column headings, etc. and because of
the
way the DataSet persists itself in XML, the name of the DataSet shows
up in
the spreadsheet. You could easily get rid of the unwanted values or
change
column names by either processing the saved XML before loading it into
Excel, or by using Excel's object model to delete and modify the data
after
loading the XML file. Finally, you could accomplish this same result
without
going through an intermediate file by iterating through the DataSet and
stuffing the data directly into Excel worksheet cells.



 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Question when opening an excel speadhseet with multiple worksheets smart Setting up and Configuration of Excel 0 September 3rd 07 07:27 PM
Excel formula question - sending results to another cell [email protected] Excel Discussion (Misc queries) 6 January 29th 07 08:38 PM
Question in opening Excel file. lklam Excel Discussion (Misc queries) 2 February 13th 06 01:39 PM
launching excel icon results in not opening workbook BobK Excel Discussion (Misc queries) 1 April 5th 05 12:34 AM
Question about opening workbooks on Excel startup 38N90W Excel Programming 5 August 28th 04 12:57 PM


All times are GMT +1. The time now is 08:11 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"