导入excel文件错误

发布于 2024-10-07 17:26:37 字数 847 浏览 4 评论 0原文

我在 youtube 上找到了一个视频,展示了如何将 Excel 文件导入到 datagridview。我收到错误:找不到可安装的 ISAM。

这是我的代码,我做错了什么?

private void button1_Click(object sender, EventArgs e)
    {
        OleDbConnection conn = new OleDbConnection();
        conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Nick\Desktop\Pricing2.xlsx" + @";Exended Properties=""Excel 8.0;HDR=No;IMEX=1;ImportMixedTypes=Text;TypeGuessRows=0""";

        OleDbCommand command = new OleDbCommand
        (
            "SELECT PartNumber,ShortDescription,RetailPrice,JobberPrice,BasePrice,YourDiscount,YourPrice,LongDescription FROM [Pricing$]",conn
        );
        DataSet ds = new DataSet();
        OleDbDataAdapter adapter = new OleDbDataAdapter(command);
        adapter.Fill(ds);
        dataGridView1.DataSource = ds.Tables[0];
    }

I found a video on youtube that shows how to import an excel file to a datagridview. I'm getting an error: Could not find installable ISAM.

Here is my code, what am I doing wrong?

private void button1_Click(object sender, EventArgs e)
    {
        OleDbConnection conn = new OleDbConnection();
        conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Nick\Desktop\Pricing2.xlsx" + @";Exended Properties=""Excel 8.0;HDR=No;IMEX=1;ImportMixedTypes=Text;TypeGuessRows=0""";

        OleDbCommand command = new OleDbCommand
        (
            "SELECT PartNumber,ShortDescription,RetailPrice,JobberPrice,BasePrice,YourDiscount,YourPrice,LongDescription FROM [Pricing$]",conn
        );
        DataSet ds = new DataSet();
        OleDbDataAdapter adapter = new OleDbDataAdapter(command);
        adapter.Fill(ds);
        dataGridView1.DataSource = ds.Tables[0];
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

星軌x 2024-10-14 17:26:37

您的连接字符串中有一个拼写错误:

Exended Properties=

应该是:

Extended Properties=

另外,我建议您对代码进行轻微改进,其中包括正确处置一次性资源:

using (var conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Nick\Desktop\Pricing2.xlsx;Extended Properties=""Excel 12.0 Xml;HDR=YES"";"))
using (var cmd = conn.CreateCommand())
{
    conn.Open();
    cmd.CommandText = "SELECT PartNumber,ShortDescription,RetailPrice,JobberPrice,BasePrice,YourDiscount,YourPrice,LongDescription FROM [Pricing$]";
    using (var adapter = new OleDbDataAdapter(cmd))
    {
        adapter.Fill(ds);
    }
}

You have a typo in your connection string:

Exended Properties=

Should be:

Extended Properties=

Also I would recommend you a slight improvement of your code which consist in properly disposing disposable resources:

using (var conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Nick\Desktop\Pricing2.xlsx;Extended Properties=""Excel 12.0 Xml;HDR=YES"";"))
using (var cmd = conn.CreateCommand())
{
    conn.Open();
    cmd.CommandText = "SELECT PartNumber,ShortDescription,RetailPrice,JobberPrice,BasePrice,YourDiscount,YourPrice,LongDescription FROM [Pricing$]";
    using (var adapter = new OleDbDataAdapter(cmd))
    {
        adapter.Fill(ds);
    }
}
小矜持 2024-10-14 17:26:37

从我的经验来看。仅 Excel 97-2003 有效。

VB 代码!

Private Sub DodajExcel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DodajExcel.Click
OpenFileDialog2.Filter = "Excel Files (*.xls)|*.xls|All Files (*.*)|*.*"
If OpenFileDialog2.ShowDialog = Windows.Forms.DialogResult.OK Then
    Dim XLSPathx As String = OpenFileDialog2.FileName
End If

Dim connectionStringTemplate As String = _
"Provider=Microsoft.ACE.OLEDB.12.0;" + _
"Data Source={0};" + _
"Extended Properties=""Excel 12.0;HDR=Yes;IMEX=1"""
Dim XLSPath As String = OpenFileDialog2.FileName
Dim connectionString As String = String.Format(connectionStringTemplate, XLSPath)
Dim sqlSelect As String = "SELECT * FROM [Arkusz1$];"
' Load the Excel worksheet into a DataTable
Dim workbook As DataSet = New DataSet()
Dim excelAdapter As System.Data.Common.DataAdapter = New System.Data.OleDb.OleDbDataAdapter(sqlSelect, connectionString)
Try
    excelAdapter.Fill(workbook)
    Dim worksheet As DataTable = workbook.Tables(0)
    DataGridView1.DataSource = worksheet


Catch
   End Try

结束子

From my experience. Only excel 97-2003 works.

CODE IN VB!

Private Sub DodajExcel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DodajExcel.Click
OpenFileDialog2.Filter = "Excel Files (*.xls)|*.xls|All Files (*.*)|*.*"
If OpenFileDialog2.ShowDialog = Windows.Forms.DialogResult.OK Then
    Dim XLSPathx As String = OpenFileDialog2.FileName
End If

Dim connectionStringTemplate As String = _
"Provider=Microsoft.ACE.OLEDB.12.0;" + _
"Data Source={0};" + _
"Extended Properties=""Excel 12.0;HDR=Yes;IMEX=1"""
Dim XLSPath As String = OpenFileDialog2.FileName
Dim connectionString As String = String.Format(connectionStringTemplate, XLSPath)
Dim sqlSelect As String = "SELECT * FROM [Arkusz1$];"
' Load the Excel worksheet into a DataTable
Dim workbook As DataSet = New DataSet()
Dim excelAdapter As System.Data.Common.DataAdapter = New System.Data.OleDb.OleDbDataAdapter(sqlSelect, connectionString)
Try
    excelAdapter.Fill(workbook)
    Dim worksheet As DataTable = workbook.Tables(0)
    DataGridView1.DataSource = worksheet


Catch
   End Try

End Sub

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文