ich wollte mir eine "Zeichen Tabelle" in Excel erstellen.
Code: Alles auswählen
FOR i := 1 TO 255
? cName := REPLICATE(CHR(i),10)
AADD(aExcel,cName)
NEXT
nun übergebe ich das Array an Excel, egal auf was für einem OS()
Code: Alles auswählen
#PRAGMA LIBRARY( "ASCOM10.LIB" )
PROCEDURE MAIN
LOCAL aExcel := {}
LOCAL i
LOCAL cName
LOCAL ZPATH := LEFT( AppName( .t. ), LEN( AppName( .t. ) ) - LEN(AppName( .f. ) ) )
SET CHARSET TO OEM // oder ANSI ist hier egal ???
CLS
SET ALTER TO CHR2EXCE.TXT
SET ALTER ON
FOR i := 1 TO 255
? cName := REPLICATE(CHR(i),10)
AADD(aExcel,cName)
NEXT
SET ALTER OFF
SET ALTER TO
WAIT
ShowExcel(aExcel,ZPATH)
RETURN
PROCEDURE ShowExcel(aExcel,ZPATH )
LOCAL oExcel
LOCAL oWorkBook
LOCAL oSheet
LOCAL oError
LOCAL bSaveError
// save Errorblock
bSaveError := ErrorBlock()
// set new Errorblock
ErrorBlock( {|e| Break(e)} )
BEGIN SEQUENCE
// Start Excel
oExcel := CreateObject( "Excel.Application" )
IF NIL == oExcel
// Excel.Application could not be created.
MSGBOX( "Excel Verbindung konnte nicht erstellt werden" )
* ? "Error: ", ComLastError()
* ? "Description:"
* ? ComLastMessage()
BREAK
ELSE
oExcel:visible := .T. // visible
// If there is a problem, don't let excel pop up messages
oExcel:Application:DisplayAlerts := .F.
// Create a workbook from scratch
oExcel:Application:Workbooks:new()
oExcel:Application:Workbooks:add()
// Office 97 Excel creates 3 worksheets by default
// Delete 2 of them
* oExcel:Application:Worksheets(3):delete()
* oExcel:Application:Worksheets(2):delete()
// Make the first one active
oWorkBook := oExcel:activeWorkBook
oExcel:Application:Worksheets( 1 ):activate()
// Rename the sheet
oExcel:Application:Worksheets( 1 ):name := "ID_USER"
// Speed things up by creating an object containing the cells
oSheet := oExcel:Worksheets( 1 ):cells
oSheet:range( "A1:A255" ):value := aExcel
// Save the workbook
// If you don't put a path, the file will be somewhere in
// My Documents or some other Excel default directory
oWorkBook:saveas(ZPATH+"TESTI")
// Quit Excel
oExcel:Quit()
// destroy the reference
oExcel:destroy()
IF ComLastError() > 0
MSGBOX( "Error: " + STR( ComLastError() ) + CHR( 13 ) + CHR( 10 ) + "Description:" + ComLastMessage() )
* ? "Error: ", ComLastError()
* ? "Description:"
* ? ComLastMessage()
BREAK
ENDIF
ENDIF
RECOVER USING oError
// restore Errorblock
ErrorBlock( bSaveError )
* ? "Error: ", ComLastError()
* ? "Description:"
* ? ComLastMessage()
IF ComLastError() > 0
MSGBOX( "Error: " + STR( ComLastError() ) + CHR( 13 ) + CHR( 10 ) + "Description:" + ComLastMessage() )
ENDIF
IF VALTYPE( oExcel ) = 'O'
IF VALTYPE( oSheet ) = 'O'
oSheet := NIL
ENDIF
oExcel:Quit()
oExcel:Destroy()
ENDIF
END SEQUENCE
// restore Errorblock
ErrorBlock( bSaveError )
RETURN
hab ich da irgendwo einen "Denkfehler"
Die Datei TESTI.XLS liegt im selben Verzeichniss wo ihr das Demo startet.
Nachtrag : wenn ich AADD(aExcel,cName) auf ein 2-Dim erweitere AADD(aExcel,{i,cName})
und entsprechend oSheet:range( "A1:B255" ):value := aExcel angebe dann geht es ...
mag Excel 1-Dim Array nicht ?