Help - Search - Members - Calendar
Full Version: Exporting Data into Excel
Weborum Webmaster Forum > Web Page Design > ASP
leir
I want to export the data into excel file and I want it to be in a tabular form.

But when I run the file, its being exported in only one column. What
I want is, for every recordset it shld. be written/exported on same row but
different column(s).

Below is my code:
-------------------------------------------------------------------------------
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open ConString, CONN_USER, CONN_PASS

strSQL2 = "SELECT * FROM tbluser WHERE dept=""Dept1"" ORDER BY name Asc ;"

Set rs2 = Server.CreateObject("ADODB.Recordset")
rs2.Open strSQL2, conn, adOpenStatic, adLockReadOnly, adCmdText

FileName = "List.xls"


sData = "<table width=""150%"" border=""0"">"
sData = "<tr><td><center><b>List</b></center></td></tr>"

sData = sData & "<tr><td width=""10%""><table width=""10%"" border=""0""><tr><td><th>Dept1</th></td></tr>"

iRecordsShown = 0
Do While Not rs2.EOF

sData = sData & "<tr><td>"
sData = sData & rs2.Fields("name").Value
sData = sData & "</td><td>"
sData = sData & rs2.Fields("local").Value
sData = sData & "</td></tr>"

iRecordsShown = iRecordsShown + 1

rs2.MoveNext
Loop

sData = sData & "</table></td>"

rs2.Close
Set rs2 = Nothing
'conn.Close
'Set conn = Nothing

''--------- 2nd Table

strSQL3 = "SELECT * FROM tbluser WHERE dept=""Dept2"" ORDER BY name Asc ;"

Set rs3 = Server.CreateObject("ADODB.Recordset")
rs3.Open strSQL3, conn, adOpenStatic, adLockReadOnly, adCmdText

sData = sData & "<td width=""10%""><table width=""10%"" border=""0""><tr><td><th>Dept2</th></td></tr>"

iRecordsShown = 0
Do While Not rs3.EOF

sData = sData & "<tr><td>"
sData = sData & rs3.Fields("name").Value
sData = sData & "</td><td>"
sData = sData & rs3.Fields("local").Value
sData = sData & "</td></tr>"

iRecordsShown = iRecordsShown + 1

rs3.MoveNext
Loop

sData = sData & "</table></td>"

'----- 3rd Table
strSQL4 = "SELECT * FROM tbluser WHERE dept=""Dept3"" ORDER BY name Asc ;"

Set rs4 = Server.CreateObject("ADODB.Recordset")
rs4.Open strSQL4, conn, adOpenStatic, adLockReadOnly, adCmdText

sData = sData & "<td width=""10%""><table width=""10%"" border=""0""><tr><td><th>Dept3</th></td></tr>"

iRecordsShown = 0
Do While Not rs4.EOF

sData = sData & "<tr><td>"
sData = sData & rs4.Fields("name").Value
sData = sData & "</td><td>"
sData = sData & rs4.Fields("local").Value
sData = sData & "</td></tr>"

iRecordsShown = iRecordsShown + 1

rs4.MoveNext
Loop

sData = sData & "</table></td>"

'--END of Table
sData = sData & "</tr></table>"

''// Write out the table
response.write sData

''// Set the Header info
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader "content-disposition","attachment; filename=" & FileName
%>

-------------------------------------------------------------------------------

Then once I run it, every table/recordset is being displayed on a single column
only.

What I want is like this: (Exported File)

List

Dept1 Dept2 Dept3

Ana 01 Kle 03 Xan 05
John 02 Sen 04 Dxc 06

-------------------------------------------------------------------------------
Anybody can give me an idea?

Thanks.
bassrek
I don't have a lot of experience in creating Excel docs from web pages, but my first guess is that you've got inconsistencies in your table structure.

Here's what you're doing with most of the ASP code stripped out:
CODE

<table width="150%" border="0">
    <tr>
         <td><center><b>List</b></center></td>
    </tr>
    <tr>
         <td width="10%">
              <table width="10%" "border="0">
                   <tr>
                        <td><th>Dept1</th></td>
                   </tr>
                   <tr>
                        <td>
                              rs2.Fields("name").Value
                        </td>
                        <td>
                               rs2.Fields("local").Value
                        </td>
                   </tr>

I'll stop there because there is the inconsistency... Inside the "Dept" tables, you have 1 row with 1 column, then the next rows all have 2 columns. That might be confusing to Excel.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.