方法一:简单实用
string docname, named
integer value
value = GetFileSaveName("选择文件", &
docname, named, "XLS", &
"Excel文件(*.XLS),*.XLS")
IF value = 1 THEN
if dw_1.SaveAsAscii(docname)=1 then
messagebox('系统信息',docname+"保存成功!")
end if
end if
方法二:
/**********************************************
* 函数名称: changexecl(datawindow,tile) *
* 函数用法:datawindow为要转化的数据窗口; *
* tile为数据窗口的标题。 *
* 举例:changexecl(dw_1,"这是我们的世界") *
**********************************************/
long numcols , numrows , c, r
OLEObject xlapp , xlsub
int ret,coli
string colname,colname1
numcols = long(dw_data.Object.DataWindow.Column.Count)
numrows = dw_data.RowCount()
xlApp = Create OLEObject
ret = xlApp.ConnectToNewObject( "Excel.application" )
if ret < 0 then
MessageBox("Connect to Excel Failed !",string(ret))
return
end if
xlApp.Application.Workbooks.add()
xlApp.Application.Visible = true
xlsub = xlapp.Application.ActiveWorkbook.Worksheets[1]
string colnum //判断是A、B...或AA、AB...还是BA、BB、BC...
if integer(numcols) > 26 then
string colnumd,colnumd1
int numcols1,numcols2
numcols1 = mod(numcols,26)
numcols2 = numcols / 26
colnumd = char(numcols1 + 96)
colnumd1 = char(numcols2 + 96)
colnum ="'" + colnumd1 + colnumd + "'"
else
colnum = char(numcols + 96)
end if
xlsub.cells[1,1] = tabname //标题
xlsub.cells[1,1].HorizontalAlignment = 3
xlsub.cells[1,1].VerticalAlignment = 3
xlsub.cells[1,1].Font.Size = 20
xlsub.range("a1:" + colnum + "1").Merge()
string ls_colname
integer i
for i = 1 to numcols
//得到标题头的名字
ls_colname = dw_data.describe('#' + string(i) + ".name") + "_t"
xlsub.cells[2,i] = dw_data.describe(ls_colname + ".text")
next
//画表格线
string ls_range
ls_range = "A2:" + colnum + Trim(string(numrows+2))
xlsub.range(ls_range).borders(1).linestyle = 1
xlsub.range(ls_range).borders(2).linestyle = 1
xlsub.range(ls_range).borders(3).linestyle = 1
xlsub.range(ls_range).borders(4).linestyle = 1
//将数据写到EXECL
For c = 1 to numcols
For r = 1 to numrows
xlsub.cells[r + 2,c] = dw_data.object.data[r,c]
Next
Next
xlapp.Application.ActiveWorkbook.saved = false
xlApp.DisConnectObject()
Destroy xlapp
方法三:
------------------------------------------------------
用OLE?参考这个吧:
//====================================================================
// Function: gf_savetoexcel()
//--------------------------------------------------------------------
// Arguments:
// value datawindow dw_1
// value string filename
//--------------------------------------------------------------------
// Returns: boolean
//====================================================================
string ls_temp = ""
// 创建一个 datastore 以存贮 dw_1 中各列的信息
datastore ds_col
ds_col = create datastore
ds_col.dataobject = "Dw_SaveToExcel_Columns"
ds_col.SetTransObject(sqlca)
SetPointer(HourGlass!)
// 记录 dw_1 中的对象数目
string l_objects, l_name, l_type, l_band, l_visible, l_objtype
string l_zw, l_objzw
long i, j, pp, l_x
l_objects = dw_1.Describe("DataWindow.Objects")
// 将 DETAIL 区域内的所有可见目标放到 ds_col 中,
// 并按照 object.x 属性大小排序
do while len(l_objects) > 0
pp = pos(l_objects, "~t")
if pp <= 0 then pp = len(l_objects) + 1
l_name = left(l_objects, pp - 1) // Object 名字
l_objects = mid(l_objects, pp + 1, len(l_objects))
l_type = upper(dw_1.Describe(l_name + ".coltype")) // Object 数据类型
if pos(l_type, "(") > 0 then
l_type = left(l_type, pos(l_type, "(") - 1 )
end if
l_x = long(dw_1.Describe(l_name + ".x")) // Object X 坐标
l_band = upper(dw_1.Describe(l_name + ".Band")) // Object 所属区域
l_visible = dw_1.Describe(l_name + ".visible") // Object 是否可见
l_objtype = upper(dw_1.Describe(l_name + ".Type")) // Object 类别
// 如果 object 在 Detail 区,且可见,并且是 column 或 Compute Column
if l_band = "DETAIL" and l_visible = "1" and &
( l_objtype = "COLUMN" or l_objtype = "COMPUTE" ) then
l_objzw = l_name + "_t" // Object 中文标头
l_zw = dw_1.describe(l_objzw + ".text")
// 去掉标头中多行之间的换行符
pp = Pos(l_zw , "~n")
Do While pp > 0
l_zw = Replace(l_zw, pp - 1, 2, "")
pp = Pos(l_zw , "~n")
Loop
// 去掉标头中多行之间的空格
pp = Pos(l_zw , " ")
Do While pp > 0
l_zw = Replace(l_zw, pp , 1, "")
pp = Pos(l_zw , " ")
Loop
// 去掉标头中多行之间的引号
pp = Pos(l_zw , '"')
Do While pp > 0
l_zw = Replace(l_zw, pp , 1, "")
pp = Pos(l_zw , '"')
Loop
i = ds_col.insertrow(0)
ds_col.setitem(i, "colname", l_name )
ds_col.setitem(i, "x", l_x )
ds_col.setitem(i, "coltype", l_type )
ds_col.setitem(i, "coltitle",l_zw )
ds_col.setitem(i, "objtype", l_objtype )
end if
loop
ds_col.sort()
// 将 dw_1 中的内容输出到 ds_tmp 中
datastore ds_tmp
ds_tmp = create datastore
ds_tmp.dataobject = "Dw_SaveToExcel"
ds_tmp.settransobject(sqlca)
string l_value, l_colname
long row
for i = 1 to dw_1.rowcount()
row = ds_tmp.insertrow(0)
for j = 1 to ds_col.rowcount()
l_type = upper(ds_col.object.coltype[j])
l_colname = ds_col.object.colname[j]
l_objtype = ds_col.object.objtype[j]
// 如果 dw_1 中某对象是 column 对象,则利用 Describe("evaluate ...
// 语法取出其当前显示信息。对 Edit、EditMask、DropDownDatawindow、
// DropDownListBox、CodeTable等风格均适用
if l_objtype = "COLUMN" then
l_value = dw_1.Describe("Evaluate('LookupDisplay(" + l_colname + ")'," + string(i) + ")")
else
// 对于 Compute 对象,利用前述方法会出错
choose case l_type
case "CHAR"
l_value = dw_1.GetItemString(i, l_colname )
case "DATE"
l_value = string( dw_1.GetItemDate(i, l_colname) , "yyyy.mm.dd")
case "DATETIME"
l_value = string( dw_1.GetItemDateTime(i, l_colname) , "yyyy.mm.dd")
case "INT","LONG", "NUMBER", "REAL", "UNLONG", "DECIMAL"
l_value = string( dw_1.GetItemNumber(i, l_colname) )
case "TIME", "TIMESTAMP"
l_value = string( dw_1.GetItemTime(i, l_colname) )
end choose
end if
if len(l_value) > 60 then
messagebox("提示","“" + l_value + "”超过了 60 个字符,系统将截短该项!" )
l_value = left(l_value, 60)
end if
l_colname = "A" + trim(string(j))
ds_tmp.setitem(row, l_colname, l_value)
next
next
// 插入表头
if ds_tmp.rowcount() >= 1 then
row = ds_tmp.insertrow(1)
else
row = ds_tmp.insertrow(0)
end if
for j = 1 to ds_col.rowcount()
l_value = ds_col.object.coltitle[j]
l_colname = "A" + trim(string(j))
ds_tmp.setitem(row, l_colname, l_value)
next
ds_tmp.SaveAs(filename, Excel!, false)
messagebox("提示","输出成功!")
destroy ds_tmp
SetPointer(Arrow!)
return true
-----------------------------------------------------------------------------