Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Disecting an Excel Worksheet
Message
 
 
À
07/03/2009 09:32:26
Raza Malik
Universal Accounting Software
Edgewater, New Jersey, États-Unis
Information générale
Forum:
Visual FoxPro
Catégorie:
COM/DCOM et OLE Automation
Versions des environnements
Visual FoxPro:
VFP 9 SP2
Divers
Thread ID:
01386393
Message ID:
01386450
Vues:
65
You can use VARTYPE() to check the type of the values in the Excel columns.
oExcel = CreateObject("Excel.Application")
oWorkbook = oExcel.Workbooks.Open(lcXlsFile)

*oExcel.Visible = .T.
oSheet = oWorkbook.ActiveSheet

lnHeaderRow = 1
lnLastCol = oSheet.UsedRange.Columns.Count

lnItemCol = 0
lnPriceCol = 0

FOR lnCol=1 TO lnLastCol
	lcHeader = oSheet.Cells(lnHeaderRow, lnCol).Value
	DO CASE
	CASE UPPER(ALLTRIM(lcHeader)) == "ITEM"
		lnItemCol = lnCol
	CASE UPPER(ALLTRIM(lcHeader)) == "PRICE"
		lnPriceCol = lnCol
	ENDCASE	
ENDFOR

IF lnPriceCol = 0 OR lnItemCol = 0
	MESSAGEBOX("Wrong spreadsheet")
	RETURN .F.
ENDIF

CREATE CURSOR Items (Item C(6), Price N(10,2))  
lnLastRow = oSheet.UsedRange.Rows.Count
FOR lnRow = lnHeaderRow + 1 TO lnLastRow
	lvItemValue = oSheet.Cells(lnRow, lnItemCol).Value
	DO CASE
	CASE VARTYPE(lvItemValue) == "N"
		lcItem = PADL(INT(lvItemValue), 6, "0") 
	CASE VARTYPE(lvItemValue) == "C"
		lcItem = lvItemValue	
	OTHERWISE
		* Wrong type, do something	
		LOOP	&& Ignore the row
	ENDCASE
	
	lvPriceValue = oSheet.Cells(lnRow, lnPriceCol ).Value
	DO CASE
	CASE VARTYPE(lvPriceValue) == "C"
		lnPrice = CAST(lvPriceValue AS N(10,2))
	CASE VARTYPE(lvPriceValue) == "N"
		lnPrice = lvPriceValue 
	OTHERWISE	
		* Wrong type, do something	
		LOOP	&& Ignore the row
	ENDCASE

	INSERT INTO Items VALUES (lcItem, lnPrice)
ENDFOR

...

>
>I have an Excel worksheet which can have many columns. The header column has data which correspond to the field names in a DBF table. How can I identify which column #'s are ITEM & PRICE and whether thay contain character or numeric data ?
>
>Listed below is my attempted code. Any help is greately appreciated.
>
>Thank You in advance..
>
>Raz
>
>
>
>
>LOCAL oExcel, lcItem, lnCost
>lcFile = "HITACHI.XLS"
>
>oExcel = CREATEOBJECT("Excel.Application",)
>oExcel.Workbooks.Open(lcFile)
>oExcel.Workbooks(1).Sheets(1).Select()
>oExcel.Cells.Select
>mnLastRow = oExcel.activesheet.UsedRange.ROWS.COUNT
>rr = oExcel.activesheet.UsedRange.ROWS.COUNT
>mcLastRow = ALLTRIM(STR(mnLastRow))
>
>lcItem = oExcel.Cells(rr,??).value		&& oExcel.Cells(rr,08).value
>lnCost = oExcel.Cells(rr,??).value		&& oExcel.Cells(rr,12).value
>
>
--sb--
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform