1

我有一个项目,我必须将文档中的多个 Excel 工作表中的数据导入 MySQL 数据库。我所看到的教程假设采用一种简单的方法,将每个单元格插入到数据库中的单独列中。我的情况有点不同,因为每个单元格都有不同的数据需要进入数据库中的单独列。一个例子如下所示:

                GST 111             GST 112              GST 114    
                 2                     2                   2


03/ED/SE        54                     52                 53            Bio111
                                                                         54

如上所示,“GST 111”的标题代表课程名称,而“03/ED/SE”的​​值代表注册号。54,52 和 53 的值代表注册号为“03/ED/SE”的​​学生在代码为“GST 111”、“GST 112”和“GST 114”的课程中的分数。当值时事情变得有点棘手

                      "Bi0111"
                        54

到达了。这是因为在这种情况下,该列包含课程代码的值,即“Bio111”和分数,即“54”......这发生在每个学生有结转课程的情况下。挑战在于能够将“Bio111”插入课程代码列和课程分数 54 列,两者都在同一列的同一单元格中。我的代码是这样的:

             protected void insertdata_Click(object sender, EventArgs e)
{
    OleDbConnection oconn = new OleDbConnection
    (@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + 
    Server.MapPath("example.xls") + ";
    Extended Properties=Excel 8.0");//OledbConnection and 
            // connectionstring to connect to the Excel Sheet
    try
    {
        //After connecting to the Excel sheet here we are selecting the data 
   //using select statement from the Excel sheet
   OleDbCommand ocmd = new OleDbCommand("select * from [Sheet1$]", oconn);
            oconn.Open();  //Here [Sheet1$] is the name of the sheet 
            //in the Excel file where the data is present
        OleDbDataReader odr = ocmd.ExecuteReader();
        string fname = "";
        string lname = "";
        string mobnum = "";
        string city = "";
        string state = "";
        string zip = "";
        while (odr.Read())
        {
            fname = valid(odr, 0);//Here we are calling the valid method
            lname = valid(odr, 1);
            mobnum = valid(odr, 2);
            city = valid(odr, 3);
            state = valid(odr, 4);
            zip = valid(odr, 5);
            //Here using this method we are inserting the data into the database
       insertdataintosql(fname, lname, mobnum, city, state, zip);
        }
        oconn.Close();
    }
    catch (DataException ee)
    {
        lblmsg.Text = ee.Message;
        lblmsg.ForeColor = System.Drawing.Color.Red;
    }
    finally
    {
        lblmsg.Text = "Data Inserted Sucessfully";
        lblmsg.ForeColor = System.Drawing.Color.Green;
    }

此代码适用于具有正确结构化数据的单张纸,但我所展示的情况并非如此。(我在某些单元格中有多张纸和多个数据)。我该如何解决这个问题?谢谢。

4

0 回答 0