关于Select查询生成的临时表使用问题
此前曾请教多表查询统计问题(见https://bbs.bccn.net/thread-399154-3-1.html)得各位朋友特别是S版主的热心指教,问题基本解决。但接下来又有了新问题——
为了将查询统计结果导出为Excel表和打印输出,在表单中添加了2个命令按纽(如下图:)
其中导出按纽的Click事件代码如下:
程序代码:
USE (temp) ALIAS FoxTable IN 0 && 打开所选择的表并定义别名为FoxTable IF NOT USED("FoxTable") =MESSAGEBOX("打开表失败, 程序将中止! ", 16, "Error") RETURN ENDIF cExcelFile = PUTFILE("保存为(&N):",JUSTSTEM(cDbfFile)+".xls","xls") && 激活"另存为?"对话框, 设置默认保存文件名。 IF EMPTY(cExcelFile) CLOSE DATABASES ALL RETURN ENDIF SELECT FoxTable oExcelSheet = GETOBJECT("","Excel.Sheet") && 产生Excel对象 IF NOT TYPE("oExcelSheet") = "O" && 如果oExcelsheet 不是对象型函数 =MESSAGEBOX ("Excel 对象创建失败, 程序将中止! ",16,"Error") RETURN ENDIF oExcelApp = oExcelSheet.Application oExcelApp.Workbooks.Add() && 添加新工作簿 oExcelApp.ActiveWindow.WindowState=2 oSheet = oExcelApp.ActiveSheet nFldCount = AFIELDS(aFldList, "FoxTable") &&把当前表的结构信息存放在一个数组中, 并且返回表的字段数。 FOR i = 1 TO nFldCount oSheet.Cells(1,i).Value = aFldList[i,1] && 将表字段名复制到对应的单元格中 ENDFOR cRecc = STR(RECCOUNT("FoxTable")) && 返回当前表的记录数目 SCAN && 扫描指针当前的位置 WAIT WINDOW ALLTRIM (STR (RECNO ()))+ "/" + cRecc NOWAIT FOR i = 1 TO nFldCount vValue = .NULL. IF AT(aFldList[i,2], "CDLMNFIBYT") = 0 && 如果字段类型不是VFP 的字段类型 LOOP ENDIF cFldName = aFldList[i,1] vValue = EVALUATE(cFldName) && 计算字符表达式的值并返回结果 DO CASE CASE aFldList[i,2] = "C" && 字符/字符串 vValue = TRIM(vValue) && TRIM() 返回删除全部后缀空格后的指定字符表达式 CASE aFldList[i,2] = "D" && 日期 vValue = DTOC(vValue) CASE aFldList[i,2] = "T" && 日期时间 vValue = TTOC(vValue) CASE INLIST(aFldList[i,2], "N", "F", "I", "B", "Y") && 数值 CASE aFldList[i,2] = "L" && 逻辑 CASE aFldList[i,2] = "M" && 备注型 OTHERWISE vValue = .NULL. ENDCASE IF VARTYPE(vValue) = "C" AND EMPTY(vValue) LOOP ENDIF IF NOT ISNULL(vValue) &&判断vValue 结果是否为NULL 值 oSheet.Cells(RECNO("FoxTable")+1,i).Value = vValue && 将值导出到对应单元格中 ENDIF ENDFOR ENDSCAN cChrStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" FOR i = 1 TO nFldCount cColumn = SUBSTR(cChrStr,INT((i- 1)/26),1)+SUBSTR(cChrStr,IIF(MOD(i,26)=0,26,MOD(i,26)),1) oSheet.Columns(cColumn+ ":" + cColumn).ColumnWidth = 12 && 命名工作表列的名称并取得导入了数据的列的宽度 IF aFldList[i,2] = "M" oSheet.Columns(cColumn + ":" + cColumn).WrapText = .F. && 设置备注型型字段列不自动换行 ENDIF ENDFOR oExcelApp.ActiveWorkbook.SaveAs(cExcelFile) && 设置另存为Excel 文件 oExcelApp.ActiveWorkbook.Close(.F.) && 关闭工作簿 oExcelApp.Quit && 退出Excel oExcelSheet = .NULL. oExcelApp = .NULL. && 释放Excel 对象 WAIT CLEAR =MESSAGEBOX("转换完毕! ",64,"OK") CLOSE DATABASES ALL运行后按导出按纽,提示“找不到TEMP变量”。何故?
附:之前查询统计(表单的Init事件)代码:
程序代码:
SELECT Qyjjlxk.jjlxMC 经济类型,CAST(COUNT(distinct Qyzycpk.jgmc) AS N(2)) 企业数,; CAST(COUNT(QYZYCPK.ZYCP) AS N(2)) 产品数,; CAST(SUM(IIF(QYZYCPK.BZDJ=[1],1,0)) AS N(2)) 国标,; CAST(SUM(IIF(QYZYCPK.BZDJ=[2],1,0)) AS N(2)) 行标,; CAST(SUM(IIF(QYZYCPK.BZDJ=[3],1,0)) AS N(2)) 地标,; CAST(SUM(IIF(QYZYCPK.BZDJ=[4],1,0)) AS N(2)) 企标,; CAST(SUM(IIF(QYZYCPK.BZDJ=[5],1,0)) AS N(2)) 协议,; ROUND((CAST(SUM(IIF(QYZYCPK.BZDJ=[1],1,0)) AS N(2))+CAST(SUM(IIF(QYZYCPK.BZDJ=[2],1,0)) AS N(2))+; CAST(SUM(IIF(QYZYCPK.BZDJ=[3],1,0)) AS N(2))+CAST(SUM(IIF(QYZYCPK.BZDJ=[4],1,0)) AS N(2))+; CAST(SUM(IIF(QYZYCPK.BZDJ=[5],1,0)) AS N(2)))/(CAST(COUNT(QYZYCPK.ZYCP) AS N(2)))*100,2) 标准覆盖率 ; FROM QYJBXXB ; INNER JOIN QYJJLXK ; ON QYJBXXB.JJLX=QYJJLXK.JJLX ; INNER JOIN QYZYCPK ON Qyjbxxb.jgdm = Qyzycpk.jgdm GROUP BY Qyjjlxk.jjlxMC INTO CURSOR TEMP READWRITE INSERT INTO TEMP SELECT [合计],SUM(企业数),SUM(产品数),SUM(国标),SUM(行标),SUM(地标),SUM(企标),SUM(协议),; ROUND((SUM(国标)+SUM(行标)+SUM(地标)+SUM(企标)+SUM(协议))/SUM(产品数)*100,2) FROM TEMP GO TOP WITH THISFORM.GRID1 .COLUMNCOUNT=FCOUNT([TEMP]) .RECORDSOURCE=[TEMP] FOR I=1 TO .COLUMNCOUNT WITH .COLUMNS(I) .WIDTH=IIF(I=1,168,IIF(i=thisform.grid1.columncount,100,48)) .HEADER1.CAPTION=FIELD(I) .HEADER1.ALIGNMENT=2 .Resizable=.F. ENDWITH ENDFOR ENDWITH SET DATE YMD thisform.text1.Value=date()
请指教。谢谢!