在 C# 中通过 OleDbDataAdapter 导入 Excel 并更改列名称
你好,我尝试将 Excel 文档导入到 C# 中的 DataGridView 中。 到目前为止它有效,但有一个包含数据的列我需要“排序”。
如果这很简单,我会在 OleDbDataAdapter 查询中执行“WHERE test > 0”。
但是.. 列的名称随着每个文档而变化,我需要经常使用它。到目前为止,我得到了这个:
private void button1_Click(object sender, EventArgs e)
{
String strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=C:\\Users\\Test\\Desktop\\Test.xls;" +
"Extended Properties=Excel 8.0;";
DataSet ds = new DataSet();
OleDbDataAdapter da = new OleDbDataAdapter
("SELECT * FROM [Test$]", strConn);
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0].DefaultView;
}
在选择中,我需要放置一行,表明该列的前 3 个字母相同,但后面的数字不同。喜欢:
QTA 12345, QTA 13213, QTA 92818。
类似于:
OleDbDataAdapter da = new OleDbDataAdapter
("SELECT * FROM [Test$] WHERE [testColumn] > 0", strConn);
但是前 3 个字母相同,最后一个数字是随机的。
有人可以帮我吗?
Hello well i try to import a Excel document into my DataGridView in C#.
So far it worked but there is a Column with Data in it i need to 'sort'.
If this was simple i would do "WHERE test > 0" in the OleDbDataAdapter query.
But.. The name of the Column is changing with each document and i need to use it often. So far i got this :
private void button1_Click(object sender, EventArgs e)
{
String strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=C:\\Users\\Test\\Desktop\\Test.xls;" +
"Extended Properties=Excel 8.0;";
DataSet ds = new DataSet();
OleDbDataAdapter da = new OleDbDataAdapter
("SELECT * FROM [Test$]", strConn);
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0].DefaultView;
}
In the select i need to put a line witch state that the first 3 letters of the column is the same but the number that follow are not. Like:
QTA 12345,
QTA 13213,
QTA 92818.
Something like:
OleDbDataAdapter da = new OleDbDataAdapter
("SELECT * FROM [Test$] WHERE [testColumn] > 0", strConn);
But then with the same first 3 letters and the last numbers who are random.
Can someone help me please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我尝试过一些代码,它对我来说效果很好。尝试一下:
注意根据您的需要更改连接字符串。
您可以使用 LINQ 来修剪代码:
I've tried some code and it works fine for me. Have a try:
Pay attention to changing the connection string to your needs.
You can trim the code, using LINQ: