Monday, December 8, 2008

Write on excel sheets Using Visual basic 6.0

if you want to export data to Excel you can do two things :
1. load Excel application
2. using Recordset to import the data (you must have an excel file to do this)first of all you must add 'Microsoft Excel [version] Object Library' reference to your program you can do this by choose menu Project >> References ..
1. Load Excel App (you must have Microsoft Excell installed)
a. declare variable as Excel.Application and Excel.Workbookex.
dim objExcel as Excel.Application,
objBook as Excel.WorkBookb. open Excel application
On Error Resume Next
Set objExcel = GetObject(, "Excel.Application") 'if excel already open you can use
GetObject
If Err.Number ThenErr.ClearSet objExcel = CreateObject("Excel.Application") 'or CreateObject to open new Excel Applicationc. create new workbook and new sheetSet
objWorkbook = objExcel.Workbooks.AddobjWorkbook.ActiveSheet.Cells(1, 1) = value ' cell(1,1) means cell A1 ;
value is the value you want to fillnote :
you can also use range to merge cell and fill the valueex.objWorkbook.ActiveSheet.Range("A4:A6").Merge
objWorkbook.ActiveSheet.Range("A4:A6").Value = value
2. Using Recordset (you musn't have Microsoft Excell installed but you must have an excel file with the first row filled with any value --> for a column header; otherwise this 'Insert Into' statement wont work)
a. create and open a connection (i'm using ADODim Conn As ADODB.ConnectionSet Conn = New ADODB.ConnectionConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=FileName.xls;Extended Properties=Excel 4.0;"b. fill the value to excell sheetsConn.Execute "Insert Into [Sheet1$] (A1) VALUES ('value') " ' [Sheet1$] is an active sheet

No comments: