Author: tonny from: Wei net power http://www.weiw.com/ For reprint, please specify the source. Author's order: This type of knowledge, there are a lot on the Internet, which is just a summary. First, use 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 graphic development environment using the Office WEB component. In this mode, the client workstation will share a lot of proportion in the entire task. Detailed description can also be found on this site.
<% Option ExplicitClass ExcelGenPrivate objSpreadsheet Private iColOffset Private iRowOffsetSub Class_Initialize () Set objSpreadsheet = Server.CreateObject ( "OWC.Spreadsheet") iRowOffset = 2iColOffset = 2End Sub Sub Class_Terminate () Set objSpreadsheet = Nothing 'Clean upEnd Sub Public Property Let ColumnOffset (iColOff ) If iColOff> 0 theniColOffset = iColOffElseiColOffset = 2End IfEnd Property Public Property Let RowOffset (iRowOff) If iRowOff> 0 theniRowOffset = iRowOffElseiRowOffset = 2End IfEnd Property Sub GenerateWorksheet (objRS) 'populates the Excel worksheet based on a Recordset's contents'Start by displaying the titlesIf objRS.EOF then Exit SubDim objField, iCol, iRowiCol = iColOffsetiRow = iRowOffsetFor Each objField in objRS.FieldsobjSpreadsheet.Cells (iRow, iCol) .Value = objField.NameobjSpreadsheet.Columns (iCol) .AutoFitColumns' an Excel table font objSpreadsheet .Cells (irow, icol) .font.bold = true objspreadsheet.cells (irow, icol) .font.italic = falseobjspreadsheet.cells (iRow, Icol) .font.size = 10objSpreadsheet.Cells (iRow, iCol) .Halignment = 2 'centrally iCol = iCol 1Next' objField'Display all of the data Do While Not objRS.EOFiRow = iRow 1iCol = iColOffsetFor Each objField in objRS.FieldsIf IsNull (objField. Value) thenobjSpreadsheet.Cells (iRow, iCol) .Value = "" ElseobjSpreadsheet.Cells (iRow, iCol) .Value = objField.ValueobjSpreadsheet.Columns (iCol) .AutoFitColumns objSpreadsheet.Cells (iRow, iCol) .Font.Bold = False Objspreadsheet.cells (irow, icol) .font.italic = falseobjspreadsheet.cells (iRow, ICOL) .Font.size = 10 end ificol = iCol 1Next '
objFieldobjRS.MoveNext Loop End Sub Function SaveWorksheet (strFileName) 'Save the worksheet to a specified filenameOn Error Resume NextCall objSpreadsheet.ActiveSheet.Export (strFileName, 0) SaveWorksheet = (Err.Number = 0) End FunctionEnd Class Dim objRSSet 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 SaveNameSaveName = Request.Cookies (" savename ") (" name ") Dim objExcelDim ExcelPathExcelPath =" Excel / "& SaveName &" .xls "Set objExcel = New ExcelGenobjExcel.RowOffset = 1objExcel.ColumnOffset = 1objExcel.GenerateWorksheet (objRS) If Object (Server.Mappath (ExcelPath) "1'Response.write "
has been saved as an Excel file. Export to Excel code