Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

When rendering page as excel, can u name the tabs? 2

Status
Not open for further replies.

VBGuy2112

Programmer
Feb 19, 2002
58
US
When I write an html page as an excel speadsheet using the following:

Dim r As HttpResponse = GetResponse()
r.Buffer = True
r.ContentType = "application/vnd.ms-excel"
r.Write(obuilder.ToString)
r.Flush()



I get a nice excel speadsheet from the HTML table structures but the tab (or worksheet name) has a strange label that looks like part of the query string. Is there a way I can name this tab myself?

Also, is there a way to write a page as excel and have multiple worksheets/tabs? I promise I will give stars if someone can answer these. :)
 
If you're using asp, you can create the excel file using xml like this....


<%
response.expires = 0

monNum = month(date())
dayNum = day(date())

if monNum < 10 then monNum = &quot;0&quot; & monNum
if dayNum < 10 then dayNum = &quot;0&quot; & dayNum

g_filename=&quot;report/ThreeDayReport&quot;& &quot;_&quot; & year(date()) & monNum & dayNum &&quot;.xls&quot;

set fso = createobject(&quot;scripting.filesystemobject&quot;)


if fso.FileExists(server.mappath(g_fileName)) then
''response.write (&quot;found&quot;)
else
Set objConn = Server.CreateObject( &quot;ADODB.Connection&quot; )
Set objCmd = Server.CreateObject( &quot;ADODB.Command&quot; )
objConn.Open Application(&quot;connectString&quot;)
Set objCmd.ActiveConnection = objConn
objCmd.CommandTimeout = 300

with objCmd
.commandText = &quot;br_sp_threeDayReport&quot;
.CommandType = &H0004
End With

set objrs = objcmd.execute()
recCount = 0
'this routine is simply writing a HTML table'
do while not objrs.eof
if lcase(objrs(&quot;created&quot;)) <> &quot;null&quot; then
if lastName <> &quot;&quot; then
if lastName <> objrs(&quot;lastName&quot;) then strOut = strOut & &quot;<tr></tr>&quot; '<
end if
recCount = recCount + 1
lastName = objrs(&quot;lastName&quot;)
end if
strOut = strOut & &quot;<td style='width: 100px'>&quot; & objrs(0) & &quot;</td>&quot;
objrs.movenext
loop

if recCount > 0 then ' <'records found'

Set act = fso.CreateTextFile(server.mappath(g_filename), true)
with act
.WriteLine &quot;<html xmlns:x=&quot;&quot;urn:schemas-microsoft-com:eek:ffice:excel&quot;&quot;>&quot;
.WriteLine &quot;<head>&quot;
.WriteLine &quot;<!--[if gte mso 9]><xml>&quot;
.WriteLine &quot;<x:ExcelWorkbook>&quot;
.WriteLine &quot;<x:ExcelWorksheets>&quot;
.WriteLine &quot;<x:ExcelWorksheet>&quot;
'the following line names the tab (you can make multiple tabs/sheets)
.WriteLine &quot;<x:Name>Items Over 3 Days Old</x:Name>&quot;
.WriteLine &quot;<x:WorksheetOptions>&quot;
.WriteLine &quot;<x:pageSetup>&quot;
'the following line writes the header for the worksheet
.WriteLine &quot;<x:Header>BRTT Active Requests Over 3 Days Old</x:Header>&quot;
.WriteLine &quot;</x:pageSetup>&quot;
.WriteLine &quot;<x:print>&quot;
.WriteLine &quot;<x:ValidPrinterInfo/>&quot;
.WriteLine &quot;</x:print>&quot;
.WriteLine &quot;</x:WorksheetOptions>&quot;
.WriteLine &quot;</x:ExcelWorksheet>&quot;
.WriteLine &quot;</x:ExcelWorksheets>&quot;
.WriteLine &quot;</x:ExcelWorkbook>&quot;
.WriteLine &quot;</xml>&quot;
.WriteLine &quot;<![endif]--> &quot;
.WriteLine &quot;</head>&quot;
'the following line writes the table to excel
.WriteLine strOut
end with
act.close
end if 'recCount > 0'
end if

Get the Best Answers! faq333-2924
Is this an asp FAQ? faq333-3048
Tek-Tips Best Practices: FAQ183-3179
 
Doesn't look like I can give stars. That must be left to the webmasters. I'm sorry. I thought I would be able to do that.

This is the best I can do:

***
 
mwolf00

I'm very intrigued by your solution, it has been most helpful so far. I am however having a problem trying to insert data into my 2nd worksheet.

I have attempted to follow your example, repeated the header 2 times to get 2 tabs, however repeating the
insert of the table 2 times (after the closing head tag) I end up with 2 tables on my first worksheet.

You wouldnt happen to know how I can get the 2nd table to appear on my 2nd worksheet?. Is another XML tag I'm not familiar with required here?

Thanks in advance,

S.

(The more ASP.NET I do, the more Javascript I find myself using)
 
At the Microsoft Office Developers Site there is a reference list and Example page</a> that use multiple sheets. It seems, unfortunately, to be compatible only with Office/Excel 2002/XP, and not Excel2000.
The only alternative is the:
<x:ExcelWorksheets>
<x:ExcelWorksheet>
<x:WorksheetSource HRef=&quot;./test_file/sheet001.htm&quot;/>
</x:ExcelWorksheet>
<x:ExcelWorksheet>
<x:WorksheetSource HRef=&quot;./test_file/sheet002.htm&quot;/>
</x:ExcelWorksheet>
</x:ExcelWorksheets>
Structure, which calls external files which are separate.
 
OldJacques -

You are right about Excel2000 vs 2002... 2002 is fully capable for XML while 2000 is VERY limited (damn Microsoft for making me need to upgrade).

You will find that your solution above does write both worksheets. HOWEVER, I've only been able to write data to one of them (unless, of course, I have Excel 2002)....

I haven't tried
<x:WorksheetSource HRef=&quot;./test_file/sheet002.htm&quot;/>

have you tested on Excel2000????


Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top