1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
namespace Suivi_tese
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private FolderBrowserDialog folderBrowserDialog2;
private bool selfichier;
private bool selrepertoire;
private static string repexc = "";
private static MySqlConnection conn;
private static MySqlDataReader monreader = null;
protected Range Cellules;
protected Range UneCellule;
List<string> numcode = new List<string>();
List<string> matadh = new List<string>();
List<string> solmon = new List<string>();
List<string> soldat = new List<string>();
List<string> adhfer = new List<string>();
List<string> raison = new List<string>();
List<string> milib = new List<string>();
List<string> emibas = new List<string>();
List<string> emicoe = new List<string>();
List<string> debdat = new List<string>();
List<string> findat = new List<string>();
List<string> datrec = new List<string>();
public string ReturnReaderValue(MySqlDataReader reader, int indexe)
{
if (!reader.IsDBNull(indexe))
{
return reader.GetString(indexe);
}
else
{
return "";
}
}
private void btn_start_ficExcel_Click_1(object sender, EventArgs e)
{
BasicConfigurator.Configure();
string path = @"C:\Users\.........\test\log.txt";
log.Info("--------------------");
log.Info("Debut de traitement");
log.Info("--------------------");
log.Info("Chemin du fichier pour le log " + path);
if (selrepertoire == true & selfichier == true)
{
log.Info("Selection OK");
int compteur = 0;
// LBLRES.Text = compteur.ToString();
// Set Minimum to 1 represente la 1ere ligne
mabarre.Visible = true;
string FileToRead = txtbox_entree.Text;
log.Info("Fichier dub choisi" + txtbox_entree.Text);
/*
RECUPERATION DES SIRENS
*/
#region Recuperation_sirens
int moncompteur = 0;
String[] lines2 = File.ReadAllLines(FileToRead);
List<String> sirens = new List<String>();
for (Int32 index = 2; index < lines2.Count(); index += 14)
{
if (lines2[index].Substring(0, 5) == "Info1")
{
sirens.Add(lines2[index].Substring(6, 14));
moncompteur++;
}
}
log.Info("Mon compteur :" + moncompteur);
// Set Maximum - correspond au nombre de ligne du fichier
mabarre.Maximum = sirens.Count;
// Set the initial value of the ProgressBar.
mabarre.Value = 1;
#endregion
/*
Connexion à la base mysql
*/
string server = "server";
string database = "mabse";
string user = "monuser";
string password = "monpass";
string port = "2222";
string connString = "Server=" + server + "; Port=" + port + ";Database=" + database + ";Uid=" + user + ";Pwd=" + password + ";SSL Mode=None;default command timeout=0";
string marequete = "";
conn = new MySqlConnection(connString);
try
{
conn.Open();
/*
CREATION ET OUVERTURE DU FICHIER EXCEL
*/
repexc = folderBrowserDialog2.SelectedPath + "\\dossier" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".xlsx";
Microsoft.Office.Interop.Excel.Application xla = new Microsoft.Office.Interop.Excel.Application();
xla.Visible = false;
Workbook wb = xla.Workbooks.Add(XlSheetType.xlWorksheet);
//WorksheetCollection worksheets = xla.Worksheets;
Worksheet ws = (Worksheet)xla.ActiveSheet;
ws.Cells[1, 1] = "colonne 1";
ws.Cells[1, 2] = " colonne 2";
ws.Cells[1, 3] = " colonne 3";
ws.Cells[1, 4] = " colonne 4";
ws.Cells[1, 5] = "colonne 5";
ws.Cells[1, 6] = "colonne 6";
ws.Cells[1, 7] = "colonne 7";
ws.Cells[1, 8] = "colonne 8";
ws.Cells[1, 9] = "colonne 9";
ws.Cells[1, 10] = "colonne 10";
ws.Cells[1, 11] = "colonne 11";
ws.Cells[1, 12] = "colonne 12";
ws.Cells[1, 13] = "colonne 13";
UneCellule = ws.get_Range("A1", "M1");
ws.Columns.Font.Name = "MS Sans Serif";
ws.Columns.Font.Size = 12;
int cptCol = 2;
/*
Parcours de la base de données et récupération du résultat
*/
int compteurAnnexe = 0;
for (int a = 0; a < sirens.Count; a++)
{
marequete = " select t0.tesccpcod, " +
"t0.numero, " +
"t0.identifiant, " +
"t0.xxxxx, " +
"t0.xxxxx, " +
"t0.xxxxx, " +
"t1.xxxxx," +
"t2.xxxxx," +
"t2.xxxxxx," +
"t2.xxxxxx," +
"t1.xxxxxx," +
"t1.xxxxxx," +
"t1.xxxxxx " +
"from tes000pf t0 " +
"left join tes001pf t1 on t1.ccccc = t0.ccccc " +
"left join tes002pf t2 on t2.ccccc = t0.ccccc " +
"left join tes003pf t3 on t3.ccccc = t0.ccccc " +
"left join tes004pf t4 on t4.ccccc = t0.ccccc " +
"where t0.numero ='" + sirens[a] + "'";
log.Info("Ma requete utilisé:" + marequete);
log.Info("sirens :" + sirens[a]);
MySqlCommand cmd = new MySqlCommand(marequete, conn);
monreader = cmd.ExecuteReader();
if (monreader.Read()) //while
{
//recuperation des données
numcode.Add(ReturnReaderValue(monreader, 0)); //code
matadh.Add(ReturnReaderValue(monreader, 1)); //siret
solmon.Add(ReturnReaderValue(monreader, 2)); // matadh
soldat.Add(ReturnReaderValue(monreader, 3));
adhfer.Add(ReturnReaderValue(monreader, 4));
raison.Add(ReturnReaderValue(monreader, 5));
milib.Add(ReturnReaderValue(monreader, 6));
emibas.Add(ReturnReaderValue(monreader, 7));
emicoe.Add(ReturnReaderValue(monreader, 8));
debdat.Add(ReturnReaderValue(monreader, 9));
findat.Add(ReturnReaderValue(monreader, 10));
datrec.Add(ReturnReaderValue(monreader, 11));
//enregistrement dans mon fichier excel
ws.Cells[cptCol, 1] = numcode[compteurAnnexe];
ws.Cells[cptCol, 2] = matadh[compteurAnnexe];
ws.Cells[cptCol, 3] = solmon[compteurAnnexe];
ws.Cells[cptCol, 4] = soldat[compteurAnnexe];
ws.Cells[cptCol, 5] = adhfer[compteurAnnexe];
ws.Cells[cptCol, 6] = raison[compteurAnnexe];
ws.Cells[cptCol, 7] = milib[compteurAnnexe];
ws.Cells[cptCol, 8] = emibas[compteurAnnexe];
ws.Cells[cptCol, 9] = emicoe[compteurAnnexe];
ws.Cells[cptCol, 10] = debdat[compteurAnnexe];
ws.Cells[cptCol, 11] = findat[compteurAnnexe];
ws.Cells[cptCol, 12] = datrec[compteurAnnexe];
compteurAnnexe++;
cptCol++;
mabarre.PerformStep();
}
else
{
ws.Cells[cptCol, 2] = sirens[a];
cptCol++;
}
mabarre.PerformStep();
monreader.Close();
compteur++;
LBLRES.Text = compteur.ToString();
}//fin du for
log.Info("********* FIn de la boucle - sauvegarde du fichier excel ********** " );
ws.SaveAs(repexc, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
xla.Quit();
MessageBox.Show("Fin du traitement", "Message d'information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (MySqlException erreur)
{
log.Info("******** ERREUR DANS LE TRAITEMENT :" + erreur.ToString());
MessageBox.Show(erreur.ToString());
conn.Close();
}
log.Info("----------- FIN DE TRAITEMENT -----------");
}
else
{
log.Info("Fin du traitement du traitement - KO ");
log.Error("AUcun Fichier ou dossier selectoinné");
MessageBox.Show("Merci de choisir un fichier et un répretoire de sortie", "Message d'information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
// monstream.Close();
}
}//fin méthode public parital class
}//fin du namespace |
Partager