I have an openFileDialog tool. I will choose a excel file from my computer and my programme read a column (for example A column) and write my listbox on GUI. How can i do it via OleDB? I am new at C#. If you explain detailed, I will be happy for that. Thank you for your help.
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
In order to use the OLEDB provider successfully we have to consider a few points.
The OLEDB provider for Excel 2003 files is different from the one used for Excel 2007/2010 files. So, the first thing we have to do is determining the Excel file format in order to select the correct provider. In the code example below I simply check the extension of the file to determine the Excel file format. Please note, that there are more elaborated methods to determine the Excel file format (e.g. via the magic bytes).
To select all rows of a Excel sheet we need to know the name of the Excel sheet. The standard sheet names are language dependent and could be renamed by the user. So, we need a way to determine the name of the sheets included in a Excel file to be language independent (and of course independent of renamed sheets). Fortunately, the
OleDbConnection
class provides a method calledGetOleDbSchemaTable
which allows us to get all the sheet names in an Excel file.The OLEDB provider for Excel supports an extended property called HDR. Setting
HDR
toYes
means that the first row of a sheet contains the column titles. So, if you use column titles you should setHDR=Yes
.So, to summarize the code sample below does the following (on button click):
mytable
.listbox1
.Code sample: