Generate Excel "Source Code

xiaoxiao2021-03-06  194

Give you a source code that you can "generate Excel" to do your own

'Option Explicit Class Excelgen Private ObjspreadsheetPrivate ICOLOFFSET Private Irowoffset

Sub class_initialize () set objspreadsheet = server.createObject ("owc.spreadsheet") rowoffset = 2 icoloffset = 2 End Sub

SUB class_Terminate ()

Set objspreadsheet = Nothing 'Clean Up

End Sub

Public Property Let Columnoffset (ICOLOFF) 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 titleness f j jr

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' Excel table set font objSpreadsheet. Cells (iRow, iCol) .Font.Bold = True objSpreadsheet.Cells (iRow, iCol) .Font.Italic = False objSpreadsheet.Cells (iRow, iCol) .Font.Size = 10 objSpreadsheet.Cells (iRow, iCol) .Halignment = 2 '

iCol = iCol 1 Next 'objField' Display all of the dataDo 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.ITIC = false objspreadsheet.cells (irow, icol) .font.size = 10 end if ICol = iCol 1 Next 'Objfield Objrs.movenext Loopend Sub

Function SaveWorksheet (Strfilename) 'Save The Worksheet To a Specified FileName

On Error ResMe next call objspreadsheet.activesheet.export (strfilename, 0) Saveworksheet = (err.number = 0) end function

End Class Dim objRS Set objRS = Server.CreateObject objRS.Open "SELECT btel as number FROM bmember", "Provider = SQLOLEDB.1 (" ADODB.Recordset "); Persist Security Info = True; User ID = sa; Password =; Initial Catalog = 7056a; Data source = 127.0.0.1; "Dim SaveName SaveName = Request.Cookies (" savename ") (" name ") Dim objExcel Dim ExcelPath ExcelPath =" Excel / gg.xls "Set objExcel = New ExcelGen objExcel. Rowoffset = 1 objexcel.columnoffset = 1 Objexcel.generateWorksheet (OBJRS)

If Object (Server.Mappath (ExcelPath)) Then Response.write " Saved as Excel file. Download " else response.write "has an error during the save!" Endiff

转载请注明原文地址:https://www.9cbs.com/read-128617.html

New Post(0)