Вы находитесь на странице: 1из 2

18/11/2010 Importing data from a Microsoft Excel …

Article ID: 949529 - Last Review: February 21, 2008 - Revision: 1.1
Importing data from a Microsoft Excel 2007 workbook using Visual
FoxPro 9.0
Source: Microsoft Support

RAPID PUBLISHING

RAPID PUBLISHING ARTIC LES PROVIDE INFORMATION DIREC TLY FROM WITHIN
THE MIC ROSOFT SUPPORT ORGANIZATION. THE INFORMATION C ONTAINED HEREIN IS C REATED IN RESPONSE TO
EMERGING OR UNIQUE TOPIC S, OR IS INTENDED SUPPLEMENT OTHER KNOWLEDGE BASE INFORMATION.

Action

You have a Microsoft Excel 2007 workbook (.XLSX). You want to import data from it into a Microsoft Visual FoxPro
(VFP) table using VFP.

Resolution

There are a number of ways of accomplishing this task:

1. Export the Excel sheet(s) as comma delimited files (.C SV) and use the IMPORT command or the Import Wizard
inside VFP to import the file(s).

2. Write custom VFP OLE automation code to automate Excel and extract the data to VFP.

3. Use the Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb) ODBC driver included with the 2007 Office System
Driver: Data C onnectivity C omponents package to access and extract data from the Excel workbook, either through
a VFP Remote View or programmatically, as in the following sample code:

*-----------------------------------
* AUTHOR: Trevor Hancock
* C REATED: 02/15/08 04:55:31 PM
* ABSTRAC T: C ode demonstrates how to connect to
* and extract data from an Excel 2007 Workbook
* using the "Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)"
* from the 2007 Office System Driver: Data C onnectivity C omponents
*-----------------------------------
LOC AL lcXLBook AS STRING, lnSQLHand AS INTEGER, ;
lcSQLC md AS STRING, lnSuccess AS INTEGER, ;
lcC onnstr AS STRING
C LEAR

lcXLBook = [C :\SampleWorkbook.xlsx]

lcC onnstr = [Driver=] + ;


[{Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};] + ;
[DBQ=] + lcXLBook

IF !FILE( lcXLBook )
? [Excel file not found]
RETURN .F.
ENDIF
*-- Attempt a connection to the .XLSX WorkBook.

support.microsoft.com/default.aspx?s… 1/2
18/11/2010
p
Importing data from a Microsoft Excel …
*-- NOTE: If the specified workbook is not found,
*-- it will be created by this driver! You cannot rely on a
*-- connection failure - it will never fail. Ergo, success
*-- is not checked here. Used FILE() instead.
lnSQLHand = SQLSTRINGC ONNEC T( lcC onnstr )

*-- C onnect successful if we are here. Extract data...


lcSQLC md = [Select * FROM "Sheet1$"]
lnSuccess = SQLEXEC ( lnSQLHand, lcSQLC md, [xlResults] )
? [SQL C md Success:], IIF( lnSuccess > 0, 'Good!', 'Failed' )
IF lnSuccess < 0
LOC AL ARRAY laErr[1]
AERROR( laErr )
? laErr(3)
SQLDISC ONNEC T( lnSQLHand )
RETURN .F.
ENDIF

*-- Show the results


SELEC T xlResults
BROWSE NOWAIT
SQLDISC ONNEC T( lnSQLHand )

More Information

The 2007 Office System Driver: Data C onnectivity C omponents package is


available for download here:
http://www.microsoft.com/downloads/details.aspx?FamilyID=7554F536-8C 28-4598-9B72-
EF94E038C 891&displaylang=en (http://www.m icrosoft.com /downloa ds/de ta ils.aspx ?Fam ilyID=7554F536-8C 28-4598-
9B72-EF94E038C 891&displaylang=e n)

DISC LAIMER

MIC ROSOFT AND/OR ITS SUPPLIERS MAKE NO REPRESENTATIONS OR


WARRANTIES ABOUT THE SUITABILITY, RELIABILITY OR AC C URAC Y OF THE INFORMATION C ONTAINED IN THE
DOC UMENTS AND RELATED GRAPHIC S PUBLISHED ON THIS WEBSITE (THE “MATERIALS”) FOR ANY PURPOSE. THE
MATERIALS MAY INC LUDE TEC HNIC AL INAC C URAC IES OR TYPOGRAPHIC AL ERRORS AND MAY BE REVISED AT ANY
TIME WITHOUT NOTIC E.

TO THE MAXIMUM EXTENT PERMITTED BY APPLIC ABLE LAW, MIC ROSOFT AND/OR ITS SUPPLIERS DISC LAIM AND
EXC LUDE ALL REPRESENTATIONS, WARRANTIES, AND C ONDITIONS WHETHER EXPRESS, IMPLIED OR STATUTORY,
INC LUDING BUT NOT LIMITED TO REPRESENTATIONS, WARRANTIES, OR C ONDITIONS OF TITLE, NON
INFRINGEMENT, SATISFAC TORY C ONDITION OR QUALITY, MERC HANTABILITY AND FITNESS FOR A PARTIC ULAR
PURPOSE, WITH RESPEC T TO THE MATERIALS.

APPLIES TO
Keywords: kbnomt kbrapidpub KB949529

Vous avez besoin d'une aide supplémentaire ?


C ontactez le support technique par email, en ligne ou par téléphone

Aide et Support Microsoft


©2010 Microsoft

support.microsoft.com/default.aspx?s… 2/2

Вам также может понравиться