vb.net 使用VB.NET按列读取excel数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7666425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
reading excel data by column using VB.NET
提问by Jeff
I have never read from an excel file in VB.NET before so sorry if anything is off.
我以前从未在 VB.NET 中读取过 excel 文件,如果有任何问题,我很抱歉。
I am downloading an .csv file from a vendors ftp service that has 8 columns ( style # - mfr.item # - description - metal type - availability - center weight - total weight - retail value )
我正在从供应商 ftp 服务下载一个 .csv 文件,该文件有 8 列(样式#-mfr.item#-描述-金属类型-可用性-中心重量-总重量-零售价值)
I am trying to retrieve all data in the rows bellow style # and retail value there are roughly 4,649 rows
我正在尝试检索下面样式中的所有数据,零售价值大约有 4,649 行
I am not sure how to do this.
我不知道该怎么做。
I load the excel file using the microsoft.office.interop.excel:
我使用 microsoft.office.interop.excel 加载 excel 文件:
Dim eApp As excel.Application
Dim eBook As excel.Workbook
Dim eSheet As excel.Worksheet
Dim eCell As excel.Range
eApp = New excel.Application
eBook = eApp.Workbooks.Open(localFile)
eSheet = eBook.Worksheets(1)
eCell = eSheet.UsedRange
Can anyone help me on what I need to do next? I have browsed google for a few hours now and have not gotten anything to work, just return an object name.
任何人都可以帮助我下一步需要做什么吗?我已经浏览谷歌几个小时了,但没有任何工作,只是返回一个对象名称。
Thanks
谢谢
回答by rskar
You are so almost there! Here are two ways.
你快到了!这里有两种方法。
Way #1: Add the following, after the eCell = eSheet.UsedRange
:
方式#1:在 之后添加以下内容eCell = eSheet.UsedRange
:
Dim eCellArray as System.Array = eCell.Value
Then get the values via eCellArray(r,c)
, where r is the row and c is the column (each starting from 1).
然后通过 获取值eCellArray(r,c)
,其中 r 是行,c 是列(每个从 1 开始)。
Way #2: Use this expression to get the value of a cell:
方式#2:使用此表达式获取单元格的值:
CType(eCell(r,c),Excel.Range).Value ' For Option Strict, or just to get IntelliSense to work.
or simply
或者干脆
eCell(r,c).Value
HTH
HTH
回答by Roman
You can, for example, access the cells in a range like this:
例如,您可以像这样访问范围内的单元格:
Dim iRow as Integer = 1
Dim iCol as Integer
Dim rng as Excel.Range = getYourRangeFromSomewhere()
Dim rngCell as Excel.Range
For iCol = 1 To 10
rngCell = rng.Cells(iRow,iCol)
Debug.Print rngCell.Value
Next