How can I select specific columns from excel sheet rather than all columns
如何从excel表而不是所有列中选择特定的列
string connectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""", txtPath.Text);
string query = String.Format("select * from [{0}$]", "Sheet1");
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, connectionString);
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet);
dataGridView1.DataSource = dataSet.Tables[0];
3 个解决方案
#1
6
What about:
是什么:
SELECT * FROM [Sheet1$B14:C20]
This should select cells B14
to C20
.
这应该选择单元格B14到C20。
#2
1
This will sound trivial but this is what I understand from your question. Instead of SELECT *
use SELECT [columnName1],[columnName2] FROM Sheet1.
. Here columnName1
and columnName2
should be the headers of columns that you want to get from Excel Sheet
.
这听起来微不足道,但这是我从你的问题中所理解的。而不是选择*使用SELECT [columnName1],[columnName2] FROM Sheet1.在这里,columnName1和columnName2应该是您希望从Excel表中获得的列的标题。
#3
0
If you want to select the data before populating here is a good reference on advanced select statements. If you want to manipulate your data post populating your DataSet then here's how:
如果您想在填充之前选择数据,那么在高级select语句中有很好的参考。如果您想要操作填充数据集的数据post,那么如下方法:
DataTable myTable = dataSet.Tables[0];
var myColumn = myTable.Columns["ColumnName"];
or
或
var myColumn = myTable.Columns[0];
To access a single field it would look something like this.
要访问单个字段,它应该是这样的。
var field = myTable.Rows[0][myColumn];
#1
6
What about:
是什么:
SELECT * FROM [Sheet1$B14:C20]
This should select cells B14
to C20
.
这应该选择单元格B14到C20。
#2
1
This will sound trivial but this is what I understand from your question. Instead of SELECT *
use SELECT [columnName1],[columnName2] FROM Sheet1.
. Here columnName1
and columnName2
should be the headers of columns that you want to get from Excel Sheet
.
这听起来微不足道,但这是我从你的问题中所理解的。而不是选择*使用SELECT [columnName1],[columnName2] FROM Sheet1.在这里,columnName1和columnName2应该是您希望从Excel表中获得的列的标题。
#3
0
If you want to select the data before populating here is a good reference on advanced select statements. If you want to manipulate your data post populating your DataSet then here's how:
如果您想在填充之前选择数据,那么在高级select语句中有很好的参考。如果您想要操作填充数据集的数据post,那么如下方法:
DataTable myTable = dataSet.Tables[0];
var myColumn = myTable.Columns["ColumnName"];
or
或
var myColumn = myTable.Columns[0];
To access a single field it would look something like this.
要访问单个字段,它应该是这样的。
var field = myTable.Rows[0][myColumn];