在 C# 中通过 OleDbDataAdapter 导入 Excel 并更改列名称

发布于 2024-12-04 11:02:57 字数 963 浏览 0 评论 0原文

你好,我尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伤痕我心 2024-12-11 11:02:57

我尝试过一些代码,它对我来说效果很好。尝试一下:

OleDbConnection oleDbConnection = new OleDbConnection(
  "Provider=Microsoft.Jet.OLEDB.4.0;" +
  "Data Source=D:\\Users\\name\\Desktop\\test.xls;" +
  "Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'");
oleDbConnection.Open();

//Get columns
DataTable dtColumns = oleDbConnection.GetSchema("Columns", new string[] { null, null, "Tabelle1$", null });
List<string> columns = new List<string>();

foreach (DataRow dr in dtColumns.Rows)
  columns.Add(dr[3].ToString());

string colName = columns.Find(item => item.Substring(0,3) == "QTA");

DataSet ds = new DataSet();
OleDbDataAdapter da = new OleDbDataAdapter
  ("SELECT * FROM [Tabelle1$] WHERE [" + colName + "] > 0", oleDbConnection);
da.Fill(ds);
dataGrid1.ItemsSource = ds.Tables[0].DefaultView;
oleDbConnection.Close();

注意根据您的需要更改连接字符串。

您可以使用 LINQ 来修剪代码:

string colName = (from DataRow dr in dtColumns.Rows where dr[3].ToString().Substring(0, 3) == "QTA" select dr[3].ToString()).ElementAt(0);

I've tried some code and it works fine for me. Have a try:

OleDbConnection oleDbConnection = new OleDbConnection(
  "Provider=Microsoft.Jet.OLEDB.4.0;" +
  "Data Source=D:\\Users\\name\\Desktop\\test.xls;" +
  "Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'");
oleDbConnection.Open();

//Get columns
DataTable dtColumns = oleDbConnection.GetSchema("Columns", new string[] { null, null, "Tabelle1$", null });
List<string> columns = new List<string>();

foreach (DataRow dr in dtColumns.Rows)
  columns.Add(dr[3].ToString());

string colName = columns.Find(item => item.Substring(0,3) == "QTA");

DataSet ds = new DataSet();
OleDbDataAdapter da = new OleDbDataAdapter
  ("SELECT * FROM [Tabelle1$] WHERE [" + colName + "] > 0", oleDbConnection);
da.Fill(ds);
dataGrid1.ItemsSource = ds.Tables[0].DefaultView;
oleDbConnection.Close();

Pay attention to changing the connection string to your needs.

You can trim the code, using LINQ:

string colName = (from DataRow dr in dtColumns.Rows where dr[3].ToString().Substring(0, 3) == "QTA" select dr[3].ToString()).ElementAt(0);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文