将数据从excel导入datagrid然后导入mysql [英] import data from excel to datagrid and then to mysql

查看:96
本文介绍了将数据从excel导入datagrid然后导入mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将数据从excel导入到datagrid再导入到mysql



I want to import data from excel to datagrid and then to mysql

if ( openFileDialog1.ShowDialog ( ) == DialogResult.OK )
    {
    System.IO.StreamReader sr = new
        System.IO.StreamReader ( openFileDialog1.FileName );
    MessageBox.Show ( sr.ReadToEnd ( ) );
    sr.Close ( );
    }
            
System.Data.OleDb.OleDbConnection MyConnection;

System.Data.DataSet DtSet;
textBox1.Text = openFileDialog1.FileName;
System.Data.OleDb.OleDbDataAdapter MyCommand;

MyConnection = new System.Data.OleDb.OleDbConnection ( 
                   @"provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + 
                   openFileDialog1.FileName + 
                   "';Extended Properties=Excel 8.0;");

MyCommand = new System.Data.OleDb.OleDbDataAdapter ( 
                "select * from [Sheet1$]", MyConnection );

MyCommand.TableMappings.Add ( "Table",                     //error here  
                              "openFileDialog1.FileName");  
DtSet = new System.Data.DataSet();

MyCommand.Fill(DtSet);

praktikanDataGridView.DataSource = DtSet.Tables[0];

MyConnection.Close();



错误是:



An System.Data.dll中发生类型'System.Data.OleDb.OleDbException'的未处理异常

附加信息:外部表格不是预期的格式。


The error was:

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll
Additional information: External table is not in the expected format.

推荐答案

,MyConnection);

MyCommand.TableMappings.Add( // 此处出错
openFileDialog1.FileName);
DtSet = new System.Data.DataSet();

MyCommand.Fill(DtSet);

praktikanDataGridView.DataSource = DtSet.Tables [ 0 ];

MyConnection.Close();
", MyConnection ); MyCommand.TableMappings.Add ( "Table", //error here "openFileDialog1.FileName"); DtSet = new System.Data.DataSet(); MyCommand.Fill(DtSet); praktikanDataGridView.DataSource = DtSet.Tables[0]; MyConnection.Close();



错误是:



未处理System.Data.dll中发生'System.Data.OleDb.OleDbException'类型的异常

附加信息:外部表格不是预期的格式。


The error was:

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll
Additional information: External table is not in the expected format.


参考: C#将excel导入datagrid然后保存到mysql数据库帮助!! [ ^ ]


// ************************** copy_excel_sheet_to_datagridview

void copy_excel_sheet_to_datagridview ( )
    {
    string  sheet_name = "Sheet1";

    if ( !retrieved_excel_filename ( ) )
        {
        return;
        }

    if ( !sheet_name_valid ( sheet_name ) )
        {
        return;
        }

    try
        {
        OleDbConnection connection = null;
        StringBuilder   connection_string = 
                            new StringBuilder ( );
        DataSet         data_set = new DataSet ( );
        string          select_command = String.Empty;

        connection_string.AppendFormat (  
            "Provider=Microsoft.Jet.OLEDB.4.0;" +
            "Data Source='{0}';" + 
            "Extended Properties=Excel 8.0;",
            excel_filename );
        select_command = String.Format ( 
                                    "select * from [{0}


这篇关于将数据从excel导入datagrid然后导入mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆