Private Sub Command1_Click() '保存单元格内容,不包含固定行固定列 Dim i As Long, j As Long Dim s As String Open "e:\m.txt " For Output As #1 With MSHFlexGrid1 For i = 1 To .Rows - 1 s = " " For j = 1 To .Cols - 1 s = s & vbTab & .TextMatrix(i, j) Next Print #1, s Next End With Close 1 End SubPrivate Sub Command2_Click() '从文件读取内容,恢复到单元格 Dim s As String MSHFlexGrid1.Redraw = False MSHFlexGrid1.Rows = 2 MSHFlexGrid1.Cols = 101 Open "e:\m.txt " For Input As #1 Do While Not EOF(1) Line Input #1, s MSHFlexGrid1.AddItem s, MSHFlexGrid1.Rows - 1 Loop Close 1 MSHFlexGrid1.Rows = MSHFlexGrid1.Rows - 1 MSHFlexGrid1.Redraw = True End Sub
用TextArray这个属性去赋值. GRID.TextArray(Index) 值得注意的是这个属性是把整个GRID当作一维数组来处理的,也就是说,如果你的表格是3行3列,那么INDEX就是从0到5 因此要用它来定位单元格是比较麻烦的,但是用于一次性输入数据还是蛮方便的. 注意,赋值前先把GRID设为不可见,赋值完毕再设为可见 with grid1 .visible=false for i=0 to nnn .TextArray(i)= xxxx next .visible=true end with基本上就是这么回事了 当然,还有更快的,就是使用CLIP属性,将格式化的字符串一次性输入整个GRID. 不过,对字符串作格式化处理又要花点时间.代码量会上去一点.
把我的直接给你粘过来 保存 Private Sub Command46_Click() Dim i As Integer, j As Integer Screen.MousePointer = vbHourglass CommonDialog1.Filter = "All Files(*.*)|*.*|Text Files(*.tou)|*.tou" CommonDialog1.FilterIndex = 2 CommonDialog1.DefaultExt = "tou" CommonDialog1.ShowSaveIf CommonDialog1.FileName <> "" Then If Dir(CommonDialog1.FileName) <> vbNullString Then If MsgBox("原文件存在,是否覆盖?", vbYesNo, "提示框") = vbYes Then Kill CommonDialog1.FileName Else Exit Sub End If End If Open CommonDialog1.FileName For Output As 1 For i = 0 To MSFlexGrid1.Rows - 1 For j = 0 To MSFlexGrid1.Cols - 1 Print #1, MSFlexGrid1.TextMatrix(i, j); IIf(j = MSFlexGrid1.Cols - 1, "", ","); Next If i < MSFlexGrid1.Rows - 1 Then Print #1, Next Close End If Screen.MousePointer = vbDefault End Sub 导入 Private Sub Command45_Click() Dim inputdata As String CommonDialog1.Filter = "All Files(*.*)|*.*|Text Files(*.tou)|*.tou" CommonDialog1.FilterIndex = 2 CommonDialog1.DefaultExt = "tou" CommonDialog1.Action = 1 File = CommonDialog1.FileName If File = "" Then MsgBox "没有选择文件!", vbOKOnly, "文件选择" Exit Sub ElseIf Right$(File, 3) <> "tou" Then MsgBox "请检查文件类型,正确文件后缀名为.tou", 0, "文件选择" Exit SubElse Dim S As String, a, i As Integer, j As Integer Open CommonDialog1.FileName For Input As 1 MSFlexGrid1.Rows = 0 Line Input #1, S a = Split(S, ",") MSFlexGrid1.Cols = UBound(a) + 1 Seek 1, 1 Do While EOF(1) = False Line Input #1, S a = Split(S, ",") MSFlexGrid1.AddItem vbNullString For j = LBound(a) To UBound(a) MSFlexGrid1.TextMatrix(i, j) = a(j) Next i = i + 1 Loop Close End If End Sub
使用SQL 绑定到MSHFlexGrid控件的 DataSource 属性
SQL太强大了,在数据读取、计算、查询方面SQL有绝对的优势!
不过这办法MS只对写入有效,读取就没用罗LZ也可以把你的读写代码简化下贴上来,大家帮你看看是不是有什么其它问题
写:也是采用二重循环,通过textmatrix(i,j)取值,然后叠加到一个变量中统一写入文件。
我知道这样的效率肯定非常低,但是不知道有什么方法效率高一点。原来的数据量比较小,没有感到速度的问题。但是现在数据量越来越大了,通常都是100×1000以上的。因此,效率不高的问题就显得非常严重了,特来求助。引用ADO
使用SQL 绑定到MSHFlexGrid控件的 DataSource 属性
SQL太强大了,在数据读取、计算、查询方面SQL有绝对的优势!
这个也许可以提高效率,测试一下看看。
Private Sub Command1_Click()
'保存单元格内容,不包含固定行固定列
Dim i As Long, j As Long
Dim s As String
Open "e:\m.txt " For Output As #1
With MSHFlexGrid1
For i = 1 To .Rows - 1
s = " "
For j = 1 To .Cols - 1
s = s & vbTab & .TextMatrix(i, j)
Next
Print #1, s
Next
End With
Close 1
End SubPrivate Sub Command2_Click()
'从文件读取内容,恢复到单元格
Dim s As String MSHFlexGrid1.Redraw = False
MSHFlexGrid1.Rows = 2
MSHFlexGrid1.Cols = 101
Open "e:\m.txt " For Input As #1
Do While Not EOF(1)
Line Input #1, s
MSHFlexGrid1.AddItem s, MSHFlexGrid1.Rows - 1
Loop
Close 1
MSHFlexGrid1.Rows = MSHFlexGrid1.Rows - 1
MSHFlexGrid1.Redraw = True
End Sub
GRID.TextArray(Index)
值得注意的是这个属性是把整个GRID当作一维数组来处理的,也就是说,如果你的表格是3行3列,那么INDEX就是从0到5
因此要用它来定位单元格是比较麻烦的,但是用于一次性输入数据还是蛮方便的.
注意,赋值前先把GRID设为不可见,赋值完毕再设为可见
with grid1
.visible=false
for i=0 to nnn
.TextArray(i)= xxxx
next
.visible=true
end with基本上就是这么回事了
当然,还有更快的,就是使用CLIP属性,将格式化的字符串一次性输入整个GRID.
不过,对字符串作格式化处理又要花点时间.代码量会上去一点.
保存
Private Sub Command46_Click()
Dim i As Integer, j As Integer Screen.MousePointer = vbHourglass CommonDialog1.Filter = "All Files(*.*)|*.*|Text Files(*.tou)|*.tou"
CommonDialog1.FilterIndex = 2
CommonDialog1.DefaultExt = "tou"
CommonDialog1.ShowSaveIf CommonDialog1.FileName <> "" Then
If Dir(CommonDialog1.FileName) <> vbNullString Then
If MsgBox("原文件存在,是否覆盖?", vbYesNo, "提示框") = vbYes Then
Kill CommonDialog1.FileName
Else
Exit Sub
End If
End If
Open CommonDialog1.FileName For Output As 1 For i = 0 To MSFlexGrid1.Rows - 1
For j = 0 To MSFlexGrid1.Cols - 1
Print #1, MSFlexGrid1.TextMatrix(i, j); IIf(j = MSFlexGrid1.Cols - 1, "", ",");
Next
If i < MSFlexGrid1.Rows - 1 Then Print #1,
Next
Close
End If
Screen.MousePointer = vbDefault
End Sub
导入
Private Sub Command45_Click()
Dim inputdata As String CommonDialog1.Filter = "All Files(*.*)|*.*|Text Files(*.tou)|*.tou"
CommonDialog1.FilterIndex = 2
CommonDialog1.DefaultExt = "tou"
CommonDialog1.Action = 1
File = CommonDialog1.FileName
If File = "" Then
MsgBox "没有选择文件!", vbOKOnly, "文件选择"
Exit Sub
ElseIf Right$(File, 3) <> "tou" Then
MsgBox "请检查文件类型,正确文件后缀名为.tou", 0, "文件选择"
Exit SubElse Dim S As String, a, i As Integer, j As Integer
Open CommonDialog1.FileName For Input As 1
MSFlexGrid1.Rows = 0
Line Input #1, S
a = Split(S, ",")
MSFlexGrid1.Cols = UBound(a) + 1
Seek 1, 1
Do While EOF(1) = False
Line Input #1, S
a = Split(S, ",")
MSFlexGrid1.AddItem vbNullString
For j = LBound(a) To UBound(a)
MSFlexGrid1.TextMatrix(i, j) = a(j)
Next
i = i + 1
Loop
Close
End If
End Sub
经过检查才知道,两个地方严重影响速度,
1:存储是采用FSO形式,这个速度不知道怎么慢很多,现在改成直接Print,速度提上来了
2:读取时,我设置了列宽,这个也是严重影响速度。AddItem的方式ms速度相差不多(采用计时的方式),数据大小为80×673
原来我也做过这个试验,自己写循环读取数据的速度和用SQL 绑定到MSHFlexGrid控件的 DataSource 属性读取的速度根本没法比!
没办法,现实就是这样!