Академический Документы
Профессиональный Документы
Культура Документы
SUMMARY
This article discusses numerous methods for transferring data to Microsoft Excel from your Microsoft Visual Basic application. This article also presents the advantages and the disadvantages for each method so that you can choose the solution that works best for you.
MORE INFORMATION
The approach most commonly used to transfer data to an Excel workbook is Automation. Automation gives you the greatest flexibility for specifying the location of your data in the workbook as well as the ability to format the workbook and make various settings at run time. With Automation, you can use several approaches for transferring your data: Transfer data cell by cell Transfer data in an array to a range of cells Transfer data in an ADO recordset to a range of cells using the CopyFromRecordset method Create a QueryTable on an Excel worksheet that contains the result of a query on an ODBC or OLEDB data source Transfer data to the clipboard and then paste the clipboard contents into an Excel worksheet There are also methods that you can use to transfer data to Excel that do not necessarily require Automation. If you are running an application server-side, this can be a good approach for taking the bulk of processing the data away from your clients. The following methods can be used to transfer your data without Automation: Transfer your data to a tab- or comma-delimited text file that Excel can later parse into cells on a worksheet Transfer your data to a worksheet using ADO Transfer data to Excel using Dynamic Data Exchange (DDE) The following sections provide more detail on each of these solutions. Note When you use Microsoft Office Excel 2007, you can use the new Excel 2007 Workbook (*.xlsx) file format when you save the workbooks. To do this, locate the following line of code in the following code examples: o B o o k . S a v e A s" C : \ B o o k 1 . x l s " Replace this code with with the following line of code: o B o o k . S a v e A s" C : \ B o o k 1 . x l s x " Additionally, the Northwind database is not included in Office 2007 by default. However, you can download the Northwind database from Microsoft Office Online.
support.microsoft.com/kb/247412
1/4
12/18/13
If you transfer your data using an array rather than cell by cell, you can realize an enormous performance gain with a large amount of data. Consider this line from the code above that transfers data to 300 cells in the worksheet: o S h e e t . R a n g e ( " A 2 " ) . R e s i z e ( 1 0 0 ,3 ) . V a l u e=D a t a A r r a y This line represents two interface requests (one for the Range object that the Range method returns and another for the Range object that the Resize method returns). On the other hand, transferring the data cell by cell would require requests for 300 interfaces to Range objects. Whenever possible, you can benefit from transferring your data in bulk and reducing the number of interface requests you make.
Create a delimited text file that Excel can parse into rows and columns
Excel can open tab- or comma-delimited files and correctly parse the data into cells. You can take advantage of this feature when you want to transfer a large amount of data to a worksheet while using little, if any, Automation. This might be a good approach for a client-server application because the text file can be generated server-side. You can then open the text file at the client, using Automation where it is appropriate. The following code illustrates how you can create a comma-delimited text file from an ADO recordset: ' C r e a t eaR e c o r d s e tf r o ma l lt h er e c o r d si nt h eO r d e r st a b l eD i ms N W i n dA sS t r i n gD i mc o n nA sN e wA D O D B . C o n n e c t i o nD i mr sA s A D O D B . R e c o r d s e tD i ms D a t aA sS t r i n gs N W i n d=_" C : \ P r o g r a mF i l e s \ M i c r o s o f tO f f i c e \ O f f i c e \ S a m p l e s \ N o r t h w i n d . m d b "c o n n . O p e n
support.microsoft.com/kb/247412
2/4
12/18/13
A D O D B . R e c o r d s e tD i ms D a t aA sS t r i n gs N W i n d=_" C : \ P r o g r a mF i l e s \ M i c r o s o f tO f f i c e \ O f f i c e \ S a m p l e s \ N o r t h w i n d . m d b "c o n n . O p e n " P r o v i d e r = M i c r o s o f t . J e t . O L E D B . 4 . 0 ; D a t aS o u r c e = "&_s N W i n d&" ; "c o n n . C u r s o r L o c a t i o n=a d U s e C l i e n tS e tr s=c o n n . E x e c u t e ( " O r d e r s " ,, a d C m d T a b l e )' S a v et h er e c o r d s e ta sat a b d e l i m i t e df i l es D a t a=r s . G e t S t r i n g ( a d C l i p S t r i n g ,,v b T a b ,v b C r ,v b N u l l S t r i n g )O p e n " C : \ T e s t . t x t "F o rO u t p u tA s# 1P r i n t# 1 ,s D a t aC l o s e# 1' C l o s et h ec o n n e c t i o nr s . C l o s ec o n n . C l o s e' O p e nt h en e wt e x tf i l ei nE x c e l S h e l l" C : \ P r o g r a mF i l e s \ M i c r o s o f tO f f i c e \ O f f i c e \ E x c e l . e x e"&_C h r ( 3 4 )&" C : \ T e s t . t x t "&C h r ( 3 4 ) ,v b M a x i m i z e d F o c u s
Note If you use the Office 2007 version of the Northwind database, you must replace the following line of code in the code example: c o n n . O p e n" P r o v i d e r = M i c r o s o f t . J e t . O L E D B . 4 . 0 ; D a t aS o u r c e = "&_s N W i n d&" ; " Replace this line of code with the following line of code: c o n n . O p e n" P r o v i d e r = M i c r o s o f t . A C E . O L E D B . 1 2 . 0 ; D a t aS o u r c e = "&_s N W i n d&" ; " If your text file has a .CSV extension, Excel opens the file without displaying the Text Import Wizard and automatically assumes that the file is comma-delimited. Similarly, if your file has a .TXT extension, Excel automatically parse the file using tab delimiters. In the previous code sample, Excel was launched using the Shell statement and the name of the file was used as a command line argument. No Automation was used in the previous sample. However, if so desired, you could use a minimal amount of Automation to open the text file and save it in the Excel workbook format: ' C r e a t ean e wi n s t a n c eo fE x c e lD i mo E x c e lA sO b j e c tD i mo B o o kA sO b j e c tD i mo S h e e tA sO b j e c tS e to E x c e l= C r e a t e O b j e c t ( " E x c e l . A p p l i c a t i o n " )' O p e nt h et e x tf i l eS e to B o o k=o E x c e l . W o r k b o o k s . O p e n ( " C : \ T e s t . t x t " )' S a v ea sE x c e lw o r k b o o ka n d Q u i tE x c e lo B o o k . S a v e A s" C : \ B o o k 1 . x l s " ,x l W o r k b o o k N o r m a lo E x c e l . Q u i t For more information about using File I/O from your Visual Basic application, please see the following article in the Microsoft Knowledge Base: 172267 (http://support.microsoft.com/kb/172267/ ) RECEDIT.VBP demonstrates file I/O in Visual Basic
3. 4. 5. 6.
To add records to MyTable using ADO, you can use code similar to the following: ' C r e a t ean e wc o n n e c t i o no b j e c tf o rB o o k 1 . x l sD i mc o n nA sN e wA D O D B . C o n n e c t i o nc o n n . O p e n" P r o v i d e r = M i c r o s o f t . J e t . O L E D B . 4 . 0 ; "&_ " D a t aS o u r c e = C : \ B o o k 1 . x l s ; E x t e n d e dP r o p e r t i e s = E x c e l8 . 0 ; "c o n n . E x e c u t e" I n s e r ti n t oM y T a b l e( F i r s t N a m e ,L a s t N a m e ) "&_"v a l u e s ( ' B i l l ' ,' B r o w n ' ) "c o n n . E x e c u t e" I n s e r ti n t oM y T a b l e( F i r s t N a m e ,L a s t N a m e ) "&_"v a l u e s( ' J o e ' ,' T h o m a s ' ) "c o n n . C l o s e E x c el 2007 1. In Excel 2007, start a new workbook. 2. Add the following headers to cells A1:B1 of Sheet1: A1: FirstName B1: LastName Format cell B1 as right-aligned. Select A1:B1. On the Ribbon, click the Formulas tab, and then click Define Name . Type the name MyTable , and then click OK. Save the new workbook as C:\Book1.xlsx, and then quit Excel.
3. 4. 5. 6.
To add records to the MyTable table by using ADO, use code that resembles the following code example. ' C r e a t ean e wc o n n e c t i o no b j e c tf o rB o o k 1 . x l sD i mc o n nA sN e wA D O D B . C o n n e c t i o nc o n n . O p e n" P r o v i d e r = M i c r o s o f t . A C E . O L E D B . 1 2 . 0 ; "&_ " D a t aS o u r c e = C : \ B o o k 1 . x l s x ; E x t e n d e dP r o p e r t i e s = E x c e l1 2 . 0 ; "c o n n . E x e c u t e" I n s e r ti n t oM y T a b l e( F i r s t N a m e ,L a s t N a m e ) "&_"v a l u e s ( ' S c o t t ' ,' B r o w n ' ) "c o n n . E x e c u t e" I n s e r ti n t oM y T a b l e( F i r s t N a m e ,L a s t N a m e ) "&_"v a l u e s( ' J a n e ' ,' D o w ' ) "c o n n . C l o s e When you add records to the table in this manner, the formatting in the workbook is maintained. In the previous example, new fields added to column B are formatted with right alignment. Each record that is added to a row borrows the format from the row above it. You should note that when a record is added to a cell or cells in the worksheet, it overwrites any data previously in those cells; in other words, rows in the worksheet are not "pushed down" when new records are added. You should keep this in mind when designing the layout of data on your worksheets. Note The method to update data in an Excel worksheet by using ADO or by using DAO does not work in Visual Basic for Application environment within Access after you install Office 2003 Service Pack 2 (SP2) or after you install the update for Access 2002 that is included in Microsoft Knowledge Base article 904018. The method works well in Visual Basic for Application environment from other Office applications, such as Word, Excel, and Outlook. For more information, click the following article numbers to view the articles in the Microsoft Knowledge Base: 904953 (http://support.microsoft.com/kb/904953/ ) You cannot change, add, or delete data in tables that are linked to an Excel workbook in Office Access 2003 or in Access 2002 904018 (http://support.microsoft.com/kb/904018/ ) Description of the update for Access 2002: October 18, 2005 For additional information on using ADO to access an Excel workbook, please see the following articles in the Microsoft Knowledge Base:
support.microsoft.com/kb/247412
3/4
12/18/13
For additional information on using ADO to access an Excel workbook, please see the following articles in the Microsoft Knowledge Base: 195951 (http://support.microsoft.com/kb/195951/ ) How to query and update Excel data using ADO from ASP
REFERENCES
For more information, click the following article number to view the article in the Microsoft Knowledge Base: 306022 (http://support.microsoft.com/kb/306022/ ) How to transfer data to an Excel workbook by using Visual Basic .NET
Properties
Article ID: 247412 - Last Review: October 6, 2011 - Revision: 10.0 A P P LIE S TO
Microsoft Office Excel 2007 Microsoft Excel 2002 Standard Edition Microsoft Excel 2000 Standard Edition Microsoft Excel 97 Standard Edition Microsoft Visual Basic for Applications 6.0 Microsoft Visual Basic 6.0 Professional Edition Microsoft Office Excel 2003 Microsoft Excel 2010
support.microsoft.com/kb/247412
4/4