vb2010读电子表格的问题,现在是每次点button,程序会读入电子表,读出数后再卸载电子表,这样程序太占资源。
vb2010读电子表格的问题,现在是每次点button,程序会载入电子表,读出数后再卸载电子表,这样程序会占大量资源,反应慢,我想的是form启动时就载入电子表,程序运行...
vb2010读电子表格的问题,现在是每次点button,程序会载入电子表,读出数后再卸载电子表,这样程序会占大量资源,反应慢,我想的是form启动时就载入电子表,程序运行中随意用 Label3.Text = xlSheet.Cells(2, 3).Text 读值,关闭form时再卸载电子表,这样怎么实现,谢谢。
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim xlApp As Microsoft.Office.Interop.Excel.Application
Dim xlBook As Microsoft.Office.Interop.Excel.Workbook
Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet
Dim FileName As String
Dim SheetName As String
FileName = "F:\Polo\Vb!\myvb2010\Book1.xlsx"
SheetName = "sheet1"
xlApp = CreateObject("Excel.Application")
xlApp.Visible = False '设置EXCEL对象可见(或不私见)
xlBook = xlApp.Workbooks.Open(FileName) '打开已存在的EXCEL工件簿文件
xlSheet = xlBook.Worksheets(SheetName) '设置活开工作表
Label3.Text = xlSheet.Cells(2, 3).Text '读出值
xlBook.Close(True) '关闭工作簿并保存
xlApp.Quit() '结束EXCEL对象
xlApp = Nothing '释放xlApp对象
End Sub 展开
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim xlApp As Microsoft.Office.Interop.Excel.Application
Dim xlBook As Microsoft.Office.Interop.Excel.Workbook
Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet
Dim FileName As String
Dim SheetName As String
FileName = "F:\Polo\Vb!\myvb2010\Book1.xlsx"
SheetName = "sheet1"
xlApp = CreateObject("Excel.Application")
xlApp.Visible = False '设置EXCEL对象可见(或不私见)
xlBook = xlApp.Workbooks.Open(FileName) '打开已存在的EXCEL工件簿文件
xlSheet = xlBook.Worksheets(SheetName) '设置活开工作表
Label3.Text = xlSheet.Cells(2, 3).Text '读出值
xlBook.Close(True) '关闭工作簿并保存
xlApp.Quit() '结束EXCEL对象
xlApp = Nothing '释放xlApp对象
End Sub 展开
1个回答
展开全部
Dim xlApp As Microsoft.Office.Interop.Excel.Application
Dim xlBook As Microsoft.Office.Interop.Excel.Workbook
Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet
Dim FileName As String
Dim SheetName As String
=======把上面这些变量的声明都放到全局变量里=======
FileName = "F:\Polo\Vb!\myvb2010\Book1.xlsx"
SheetName = "sheet1"
xlApp = CreateObject("Excel.Application")
xlApp.Visible = False '设置EXCEL对象可见(或不私见)
xlBook = xlApp.Workbooks.Open(FileName) '打开已存在的EXCEL工件簿文件
xlSheet = xlBook.Worksheets(SheetName) '设置活开工作表
=======读取这部分放到Form_Shown事件里=======
Label3.Text = xlSheet.Cells(2, 3).Text '读出值
=======调用吧========
xlBook.Close(True) '关闭工作簿并保存
xlApp.Quit() '结束EXCEL对象
xlApp = Nothing '释放xlApp对象
=======卸载资源的事情放到Form_Closing事件里吧=======
Dim xlBook As Microsoft.Office.Interop.Excel.Workbook
Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet
Dim FileName As String
Dim SheetName As String
=======把上面这些变量的声明都放到全局变量里=======
FileName = "F:\Polo\Vb!\myvb2010\Book1.xlsx"
SheetName = "sheet1"
xlApp = CreateObject("Excel.Application")
xlApp.Visible = False '设置EXCEL对象可见(或不私见)
xlBook = xlApp.Workbooks.Open(FileName) '打开已存在的EXCEL工件簿文件
xlSheet = xlBook.Worksheets(SheetName) '设置活开工作表
=======读取这部分放到Form_Shown事件里=======
Label3.Text = xlSheet.Cells(2, 3).Text '读出值
=======调用吧========
xlBook.Close(True) '关闭工作簿并保存
xlApp.Quit() '结束EXCEL对象
xlApp = Nothing '释放xlApp对象
=======卸载资源的事情放到Form_Closing事件里吧=======
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询