|
OscarSierra -> RE: Help on exporting Access data to a Excel sheet. (7/30/2005 7:36:23)
|
I use this, don't work with MySQL and require Excel installed on the server: ///////////////// the include file dataconn.asp here <% Set Conn = Server.CreateObject("Adodb.Connection") ' DSNless connection to Database or whatever you have .... Dim DSNtemp DSNtemp="Provider=MSDASQL;" DSNtemp=DSNtemp & "DRIVER={Microsoft Access Driver (*.mdb)}; " DSNtemp=DSNtemp & "DBQ=C:\Documents and Settings\Administrator\My documants\table_test.mdb;" 'DSNtemp=DSNtemp & "DBQ=" & Server.MapPath(".\table_test.mdb") & ";" Conn.open DSNtemp %> ////////////////////////// the excel.asp goes here <%@ Language="VbScript"%> <!--#include file="dataconn.asp"--> <% Dim xlWorkSheet ' Excel Worksheet object Dim xlApplication Dim uniqueName uniqueName = replace(Now()," ","") uniqueName = replace(uniqueName,"/","") uniqueName = replace(uniqueName,":","") myfilename = uniqueName Set xlApplication = CreateObject("Excel.Application") xlApplication.Visible = False xlApplication.Workbooks.Add Set xlWorksheet = xlApplication.Worksheets(1) '--------------------------------------------------------------------------- ' The SQL below selects all the records from the Table table_1 '--------------------------------------------------------------------------- msql = "SELECT * FROM table_1" Set RS=Conn.Execute(msql) '--------------------------------------------------------------------------- ' The The following code generates the header of the Excell spreadsheet '--------------------------------------------------------------------------- xlWorksheet.Cells(1,1).Value = "rec-id" xlWorksheet.Cells(1,1).Interior.ColorIndex = 24 xlWorksheet.Cells(1,2).Value = "customer" xlWorksheet.Cells(1,2).Interior.ColorIndex = 24 xlWorksheet.Cells(1,3).Value = "name" xlWorksheet.Cells(1,3).Interior.ColorIndex = 24 xlWorksheet.Cells(1,4).Value = "address" xlWorksheet.Cells(1,4).Interior.ColorIndex = 24 xlWorksheet.Cells(1,5).Value = "zip" xlWorksheet.Cells(1,5).Interior.ColorIndex = 24 xlWorksheet.Cells(1,6).Value = "insurance" xlWorksheet.Cells(1,6).Interior.ColorIndex = 24 iRow = 2 If Not RS.EOF Then Do Until RS.EOF For i = 0 To RS.fields.Count-1 xlWorksheet.Cells(iRow,i + 1).Value = RS.fields(i) xlWorkSheet.Cells(iRow,i + 1).Interior.ColorIndex = 2 Next iRow = iRow + 1 RS.MoveNext Loop End If strFile = myfilename xlWorksheet.SaveAs Server.MapPath(".") & "\" & strFile & ".xls" xlApplication.Quit ' Close the Workbook Set xlWorksheet = Nothing Set xlApplication = Nothing RS.Close Conn.Close Set RS = Nothing Set Conn = Nothing Response.Write("Click <A HRef=" & strFile & ".xls>Here</A> to get XLS file") %>
|
|
|
|