Excel批量导入Orale

 /// <summary>
        /// 批量插入数据
        /// </summary>
        /// <param name="table"></param>
        public static string ImportData(DataSet dataset)
        {
            string guid = Guid.NewGuid().ToString();
            using (OracleConnection connection = new OracleConnection(ConnectionString))
            {
                string selectstr = @"select hostid,sfzname, rq,
                                        z_iflow, z_iamount, z_oflow, z_oamount,
                                        o_iflow, o_iamount, o_oflow, o_oamount,
                                        t_iflow, t_iamount, t_oflow, t_oamount,
                                        th_iflow, th_iamount, th_oflow, th_oamount,
                                        f_iflow, f_iamount, f_oflow, f_oamount,
                                        fi_iflow, fi_iamount, fi_oflow, fi_oamount,
                                        s_iflow, s_iamount, s_oflow, s_oamount,
                                        m_oflow, m_oamount,
                                        hj_iflow, hj_iamount, hj_oflow, hj_oamount,
                                        b_oflow, b_oamount, w_oflow, w_oamount,
                                        free_iflow, free_iamount, free_oflow, free_oamount,
                                        cg_iflow, cg_iamount, cg_oflow, cg_oamount,
                                        zj_iflow, zj_iamount, zj_oflow, zj_oamount
                                    from sf_temp";
                OracleCommand cmd = new OracleCommand(selectstr, connection);
                OracleDataAdapter da = new OracleDataAdapter(cmd);
                OracleCommandBuilder builder = new OracleCommandBuilder(da);
                da.InsertCommand = builder.GetInsertCommand(true);
                //SetInsertCommand(connection, ref da);
                DataTable ds = new DataTable("sf_temp");
                da.Fill(ds);
                ///Excel数据写入
                ExcelToTable(guid, dataset.Tables[0], ref ds);
                da.Update(ds);
            }
            return guid;
        }

        private static void ExcelToTable(string guid, DataTable ExcelTable, ref DataTable ToTable)
        {
            for (int i = 3; i < ExcelTable.Rows.Count; i++)
            {
                if (string.IsNullOrEmpty(ExcelTable.Rows[i][0].ToString()))
                    break;
                DataRow torow = ToTable.NewRow();
                torow["HostId"] = guid;
                torow["SFZNAME"] = ExcelTable.Rows[i][0];
                torow["RQ"] = ExcelTable.Rows[i][1];

                torow["Z_IFLOW"] = ExcelTable.Rows[i][2];
                torow["Z_IAMOUNT"] = ExcelTable.Rows[i][3];
                torow["Z_OFLOW"] = ExcelTable.Rows[i][4];
                torow["Z_OAMOUNT"] = ExcelTable.Rows[i][5];

                torow["O_IFLOW"] = ExcelTable.Rows[i][6];
                torow["O_IAMOUNT"] = ExcelTable.Rows[i][7];
                torow["O_OFLOW"] = ExcelTable.Rows[i][8];
                torow["O_OAMOUNT"] = ExcelTable.Rows[i][9];

                torow["T_IFLOW"] = ExcelTable.Rows[i][10];
                torow["T_IAMOUNT"] = ExcelTable.Rows[i][11];
                torow["T_OFLOW"] = ExcelTable.Rows[i][12];
                torow["T_OAMOUNT"] = ExcelTable.Rows[i][13];

                torow["TH_IFLOW"] = ExcelTable.Rows[i][14];
                torow["TH_IAMOUNT"] = ExcelTable.Rows[i][15];
                torow["TH_OFLOW"] = ExcelTable.Rows[i][16];
                torow["TH_OAMOUNT"] = ExcelTable.Rows[i][17];

                torow["F_IFLOW"] = ExcelTable.Rows[i][18];
                torow["F_IAMOUNT"] = ExcelTable.Rows[i][19];
                torow["F_OFLOW"] = ExcelTable.Rows[i][20];
                torow["F_OAMOUNT"] = ExcelTable.Rows[i][21];

                torow["FI_IFLOW"] = ExcelTable.Rows[i][22];
                torow["FI_IAMOUNT"] = ExcelTable.Rows[i][23];
                torow["FI_OFLOW"] = ExcelTable.Rows[i][24];
                torow["FI_OAMOUNT"] = ExcelTable.Rows[i][25];

                torow["S_IFLOW"] = ExcelTable.Rows[i][26];
                torow["S_IAMOUNT"] = ExcelTable.Rows[i][27];
                torow["S_OFLOW"] = ExcelTable.Rows[i][28];
                torow["S_OAMOUNT"] = ExcelTable.Rows[i][29];

                torow["M_OFLOW"] = ExcelTable.Rows[i][30];
                torow["M_OAMOUNT"] = ExcelTable.Rows[i][31];

                torow["HJ_IFLOW"] = ExcelTable.Rows[i][32];
                torow["HJ_IAMOUNT"] = ExcelTable.Rows[i][33];
                torow["HJ_OFLOW"] = ExcelTable.Rows[i][34];
                torow["HJ_OAMOUNT"] = ExcelTable.Rows[i][35];

                torow["B_OFLOW"] = ExcelTable.Rows[i][36];
                torow["B_OAMOUNT"] = ExcelTable.Rows[i][37];

                torow["W_OFLOW"] = ExcelTable.Rows[i][38];
                torow["W_OAMOUNT"] = ExcelTable.Rows[i][39];

                torow["FREE_IFLOW"] = ExcelTable.Rows[i][40];
                torow["FREE_IAMOUNT"] = ExcelTable.Rows[i][41];
                torow["FREE_OFLOW"] = ExcelTable.Rows[i][42];
                torow["FREE_OAMOUNT"] = ExcelTable.Rows[i][43];

                torow["CG_IFLOW"] = ExcelTable.Rows[i][44];
                torow["CG_IAMOUNT"] = ExcelTable.Rows[i][45];
                torow["CG_OFLOW"] = ExcelTable.Rows[i][46];
                torow["CG_OAMOUNT"] = ExcelTable.Rows[i][47];

                torow["ZJ_IFLOW"] = ExcelTable.Rows[i][48];
                torow["ZJ_IAMOUNT"] = ExcelTable.Rows[i][49];
                torow["ZJ_OFLOW"] = ExcelTable.Rows[i][50];
                torow["ZJ_OAMOUNT"] = ExcelTable.Rows[i][51];

                ToTable.Rows.Add(torow);
            }

        }

posted @ 2013-02-20 09:06  jsping68  阅读(242)  评论(0编辑  收藏  举报