通过标准验证导入主数据

Importation of Master data with standard validation

我有 excel 文件按钮(导入)openfiledialog gridview 在 VB.Net 2013 年。 我的任务是制作一个按钮,将所有数据从 excel file 提取到 datagridview

openFileDialog1.InitialDirectory = "C:\Users\ProgrammerPC1\Desktop\DLAV FILES";
openFileDialog1.Title = "Import Master Data";
openFileDialog1.FileName = "";
openFileDialog1.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm";

try { 
    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        string name = "Sheet1";
        string constr = @"provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + openFileDialog1.FileName + "'; Extended Properties=Excel 8.0; HDR=Yes; IMEX=1;";

        System.Data.OleDb.OleDbConnection con = new System.Data.OleDb.OleDbConnection(constr);
        System.Data.OleDb.OleDbCommand oconn = new System.Data.OleDb.OleDbCommand("SELECT * FROM [" + name + "$]", con);
                con.Open();

        System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter(oconn);
        DataTable data = new DataTable();
        adapter.Fill(data);
        dataGridView1.DataSource = data;
    }
    else
    {
        MessageBox.Show("Operation Cancelled");
    }
}catch (Exception err)
    {
        MessageBox.Show(err.Message);
    }

我的错误是 外部 table 不是预期的格式

我发现您对 XLS(Excel 97-2003)和 XLSX(Excel 2007 及更高版本)使用相同的连接字符串提供程序(MS Jet OLEDB 4.0 提供程序)文件,因此在尝试读取 XLSX/XLSM 文件时导致 external table is not in the expected format 错误。

您需要使用 2 个单独的连接字符串,并使用 Path.GetExtension() 方法根据存储在 OpenFileDialog 中的文件扩展名切换它们,如下例所示:

if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
    string extension = Path.GetExtension(openFileDialog1.FileName); // get file extension
    string name = "Sheet1"

    using (System.Data.OleDb.OleDbConnection con = new System.Data.OleDb.OleDbConnection())
    {
        switch (extension)
        {
            case ".xls": // Excel 97-2003 files
               // Excel 97-2003 connection string
               string xlsconstr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + openFileDialog1.FileName + "'; Extended Properties=Excel 8.0; HDR=Yes; IMEX=1;";
               con.ConnectionString = xlsconstr;
               break;

            case ".xlsx": // Excel 2007 files
            case ".xlsm":
               // Excel 2007+ connection string, see in https://www.connectionstrings.com/ace-oledb-12-0/    
               string xlsxconstr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + openFileDialog1.FileName + "'; Extended Properties=Excel 12.0; HDR=Yes; IMEX=1;";
               con.ConnectionString = xlsxconstr;
               break;
        }

        using (System.Data.OleDb.OleDbCommand oconn = new System.Data.OleDb.OleDbCommand("SELECT * FROM [" + name + "$]", con))
        {
            con.Open();

            System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter(oconn);
            DataTable data = new DataTable();
            adapter.Fill(data);
            dataGridView1.DataSource = data;
        }
    }
}
else
{
    MessageBox.Show("Operation Cancelled");
}