We do this all the time, here's a demo I just threw together
One major trick I learned was use COPY and PASTE to add data to the Excel document. I don't know why, but it is
significantly faster. Columns must be preformatted for cut/paste to work properly(or you'll get weird results on text data that looks like dates or numbers). The only downside is you cannot run multiple "Excel report" programs(or use cut/paste elsewhere) while the Excel document is being populated.
Another trick - to figure out how to do something just record a macro in Excel, do whatever you wish to do, and then view the macro to see how to program it. The only tricky part is figuring out of the code goes after mxlApp. mxlWB. or mxlSheet.
The following code does work(just tested it), though it's incomplete in that I didn't include the init for gProd_CN(the connection to Oracle). Not sure why <pre> is doublespacing everything. Tried a few things to stop it, but gave up.
Option Explicit
'Global variables
Private mxlApp As Excel.Application
Private mxlWB As Excel.Workbook
Private mxlSheet As Excel.Worksheet
Private mxlRow As Long
Public Sub ExcelReport()
Call InitExcel
Call ProcessData
Call WrapUp
End Sub
Private Sub InitExcel()
Set mxlApp = New Excel.Application
Set mxlWB = mxlApp.Workbooks.Add()
Set mxlSheet = mxlWB.Worksheets.Item(1)
' set TRUE to watch it work while debugging
' set FALSE for when user runs, else typing can cause program to crash
mxlApp.Visible = True
mxlSheet.Columns(1).NumberFormat = "@" ' TEXT data
mxlSheet.Columns(2).NumberFormat = "#,##0_);[Red](#,##0)" ' NUMERIC data
mxlSheet.Columns(3).NumberFormat = "mm/dd/yyyy" ' DATE
Clipboard.Clear
Clipboard.SetText ("ITEM" & vbTab & "QTY" & vbTab & "Date")
mxlSheet.Cells(1, 1).Activate
mxlSheet.Paste
' the 3 cells just pasted are "selected" making it convient
' to adjust the look at this moment
mxlApp.Selection.Font.Bold = True
mxlApp.Selection.Font.Color = vbBlue
mxlRow = 2
End Sub
Private Sub ProcessData()
Dim RS As New ADODB.Recordset
Dim SQL As String
Dim text As String
SQL = ""
SQL = SQL & "select item,"
SQL = SQL & " rt_sub_item.on_hand('1','M1',item,revision) oh,"
SQL = SQL & " rt_sub_so.item_last_shipment_date(item,revision) last_shipped"
SQL = SQL & " From item_ccn"
SQL = SQL & " where make = 'Y'"
SQL = SQL & " and item like '0010%'"
SQL = SQL & " and rownum < 100"
RS.Open SQL, gProd_CN
Do While Not RS.EOF
text = RS("ITEM")
text = text & vbTab & RS("OH")
text = text & vbTab & RS("last_shipped")
Clipboard.Clear
Clipboard.SetText (text)
mxlSheet.Cells(mxlRow, 1).Activate
mxlSheet.Paste
With mxlApp.Selection ' put gridlines around data
.Borders(xlEdgeLeft).LineStyle = xlContinuous
.Borders(xlEdgeLeft).Weight = xlThin
.Borders(xlEdgeRight).LineStyle = xlContinuous
.Borders(xlEdgeRight).Weight = xlThin
.Borders(xlEdgeTop).LineStyle = xlContinuous
.Borders(xlEdgeTop).Weight = xlThin
.Borders(xlEdgeBottom).LineStyle = xlContinuous
.Borders(xlEdgeBottom).Weight = xlThin
.Borders(xlInsideVertical).LineStyle = xlContinuous
.Borders(xlInsideVertical).Weight = xlThin
End With
mxlRow = mxlRow + 1
RS.MoveNext
Loop
RS.Close
End Sub
Private Sub WrapUp()
Dim i As Integer
' fit columns to data
For i = 1 To 3
mxlSheet.Columns(i).EntireColumn.AutoFit
Next i
' set first cell active, else you'll be at the bottom of the sheet
mxlSheet.Cells(1, 1).Activate
' show spreadsheet, not needed if you're going to save file and quit
mxlApp.Visible = True
' uncomment this to save file & quit Excel
' mxlWB.SaveAs "c:\\testfile.xls"
' mxlWB.Close
' mxlApp.Quit
End Sub
Darrell Spice, Jr.
[link|http://home.houston.rr.com/spiceware/|SpiceWare] - We don't do Windows, it's too much of a chore