| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2349 人关注过本帖, 1 人收藏
标题:高手帮看下 使用excel 数据慢的问题在哪?
只看楼主 加入收藏
keko3201
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2015-3-31
结帖率:60%
收藏(1)
已结贴  问题点数:10 回复次数:12 
高手帮看下 使用excel 数据慢的问题在哪?
我编了一个学习用的程序,资料来自一个excel文件,内容大致为:序号,问题题目,回答选项,答案共四个类型,有近3000题。在打开程序后,要分别调用excel中的数据来显示在多个label中。但我发现EXCEL如果在FromLoad中打开后,好像只能调用一次数组,我想换题时要使用command1的Click过程,需要重新打开EXCEL才能读入下一组数据,然后还要关闭EXCEL。否则内存会有很多EXCEL在运行。这样,每次读入题目要打开,关闭一次,再读入题目,又要再打开,关闭一次。程序的运行变慢,响应慢。毕竟打开和关闭都要时间的。有什么办法可以改进这个问题呢,我是新手,边查资料边编写的。各位最好能给出方法和注释。

这是我的换题主程序 ,有些语句是抄来的,没用上,所以加引号了

Private Sub Command1_Click()




Command1.Caption = "下一页"
Beep

'Dim S() As String, FileName As String, i As Integer, j As Integer
'Dim xlApp
'Dim xlBook
'Dim xlSheet

FileName = App.Path & "\book1.xlsx" '程序所在目录下的文件,请修改
   Set xlApp = CreateObject("Excel.Application")    '创建EXCEL对象
   xlApp.DisplayAlerts = False    '不显示对话框
   Set xlBook = xlApp.Workbooks.Open(FileName)    '打开已经存在的EXCEL工件簿文件
   xlApp.Visible = False  '设置EXCEL对象不可见
   
   
   'Set xlSheet = xlBook.Worksheets("Sheet1")    '设置活动工作表
   'xlSheet.Cells(1, 1) = ""   '清除单元格内容
   'xlSheet.Cells(1, 1) = 1    '修改单元格内容
   

Set xlSheet = xlBook.Worksheets("sheet1")   '打开表sheet1


Label1.Caption = xlSheet.Cells(Label1.Caption + 5, 2)    'label1读入EXCEL中的某个单元格


'Label2.Caption = xlSheet.Cells(Label1.Caption, 4)        ''label2读入EXCEL中的某个单元格
 
If Label1.Caption <= 850 Then Label2.Caption = "单选" & Label1.Caption & ". " & xlSheet.Cells(Label1.Caption, 4)                                   '单选 单选题号 + .+ 序号 + 文字所在地址

If Label1.Caption > 850 And Label1.Caption <= 1700 Then Label2.Caption = "多选" & Label1.Caption - 850 & ". " & xlSheet.Cells(Label1.Caption, 4)   '多选 多选题号 +. + 序号 + 文字所在地址

If Label1.Caption > 1700 Then Label2.Caption = "判断" & Label1.Caption - 1700 & ". " & xlSheet.Cells(Label1.Caption, 4)                            '判断 判断题号 +. + 序号 + 文字所在地址



Label3.Caption = xlSheet.Cells(Label1.Caption, 5)

Label4.Caption = xlSheet.Cells(Label1.Caption, 6)

'----------------------------------

Label5.Caption = xlSheet.Cells(Label1.Caption + 1, 2)

'Label6.Caption = xlSheet.Cells(Label1.Caption + 1, 4)

If Label5.Caption <= 850 Then Label6.Caption = "单选" & Label5.Caption & ". " & xlSheet.Cells(Label1.Caption + 1, 4)                                 '单选 单选题号 + .+ 序号 + 文字所在地址

If Label5.Caption > 850 And Label5.Caption <= 1700 Then Label6.Caption = "多选" & Label5.Caption - 850 & ". " & xlSheet.Cells(Label1.Caption + 1, 4) '多选 多选题号 +. + 序号 + 文字所在地址

If Label5.Caption > 1700 Then Label6.Caption = "判断" & Label5.Caption - 1700 & ". " & xlSheet.Cells(Label1.Caption + 1, 4)                          '判断 判断题号 +. + 序号 + 文字所在地址


Label7.Caption = xlSheet.Cells(Label1.Caption + 1, 5)

Label8.Caption = xlSheet.Cells(Label1.Caption + 1, 6)

'----------------------------------

Label9.Caption = xlSheet.Cells(Label1.Caption + 2, 2)

'Label10.Caption = xlSheet.Cells(Label1.Caption + 2, 4)

If Label9.Caption <= 850 Then Label10.Caption = "单选" & Label9.Caption & ". " & xlSheet.Cells(Label1.Caption + 2, 4)                                  '单选 单选题号 + .+ 序号 + 文字所在地址

If Label9.Caption > 850 And Label9.Caption <= 1700 Then Label10.Caption = "多选" & Label9.Caption - 850 & ". " & xlSheet.Cells(Label1.Caption + 2, 4)  '多选 多选题号 +. + 序号 + 文字所在地址

If Label9.Caption > 1700 Then Label10.Caption = "判断" & Label9.Caption - 1700 & ". " & xlSheet.Cells(Label1.Caption + 2, 4)                           '判断 判断题号 +. + 序号 + 文字所在地址


Label11.Caption = xlSheet.Cells(Label1.Caption + 2, 5)

Label12.Caption = xlSheet.Cells(Label1.Caption + 2, 6)

'----------------------------------

Label13.Caption = xlSheet.Cells(Label1.Caption + 3, 2)

'Label14.Caption = xlSheet.Cells(Label1.Caption + 3, 4)

If Label13.Caption <= 850 Then Label14.Caption = "单选" & Label13.Caption & ". " & xlSheet.Cells(Label1.Caption + 3, 4)                                   '单选 单选题号 + .+ 序号 + 文字所在地址

If Label13.Caption > 850 And Label13.Caption <= 1700 Then Label14.Caption = "多选" & Label13.Caption - 850 & ". " & xlSheet.Cells(Label1.Caption + 3, 4)  '多选 多选题号 +. + 序号 + 文字所在地址

If Label13.Caption > 1700 Then Label14.Caption = "判断" & Label13.Caption - 1700 & ". " & xlSheet.Cells(Label1.Caption + 3, 4)                            '判断 判断题号 +. + 序号 + 文字所在地址


Label15.Caption = xlSheet.Cells(Label1.Caption + 3, 5)

Label16.Caption = xlSheet.Cells(Label1.Caption + 3, 6)


'----------------------------------

Label17.Caption = xlSheet.Cells(Label1.Caption + 4, 2)

'Label18.Caption = xlSheet.Cells(Label1.Caption + 4, 4)

If Label17.Caption <= 850 Then Label18.Caption = "单选" & Label17.Caption & ". " & xlSheet.Cells(Label1.Caption + 4, 4)                                   '单选 单选题号 + .+ 序号 + 文字所在地址

If Label17.Caption > 850 And Label17.Caption <= 1700 Then Label18.Caption = "多选" & Label17.Caption - 850 & ". " & xlSheet.Cells(Label1.Caption + 4, 4)  '多选 多选题号 +. + 序号 + 文字所在地址

If Label17.Caption > 1700 Then Label18.Caption = "判断" & Label17.Caption - 1700 & ". " & xlSheet.Cells(Label1.Caption + 4, 4)                               '判断 判断题号 +. + 序号 + 文字所在地址


Label19.Caption = xlSheet.Cells(Label1.Caption + 4, 5)

Label20.Caption = xlSheet.Cells(Label1.Caption + 4, 6)


If Label1.Caption >= 6 Then
   Command5.Enabled = True  '当序号为6时,起用“上一页”
Else
  Command5.Enabled = False
End If

If Label1.Caption >= 2695 Then Command1.Enabled = False '当最后一题出现时,关掉  下一页 键


'xlBook.SaveAs FileName:=FileName    '保存工作表,结束时一定别忘了保存
xlBook.Close (False)    '关闭工作簿 这里的false表示退出时不保存修改
xlApp.quit    '结束EXCEL对象
Set xlApp = Nothing    '释放xlApp对象

Command6.Enabled = True    '打开 确定键
Command9.Enabled = True

Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text5.Text = ""


Text1.Visible = True
Text2.Visible = True
Text3.Visible = True
Text4.Visible = True
Text5.Visible = True

Text1.SetFocus

End Sub


再加问一个问题,有没有办法能改写EXCEl 中,一个单元格内的一串字符串中的“|”,并按它在字符串中显示的次序,改成A,B,C,E五个字母。或者直接在VB中改好后显示在label中?
例如:|退回请求|查询|查复|止付申请   改为: A退回请求 B查询 C查复 D止付申请 
例如:|法律|赔偿|刑事|民事       改为: A法律 B赔偿 C刑事 D民事 


[此贴子已经被作者于2016-11-14 13:14编辑过]

搜索更多相关主题的帖子: excel EXCEL 资料 
2016-11-14 12:54
风吹过b
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:364
帖 子:4938
专家分:30047
注 册:2008-10-15
收藏
得分:10 
首先,直接操作 EXCEL ,速度是肯定慢的。
其次,如果 xlApp 定义为全局变量,是可以一次打开,多次读取的。
      每次都不要关,也不要再对 xlApp 进行操作了。程序关闭时再关掉 EXCEL 。

--------
“|”,并按它在字符串中显示的次序,改成A,B,C,E
先使用
fj()=split(源字符串,"|")         '分解成一个字符串数组。
然后再把自个部分连起来。
for i=0 to ubound(fj)
    s=s & chr(65+i) & fj(i)
next i
-----------
注:以上代码,在浏览器写的,如果有错误,请自行修改,只是提供想法。

授人于鱼,不如授人于渔
早已停用QQ了
2016-11-14 13:08
keko3201
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2015-3-31
收藏
得分:0 
感谢版主!fj()是否需要声明
2016-11-14 13:18
ZHRXJR
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:125
帖 子:1034
专家分:5519
注 册:2016-5-10
收藏
得分:0 
直接操作Excel ,速度是肯定快不了,建议你使用ADO对象比直接使用Excel要快得多,而且非常灵活,与数据库操作基本没有什么区别。
如果有你的Excel文件,可以给你做个示例。

[此贴子已经被作者于2016-11-14 13:27编辑过]


请不要选我!!!
2016-11-14 13:26
keko3201
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2015-3-31
收藏
得分:0 
回复 2楼 风吹过b
用了您给的代码,出了点错,不知自怎么改。结果是:ABxxxx cxxxxxx Dxxxxx Exxxxxx,AB显示在第一个位置上了
2016-11-14 13:31
keko3201
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2015-3-31
收藏
得分:0 
搞好了,改好了,知道怎么弄了,谢谢!
2016-11-14 13:43
xss_wl
Rank: 5Rank: 5
等 级:禁止访问
威 望:3
帖 子:100
专家分:315
注 册:2016-10-15
收藏
得分:0 
回复 6楼 keko3201
那就结帖吧
2016-11-14 13:52
keko3201
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2015-3-31
收藏
得分:0 
发现关不掉EXCEL了,关闭程序时,出现“要求对像”,指向下表第一行


xlBook.Close (False)    '关闭工作簿 这里的false表示退出时不保存修改
xlApp.quit    '结束EXCEL对象
Set xlApp = Nothing    '释放xlApp对象

这是为什么?原来这些语句在下一页按钮click是可以用的
2016-11-14 16:38
keko3201
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2015-3-31
收藏
得分:0 
回复 7楼 xss_wl
发现关不掉EXCEL了,关闭程序时,出现“要求对像”,指向下表第一行


xlBook.Close (False)    '关闭工作簿 这里的false表示退出时不保存修改
xlApp.quit    '结束EXCEL对象
Set xlApp = Nothing    '释放xlApp对象

这是为什么?原来这些语句在下一页按钮click是可以用的
2016-11-14 16:41
风吹过b
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:364
帖 子:4938
专家分:30047
注 册:2008-10-15
收藏
得分:0 
这个对象的定义在什么地方。
就是 dim 或 public 定义,在什么地方,要注意作用域。这个是一定要定义的。

授人于鱼,不如授人于渔
早已停用QQ了
2016-11-14 17:31
快速回复:高手帮看下 使用excel 数据慢的问题在哪?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016777 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved