折腾我一天的问题,结果居然是“微软的ODBC不支持EXCEL的更新”!

Can I use ODBC to write to an existing Excel file?

Title  Writing to Excel via ODBC
Author Kevin S. Turner, StataCorp
Date April 2004; updated April 2005

The general answer is yes; however, be aware of some limitations of the Excel ODBC driver.

Closing Excel worksheet before using Stata

For the Excel ODBC driver to work properly, any instance of Excel that has the spreadsheet open must first be closed. If the worksheet is left open in Excel and you try to perform ODBC operations on the same worksheet using Stata, an error will result. Unfortunately, the error message that the driver issues is not descriptive and does not provide any guidance about closing the other application.

Unsupported SQL

The Excel ODBC driver does not support the use of SQL statements DELETE, UPDATE, or ALTER. Microsoft lists this shortcoming on their support Web site in an article written for Excel 97, even though it applies to later Excel versions:

The driver will not support DELETE, UPDATE, or ALTER TABLE statements. While it is possible to update values, DELETE statements will not remove a row from a table based on an Excel spreadsheet. These operations are not supported. Basically, you can only append (insert) to a table.

Due to the SQL limitations, the following Stata ODBC insert command with the overwrite option will fail because the operation first clears the table using a DELETE statement.

        . webuse restaurant, clear
        . odbc insert, table(restaurant) dsn(test_one) overwrite

If you perform any direct SQL on an Excel worksheet, you will also not be able to use these three commands. However, you can still append data to an existing table, for example, using the following command:

        . webuse restaurant, clear
        . odbc insert, table(restaurant) dsn(test_one) insert 

You can also use the create option to create a new sheet or table.

        . webuse restaurant, clear
        . odbc insert, table(restaurant) dsn(test_one) create

Solutions to limitation

If you really need a repository for data, consider using a database such as Microsoft Access. The database does not have to be anything fancy, but it would provide more robust capabilities for storing and updating data than an Excel spreadsheet would. If needed, an Excel spreadsheet can then be populated with portions of the Access database. However, Stata would interact with the Access database via ODBC.

If you are serious about sticking with an Excel spreadsheet, you might conclude that ODBC was simply overkill and that you only needed to outsheet the data and import it via Excel. You might lose some characteristics of your spreadsheet, such as colors, display formats, and dimensions, but you would have lost them using ODBC as well. When transferring data between formats, only the data are transferred. Information that is useless for one application will typically not be carried over.

If you are using Excel in Office XP or Office 2003, you may also consider the added XML support, which works with Stata 9 or later. For more information, see xmluse in [D] xmlsave.

Also, there are numerous ways to import and export data with Microsoft products. Any combination of these, paired with Stata’s numerous data-management commands, might produce an ad hoc solution that works perfectly for your particular needs.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值