Remplir un Excel avec une requete (Please Help Me !!!)
mayerp
Messages postés14Date d'inscriptionmercredi 21 janvier 2004StatutMembreDernière intervention16 juin 2004
-
16 juin 2004 à 16:33
conseildg
Messages postés86Date d'inscriptionlundi 16 décembre 2002StatutMembreDernière intervention18 mars 2007
-
17 juin 2004 à 18:55
Salut,
j'ai une requete de selection sous access et j'aimerai afficher le resultat dans un excel.
Je ne veux pas remplir cellule par cellule car ce serai trop long.
Je crois qu'il faut utiliser la cmd OutputTo mais bon...
Quelqu'un pourrait-il m'aider ?
Merci pour vos réponses !
A voir également:
Remplir un Excel avec une requete (Please Help Me !!!)
conseildg
Messages postés86Date d'inscriptionlundi 16 décembre 2002StatutMembreDernière intervention18 mars 2007 17 juin 2004 à 18:55
si j'ai bien compris la question , essayer ce code :
Set ApExcel = CreateObject("Excel.application") 'Creates an object
ApExcel.Visible = True 'This enable you to see the process in Excel
ApExcel.Workbooks.Add 'Adds a new book.
'Set the connection string
cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DBAccess
'Open the connection
cn.Open
'Check that the connection is open
If cn.State = 0 Then cn.Open
Set cmd.ActiveConnection = cn
cmd.CommandText = DBTable
cmd.CommandType = adCmdTable
Set rs = cmd.Execute
'Count the number of fields or column
MyFieldCount = rs.Fields.Count
'Fill the first line with the name of the fields
For MyIndex = 0 To MyFieldCount - 1
ApExcel.Cells(InitRow, (MyIndex + 1)).Formula = rs.Fields(MyIndex).Name 'Write Title to a Cell
ApExcel.Cells(InitRow, (MyIndex + 1)).Font.Bold = True
ApExcel.Cells(InitRow, (MyIndex + 1)).interior.colorindex = 36
ApExcel.Cells(InitRow, (MyIndex + 1)).WrapText = True
Next
'Fill the excel book with the values from the database
Do While rs.EOF = False
For MyIndex = 1 To MyFieldCount
ApExcel.Cells(MyRecordCount, MyIndex).Formula = rs((MyIndex - 1)).Value 'Write Value to a Cell
ApExcel.Cells(MyRecordCount, MyIndex).WrapText = False 'Format the Cell
Next
MyRecordCount = MyRecordCount + 1
rs.MoveNext
If MyRecordCount > 50 Then
Exit Do
End If
Loop
'Return the last position in the workbook
Export2XL = MyRecordCount
ceci suppose que les variables soient définies avant.