代码运行的时候报“字符串太长,超出可容纳范围”

wmawma123 2017-06-15 02:02:45
TEXT TO strABC NOSHOW TEXTMERGE PRETEXT 1+2+4+8
SELECT ALLTRIM(PatName) as 姓名,ALLTRIM(CellPhone) as 电话,ALLTRIM(Demo) as 内容,ALLTRIM(TTOC(InsertDate)) as 日期 FROM DeTemp INTO CURSOR De2Temp
ENDTEXT
&strABC
demo是备注字段,其他都是正常的字段
上面代码运行的时候报“字符串太长,超出可容纳范围”,为什么?谢谢
...全文
597 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
wmawma123 2018-07-04
  • 打赏
  • 举报
回复
引用 4 楼 dkfdtf 的回复:
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, '')


夜猫老师,这段代码确实很好用,但是有个问题,如果有个字段的值是3-8,字符型,粘贴过来就变成了2018年3月8日了,这个问题怎么解决呢?谢谢
wmawma123 2018-07-04
  • 打赏
  • 举报
回复
另外,这个方式导出的xls打开的时候总是报“文件格式和扩展名不匹配”,这个问题怎么解决?谢谢
wmawma123 2017-06-16
  • 打赏
  • 举报
回复
引用 4 楼 dkfdtf 的回复:
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, '')
就觉得这方案好麻烦,直接导出方便好多(想偷懒,我错了~),一般在打印excel模板的时候我才用,因为好控制……anyway,谢谢夜猫老师一直以来都这么耐心的指导~帮我们这些后辈梳理和规范地学到了很多东西!
都市夜猫 2017-06-15
  • 打赏
  • 举报
回复
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, '')
都市夜猫 2017-06-15
  • 打赏
  • 举报
回复
引用 2 楼 wmawma123 的回复:
夜猫老师,我还要导出excel的,备注字段有超过255个字符的,应该怎么处理呢?
vfp 的 export 和 copy 命令导出到 excel 文件时会丢弃掉 memo 字段,我没用它们来导出,我直接用 ole 方式打开 excel,再粘贴要导出的数据,备注字段再逐行赋值 过会儿写段代码贴上来
wmawma123 2017-06-15
  • 打赏
  • 举报
回复
引用 1 楼 dkfdtf 的回复:
vfp 系统规定,字符字段最大长度只能是 254 个字符 如果希望 de2tmp.内容是字符型字段,取数语句只能是 SELECT .... , CAST(ALLTRIM(demo) as V(254)) as 内容 ... 如果允许 de2tmp.内容仍是备注字段,取数语句改为 SELECT .... ,CAST(ALLTRIM(demo), as M) as 内容 ...
夜猫老师,我还要导出excel的,备注字段有超过255个字符的,应该怎么处理呢?
都市夜猫 2017-06-15
  • 打赏
  • 举报
回复
vfp 系统规定,字符字段最大长度只能是 254 个字符 如果希望 de2tmp.内容是字符型字段,取数语句只能是 SELECT .... , CAST(ALLTRIM(demo) as V(254)) as 内容 ... 如果允许 de2tmp.内容仍是备注字段,取数语句改为 SELECT .... ,CAST(ALLTRIM(demo), as M) as 内容 ...

2,723

社区成员

发帖
与我相关
我的任务
社区描述
VFP,是Microsoft公司推出的数据库开发软件,用它来开发数据库,既简单又方便。
社区管理员
  • VFP社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧