从Excel文件读取值 [英] Reading values from an Excel File

查看:131
本文介绍了从Excel文件读取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从12个Excel表获得一个值。有没有什么办法,我得到的价值观,而不打开Excel表?
我正在使用vb.net。请张贴一个示例代码,如果有一种方法来读取值而不打开Excel文件。
谢谢

解决方案

根本无法打开Excel文件,无法读取值。但是您可以读取这些值,而无需打开Excel。



如果文件以xml格式保存,将会更容易。如果没有,最简单的方法是仍然使用Excel,但使用Office自动化来做到这一点。困难的方法是创建一个excel文件解析器 - 非常开放的xml excel格式(pre Office 2003) - 很难但仍然可能。



然而,它完全不可能从excel电子表格中读取,而无需打开文件。



这是一段代码,您可以用它来从VB.NET打开一个电子表格,由利用Office Automation(它仍然打开文件,依赖于Excel自动化dll,但不需要打开Excel):



免责声明 / p>

以下代码不打算原样使用,但只是指导读者自己的解决方案的示例,该解决方案应进行彻底测试。 / em>

 '以下代码要求您将Office Interop程序集
'的引用添加到VB.NET中项目(如果你不知道如何做这个搜索Google)

xlApp =新的Excel.ApplicationClass
xlWorkBook = xlApp.Workbooks.Open(<你的E XCEL SPREADSHEET FILE HERE)
xlWorkSheet = xlWorkBook.Worksheets(sheet1)

range = xlWorkSheet.UsedRange

对于rCnt = 1到range.Rows。计数
对于cCnt = 1到range.Columns.Count
Obj = CType(range.Cells(rCnt,cCnt),Excel.Range)
'Obj.value现在包含单元格
下一个
下一个


I want to get a value from 12 excel sheet. is there any way that i get the values without opening the excel sheet? I am using vb.net. Please post an example code, if there is a way to read values without opening the excel file. thanks

解决方案

You can't read the values without opening the Excel file at all. But you may read the values without having to open Excel.

If the file is saved in the xml format it's going to be easier. If not, the easiest method is to still use Excel but use Office Automation to do it. The hard way is to create an excel file parser - quite hard on the non-open xml excel format (pre Office 2003) - hard but still possible.

However, it is quite impossible to read from an excel spreadsheet without opening the file at all..

Here's a snippet of code you could use to open a spreadsheet from VB.NET, by leveraging Office Automation (it still opens the file, an relies on Excel automation dlls, but doesn't require opening Excel):

DISCLAIMER

The following code is not intended to be used as is, but merely it is a sample to guide the reader to their own solution which should be thoroughly tested.

' The code below requires you to add references to Office Interop assemblies
' into your VB.NET project  (if you don't know how to do that search Google)

xlApp = New Excel.ApplicationClass
xlWorkBook = xlApp.Workbooks.Open("<YOUR EXCEL SPREADSHEET FILE HERE")
xlWorkSheet = xlWorkBook.Worksheets("sheet1")

range = xlWorkSheet.UsedRange

For rCnt = 1 To range.Rows.Count
    For cCnt = 1 To range.Columns.Count
        Obj = CType(range.Cells(rCnt, cCnt), Excel.Range)
        ' Obj.value now contains the value in the cell.. 
    Next
Next

这篇关于从Excel文件读取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆