2,723
社区成员
发帖
与我相关
我的任务
分享
Local cFile, cAls, cMemoFld, nCol
Local oExcel as Excel.Application
*-------------------
m.cAls = 'De2Temp' && 要导出的表别名
m.cMemoFld = '内容' && 备注字段名
*-------------------
If !Used(m.cAls) or Empty(Field(m.cMemoFld, m.cAls))
*-- 表没打开 or 备注字段名不存在
Return .f.
EndIf
m.cFile = GetFile('xls','文件名(&N):','确定(&O)', 0, '保存 Excel 文件')
Do case
Case Empty(m.cFile)
Return .f.
Case !Empty(Sys(2000, m.cFile))
If 6 != MessageBox('文件已存在,是否改写?', 4+32+256, '')
Return .f.
EndIf
Otherwise
EndCase
Select (m.cAls)
_vfp.DataToClip(,,3) && 复制所有记录到剪贴板, tab 符分隔
Wait window nowait noclear '正在打开 Excel ...'
m.oExcel = NewObject('Excel.Application')
Wait window nowait noclear '正在导出数据到 Excel ...'
With m.oExcel as Excel.Application
.DisplayAlerts = .f. && 不显示警告
.AlertBeforeOverwriting = .f. && 不提示覆盖
If .Workbooks.Count < 1
m.oExcel.Workbooks.Add() && 确保至少有一个工作簿
EndIf
EndWith
With m.oExcel.Workbooks(1).Sheets(1) as Excel.Worksheet
.Cells(1,1).Select()
.Paste() && 粘贴过来
*-- 找到 memo 字段对应的列
m.nCol = .Cells(1,1).EntireRow.Find(m.cMemoFld,,,,,,.f.).Column
Scan all && 逐行替换 memo 列的内容
.Cells(1+Recno(), m.nCol).Value = ;
Alltrim(Evaluate(m.cMemoFld), 0h0d, 0h0a, 0h20)
EndScan
*-- 调整下行高和列宽
.Cells.Columns.AutoFit()
.Cells.Rows.AutoFit()
.SaveAs(m.cFile)
EndWith
m.oExcel.Quit()
Wait clear
MessageBox('数据已导出。', 64, '')