2

我是 teradata 的新手。我在 teradata 上运行了一个 sql 代码,它给了我一个表作为输出。每当我运行此代码时,我希望此表直接填充到 Excel 工作表中。我该怎么做?谢谢你。

4

3 回答 3

2

最简单的方法是通过 ODBC 从 Excel 运行查询。因此,您在 Excel 中设置了外部数据源。每当您刷新数据时,我们都会针对 Teradata 执行查询。

在 Excel 2010 中,它位于、Data或from如果您有 OLEDB 设置。From Other SourcesFrom Microsoft QueryData Connection Wizard

于 2013-08-12T04:43:10.637 回答
0

Teradata SQL Assistant has the ability to automatically export results to a csv which Excel is usually the default program to open csv.

You'll want to select "Export Results" from under the "File" menu in SQL Assistant.

If it's not exporting properly then select Tools->Options->Export/Import and change the delimiter to a comma.

Unfortunately from within SQL Assistant there's no way to automatically save as xls or xlsx.

于 2013-08-14T06:31:04.550 回答
0

使用 excel 本身的 ODBC 连接是一次性使用的最快方式。

如果您计划多次运行可以从另一种语言运行的东西,它会增加一个步骤,但如果您希望安排定期运行的任务,这是非常值得的。这是python中的一些代码:

import pyodbc as pc

conn = pc.connect('DRIVER={Teradata};DBCNAME=dbname;UID=username;PWD=password;QUIETMODE=YES;')
curs = conn.cursor()

rows = curs.execute("""
    select *
    from table
    ;""").fetchall()

f = open('myfile1.csv', 'w')
for row in rows:
    s = [str(x) for x in row]
    f.write(','.join(s) + '\n')
    print ','.join(s) + '\n'
f.close()

用于打开 .csv 文件的默认程序是 excel。

如果您明确需要它是一个 excel 文件,您可以使用 xlwt 模块或让 python 执行 vba 代码以导入现有文件:

import win32com.client
import xl

excel = win32com.client.Dispatch("Excel.Application")
f = xl.Workbook("C:\...\Target.xlsm")
excel.Run("%s!testmacro"%f)

即时报道!

于 2013-08-12T23:07:23.630 回答