2020-12-28 00:24:12 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using System.Windows;
|
|
|
|
|
using Hermes.Model;
|
|
|
|
|
using Excel = Microsoft.Office.Interop.Excel;
|
|
|
|
|
|
|
|
|
|
namespace Hermes {
|
|
|
|
|
public partial class ImportWindow : Window {
|
|
|
|
|
private bool importing = false;
|
|
|
|
|
private Excel.Application xlApp;
|
|
|
|
|
private Excel.Workbook xlWb;
|
|
|
|
|
private static Regex communeRgx = new Regex(@"(\d{5})(.*)");
|
|
|
|
|
|
|
|
|
|
private static string prepareValue(object value, bool notNull = false) {
|
|
|
|
|
if(value != null) {
|
|
|
|
|
return value.ToString().Trim();
|
|
|
|
|
}
|
|
|
|
|
if(notNull) {
|
|
|
|
|
return "";
|
|
|
|
|
} else {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ImportWindow(Window parent, string filepath) {
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
Owner = parent;
|
|
|
|
|
List<string> sheets = new List<string>();
|
|
|
|
|
|
|
|
|
|
adresseTextBox.Text = Path.GetFileNameWithoutExtension(filepath);
|
|
|
|
|
|
|
|
|
|
xlApp = new Excel.Application();
|
|
|
|
|
xlWb = xlApp.Workbooks.Open(filepath);
|
|
|
|
|
for(int i = 1; i <= xlWb.Sheets.Count; i++) {
|
|
|
|
|
Excel.Worksheet sheet = xlWb.Sheets.Item[i];
|
|
|
|
|
sheets.Add(sheet.Name);
|
|
|
|
|
}
|
|
|
|
|
sheetComboBox.ItemsSource = sheets;
|
|
|
|
|
|
|
|
|
|
if(sheets.Count == 0) {
|
|
|
|
|
importButton.IsEnabled = false;
|
|
|
|
|
} else {
|
|
|
|
|
sheetComboBox.SelectedIndex = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Importer_Click(object sender, RoutedEventArgs e) {
|
|
|
|
|
importing = true;
|
|
|
|
|
Excel.Worksheet sheet = xlWb.Sheets.Item[sheetComboBox.SelectedIndex + 1];
|
|
|
|
|
string cell = prepareValue(sheet.Cells[1, "J"].Value);
|
|
|
|
|
bool complement = false;
|
|
|
|
|
string rue = adresseTextBox.Text == null ? "" : adresseTextBox.Text;
|
|
|
|
|
if(!string.IsNullOrEmpty(cell)) {
|
|
|
|
|
complement = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for(int i = 2; string.IsNullOrEmpty(prepareValue(sheet.Cells[i, "A"].Value)) != true ; i++) {
|
|
|
|
|
Citoyen citoyen = new Citoyen();
|
|
|
|
|
citoyen.Nom = prepareValue(sheet.Cells[i, "A"].Value);
|
|
|
|
|
citoyen.Prenom = prepareValue(sheet.Cells[i, "B"].Value);
|
|
|
|
|
if(complement) {
|
|
|
|
|
string num = prepareValue(sheet.Cells[i, "D"].Value);
|
|
|
|
|
if(residenceCheckBox.IsChecked == true) {
|
|
|
|
|
citoyen.Adresse = rue;
|
|
|
|
|
citoyen.AdresseNumeroBatiment = num;
|
|
|
|
|
} else {
|
|
|
|
|
citoyen.Adresse = $"{num} {rue}";
|
|
|
|
|
}
|
|
|
|
|
citoyen.Mail = prepareValue(sheet.Cells[i, "E"].Value);
|
|
|
|
|
citoyen.Tel = prepareValue(sheet.Cells[i, "F"].Value);
|
|
|
|
|
citoyen.TelPort = prepareValue(sheet.Cells[i, "G"].Value);
|
|
|
|
|
string res = prepareValue(sheet.Cells[i, "H"].Value, true);
|
2020-12-28 18:56:26 +01:00
|
|
|
|
if(res.ToLower().Replace(" ", "").Equals("r.secondaire")) {
|
2020-12-28 00:24:12 +01:00
|
|
|
|
citoyen.TypeResidence = true;
|
2020-12-28 18:56:26 +01:00
|
|
|
|
} else {
|
|
|
|
|
citoyen.TypeResidence = false;
|
2020-12-28 00:24:12 +01:00
|
|
|
|
}
|
|
|
|
|
if(citoyen.TypeResidence == true) {
|
|
|
|
|
citoyen.AdresseExt = prepareValue(sheet.Cells[i, "I"].Value);
|
|
|
|
|
Match m = communeRgx.Match(prepareValue(sheet.Cells[i, "J"].Value, true));
|
|
|
|
|
if(m.Success) {
|
|
|
|
|
citoyen.AdresseExtCP = m.Groups[1].Value.Trim();
|
|
|
|
|
citoyen.AdresseExtVille = m.Groups[2].Value.Trim();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
string num = prepareValue(sheet.Cells[i, "C"].Value);
|
|
|
|
|
if(residenceCheckBox.IsChecked == true) {
|
|
|
|
|
citoyen.Adresse = rue;
|
|
|
|
|
citoyen.AdresseNumeroBatiment = num;
|
|
|
|
|
} else {
|
|
|
|
|
citoyen.Adresse = $"{num} {rue}";
|
|
|
|
|
}
|
|
|
|
|
citoyen.Mail = prepareValue(sheet.Cells[i, "D"].Value);
|
|
|
|
|
citoyen.Tel = prepareValue(sheet.Cells[i, "E"].Value);
|
|
|
|
|
citoyen.TelPort = prepareValue(sheet.Cells[i, "F"].Value);
|
|
|
|
|
string res = prepareValue(sheet.Cells[i, "G"].Value, true);
|
2020-12-28 18:56:26 +01:00
|
|
|
|
if(res.ToLower().Replace(" ", "").Equals("r.secondaire")) {
|
2020-12-28 00:24:12 +01:00
|
|
|
|
citoyen.TypeResidence = true;
|
2020-12-28 18:56:26 +01:00
|
|
|
|
} else {
|
|
|
|
|
citoyen.TypeResidence = false;
|
2020-12-28 00:24:12 +01:00
|
|
|
|
}
|
|
|
|
|
if(citoyen.TypeResidence == true) {
|
|
|
|
|
citoyen.AdresseExt = prepareValue(sheet.Cells[i, "H"].Value);
|
|
|
|
|
Match m = communeRgx.Match(prepareValue(sheet.Cells[i, "I"].Value, true));
|
|
|
|
|
if(m.Success) {
|
|
|
|
|
citoyen.AdresseExtCP = m.Groups[1].Value.Trim();
|
|
|
|
|
citoyen.AdresseExtVille = m.Groups[2].Value.Trim();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
citoyen.AdresseBatiment = batimentTextBox.Text;
|
|
|
|
|
citoyen.Quartier = quartierTextBox.Text;
|
|
|
|
|
citoyen.DateCreation = DateTime.Now;
|
|
|
|
|
citoyen.DateModification = DateTime.Now;
|
|
|
|
|
ModelContext.Getinstance().CitoyenSet.Add(citoyen);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ModelContext.Getinstance().SaveChanges();
|
|
|
|
|
Marshal.FinalReleaseComObject(sheet);
|
|
|
|
|
importing = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) {
|
|
|
|
|
if(importing) {
|
|
|
|
|
e.Cancel = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
xlWb.Close();
|
|
|
|
|
Marshal.FinalReleaseComObject(xlWb);
|
|
|
|
|
xlApp.Quit();
|
|
|
|
|
Marshal.FinalReleaseComObject(xlApp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|