ASP export Excel data source of four methods: aspsky Author: tonny First, use the OWC What is OWC? OWC is the abbreviation of Office Web Compe, which is Microsoft Office Web Components, which provides flexible simultaneous mechanisms for drawing graphics in the Web. In an intranet environment, if you can assume that there is a specific browser and some powerful software (such as IE5 and Office 2000), you can provide an interactive graphics development environment using the Office WEB component. In this mode, the client workstation will share a lot of proportion in the entire task.
<% Option Explicit Class ExcelGen Private objSpreadsheet Private iColOffset Private iRowOffset Sub Class_Initialize () Set objSpreadsheet = Server.CreateObject ( "OWC.Spreadsheet") iRowOffset = 2 iColOffset = 2 End Sub Sub Class_Terminate () Set objSpreadsheet = Nothing 'Clean up End Sub public Property Let ColumnOffset (iColOff) If iColOff> 0 then iColOffset = iColOff Else iColOffset = 2 End If End Property public Property Let RowOffset (iRowOff) If iRowOff> 0 then iRowOffset = iRowOff Else iRowOffset = 2 End If End Property Sub GenerateWorksheet (objRS ) 'populates the Excel worksheet based on a Recordset's contents' Start by displaying the titles If objRS.EOF then Exit Sub Dim objField, iCol, iRow iCol = iColOffset iRow = iRowOffset For Each objField in objRS.Fields objSpreadsheet.Cells (iRow, iCol ) .Value = objField.Name objSpreadsheet.Columns (iCol) .AutoFitColumns' an Excel table font objSpreadsheet.Cells (iRow, iCol) .Font.Bold = True objSpreadsheet.Cells (iRow, iCol) .Font.Italic = False Objspreadsh EET.CELLS (IROW, ICOL) .font.size = 10 objspreadsheet.cells (irow, icol) .halignment = 2 'hin ICOL = ICOL 1 Next' Objfield 'Display All of the data do while not objrs.eof irow = iRow 1 iCol = iColOffset For Each objField in objRS.Fields If IsNull (objField.Value) then objSpreadsheet.Cells (iRow, iCol) .Value = "" Else objSpreadsheet.Cells (iRow, iCol) .Value = objField.Value objSpreadsheet .Columns (iCol) .AutoFitColumns objSpreadsheet.Cells (iRow, iCol) .Font.Bold = False objSpreadsheet.Cells (iRow, iCol) .Font.Italic = False objSpreadsheet.Cells (iRow, iCol) .Font.Size =
10 End If iCol = iCol 1 Next 'objField objRS.MoveNext Loop End Sub Function SaveWorksheet (strFileName)' Save the worksheet to a specified filename On Error Resume Next Call objSpreadsheet.ActiveSheet.Export (strFileName, 0) SaveWorksheet = (Err. Number = 0) End Function End Class Dim objRS Set objRS = Server.CreateObject ( "ADODB.Recordset") objRS.Open "SELECT * FROM xxxx", "Provider = SQLOLEDB.1; Persist Security Info = True; User ID = xxxx ; Password = xxxx; Initial Catalog = xxxx; Data source = xxxx; "Dim SaveName SaveName = Request.Cookies (" savename ") (" name ") Dim objExcel Dim ExcelPath ExcelPath =" Excel / "& SaveName &" .xls " Set objExcel = New ExcelGen objExcel.RowOffset = 1 objExcel.ColumnOffset = 1 objExcel.GenerateWorksheet (objRS) If objExcel.SaveWorksheet (Server.MapPath (ExcelPath)) then 'Response.Write "
Saved as an Excel file. Export to Excel code <