コード例 #1
0
        private void ImportInterop()
        {
            Excel.Workbook workbook;
            Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
            try
            {
                workbook = app.Workbooks.Open(OpenFile());
            }
            catch (Exception ex)
            {
                return;
            }
            string wk = "[" + Xlsheetname + "$]";

            Excel.Worksheet worksheet = NameXl(workbook, wk);
            var             range     = worksheet.UsedRange;

            try
            {
                int i = 0;
                pBChargerEtu.Visibility = Visibility.Visible;
                pBChargerEtu.Maximum    = range.Rows.Count - 1;
                for (int row = 0; row < range.Rows.Count - 1; row++)
                {
                    pBChargerEtu.Dispatcher.Invoke(() => pBChargerEtu.Value = row, System.Windows.Threading.DispatcherPriority.Background);//rr
                    //Apb.Value = Apb.Value + 1;
                    System.Threading.Thread.Sleep(1);
                    if (range.Cells[row + 1, 1].Value.ToString() != "")
                    {
                        string[] data = new string[range.Columns.Count];
                        for (int col = 0; col <= range.Columns.Count - 1; col++)
                        {
                            if (col != 3)
                            {
                                data[col] = range.Cells[row + 2, col + 1].Value.ToString();;
                                //MessageBox.Show(range.Cells[row+2, col+1].Value.ToString());
                            }
                            else
                            {
                                data[col] = range.Cells[row + 2, col + 1].Value.ToString();
                                //data[col] = DateTime.ParseExact(range.Cells[row + 2, col + 1].Value, "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture).ToString();
                            }
                        }
                        try
                        {
                            String d = DateTime.ParseExact(data[3], "dd/MM/yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None).ToString("yyyy-MM-dd");
                            if (Controleur.Vmodele.InsertEtudiant(data[0], data[1], d, data[2], Hash(GenerateMdp()), data[4], data[5]) == 1)
                            {
                                i++;
                                try
                                {
                                    string mdp = GenerateMdp();
                                    Controleur.Vmodele.ChangePassword(Controleur.Vmodele.ReturnLoginEmailLastId().Rows[0].ItemArray.ElementAt(0).ToString(), Hash(mdp));
                                    Mail.CreateMessage(Controleur.Vmodele.ReturnLoginEmailLastId().Rows[0].ItemArray.ElementAt(1).ToString(), "Réinitialisation de votre mot de passe Chevloc", "Bonjour " + Controleur.Vmodele.ReturnLoginEmailLastId().Rows[0].ItemArray.ElementAt(2).ToString() + ",\n\nVoici votre nouveau mot de passe : " + mdp + "\nVous pourrez le changer sur nore site " + Mail.site + ", rubrique \"profil\".\n\nL'équipe Chevloc");
                                }
                                catch (Exception ex)
                                {
                                    MessageBox.Show(ex.ToString());
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                        }
                    }
                }
                workbook.Close();
                app.Quit();
                KillProcess();
                System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
                MessageBox.Show("Insertion de " + i + " étudiants.", "", MessageBoxButton.OK);
                pBChargerEtu.Visibility = Visibility.Hidden;
            }
            catch (Exception err)
            {
                MessageBox.Show(err.ToString());
            }
        }