如何实现excel文件与vb中grid之间的复制粘贴

kewen 2003-10-21 11:35:57
如何实现excel文件与vb中grid之间被选择部分的复制粘贴
...全文
251 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
哇,真的很困难耶。
我把excel的内容复制后,打开“剪贴板查看程序”,
发现竟然只有多少行的提示,呵呵。
然后,我把它存为一个文件,哇,竟然少少的6行,竟然变成了600多KB,恐怖呀,呵呵。

到MSDN找一下吧,应该没什么问题:)
abc10 2003-11-14
  • 打赏
  • 举报
回复
先了解string的格式,分离出每个单元格的内容,再依次写入mshflexgrid中,关键是了解格式,然后就好办了。 不过我没试过
kewen 2003-11-14
  • 打赏
  • 举报
回复
谢谢 abc10:
我想从excel复制到mshflexgrid中,问题出在,写入剪贴板的是string类型,我如何将这个str类型的数据写到各个网格里呢?
abc10 2003-11-14
  • 打赏
  • 举报
回复
至于从excel复制到MSHFlexGrid,你可先了解excel格式,再依次写入每个单元格
abc10 2003-11-14
  • 打赏
  • 举报
回复
你是要从grid复制到excel,还是从excel复制到MSHFlexGrid?
我用一个简单的方法实现了从grid复制到excel,其它网格控件类似。


Function ExportExcelclicp(FLex As MSHFlexGrid)
'------------------------------------------------
'功能:将MSHFlexGrid表中内容复制至剪粘板
'变量:
'调用方法: call ExportExcelclicp(MSHFlexGrid1)
' [Scols]................MSHFlexGrid表格的启始列
' [Srows]............... MSHFlexGrid表格的启始行
' [Ecols]................MSHFlexGrid表格的结束列
' [Erows]............... MSHFlexGrid表格的结束行
'------------------------------------------------
Screen.MousePointer = 13
'
Dim Scols, Srows, Ecols, Erows As Integer
With FLex
Scols = .Col
Srows = .Row
Ecols = .ColSel
Erows = .RowSel
If .ColSel > .Col And .RowSel > .Row Then
Scols = .Col
Srows = .Row
Ecols = .ColSel
Erows = .RowSel
ElseIf .ColSel < .Col And .RowSel < .Row Then
Scols = .ColSel
Srows = .RowSel
Ecols = .Col
Erows = .Row
ElseIf .ColSel > .Col And .RowSel < .Row Then
Scols = .Col
Srows = .RowSel
Ecols = .ColSel
Erows = .Row
ElseIf .ColSel < .Col And .RowSel > .Row Then
Scols = .ColSel
Srows = .Row
Ecols = .Col
Erows = .RowSel
End If


End With


Dim I, J As Integer
Dim str As String
Dim Fileopens As Boolean
On Error GoTo err


str = ""
If Srows > 0 Then
For I = Scols To Ecols '复制题头
If I = Scols Then
str = str & FLex.TextMatrix(0, I)
Else
str = str & Chr(9) & FLex.TextMatrix(0, I)
End If
Next
End If

For J = Srows To Erows
str = str & vbCrLf
For I = Scols To Ecols
If I = Scols Then
str = str & FLex.TextMatrix(J, I)
Else
str = str & Chr(9) & FLex.TextMatrix(J, I)
End If
Next
Next
Clipboard.Clear ' 清除剪贴板。
Clipboard.SetText str ' 将正文放置在剪贴板上。
Screen.MousePointer = 0


err:
Select Case err.Number
Case 0
Case Else
Screen.MousePointer = 0
MsgBox err.Description, vbInformation, "出错"
Exit Function
End Select


End Function
kewen 2003-11-14
  • 打赏
  • 举报
回复
可以将excel中的数据复制到剪贴板上,然后再从剪贴板上复制到Grid上,只是我不太清楚剪贴板中的格式,请大家帮忙
kewen 2003-11-06
  • 打赏
  • 举报
回复
我等到花儿也谢了
airon2002 2003-10-25
  • 打赏
  • 举报
回复
不行
cnhxjt 2003-10-23
  • 打赏
  • 举报
回复
帮你UP
suiyuxing 2003-10-23
  • 打赏
  • 举报
回复
我也想知道,
kewen 2003-10-21
  • 打赏
  • 举报
回复
没有人知道吗?慷慨一点吗,100分呢

2,462

社区成员

发帖
与我相关
我的任务
社区描述
VBA(Visual Basic for Applications)是Visual Basic的一种宏语言,是在其桌面应用程序中执行通用的自动化(OLE)任务的编程语言。
社区管理员
  • VBA
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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