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
|
private void btn_start_ficExcel_Click(object sender, EventArgs e)
{
if (selrepertoire == true & selfichier == true)//ici je fais un test pour obliger l'utilisateeur à choisir une fichier texte en entré
{ //et un repertoire
int compteur = 0;
LBLRES.Text = compteur.ToString(); //j'ai créé un petit compteur pour avoir un visu du traitement
// Set Minimum to 1 represente la 1ere ligne
mabarre.Visible = true;
mabarre.Minimum = 1;
string FileToRead = txtbox_entree.Text;
/*
RECUPERATION DES SIRENS
*/
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) == "DT1DT")
{
sirens.Add(lines2[index].Substring(6, 14));
}
}//je recuperer mes sirens - a la fin du traitement, j'ai un tableau fait que de siren
mabarre.Maximum = sirens.Count; // Set Maximum - correspond au nombre d'elements de mon tableau de siren
// Set the initial value of the ProgressBar.
mabarre.Value = 1;
/*
Connexion à la base mysql
*/
string server = "xxxxxxx";
string database = "xxxxx";
string user = "xxxxxx";
string password = "xxxxxx";
string port = "xxxx";
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();
}
catch (MySqlException erreur)
{
MessageBox.Show(erreur.ToString());
conn.Close();
}
/*
CREATION ET OUVERTURE DU FICHIER EXCEL
*/
DateTime today = (DateTime.Now).AddMonths(-1);
string d = Convert.ToString(today);
string yyyy = d.Substring(6, 4);
string mm = d.Substring(3, 2);
string jour = d.Substring(0, 2);
string heure = d.Substring(11, 2);
string minute = d.Substring(14, 2);
string seconde = d.Substring(17, 2);
repexc = folderBrowserDialog2.SelectedPath + "\\mondossier" + yyyy + mm + jour + "_" + heure + minute + seconde + ".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;
//je créé mes entente du fichier excel
Worksheet ws = (Worksheet)xla.ActiveSheet;
ws.Cells[1, 1] = "code";
ws.Cells[1, 2] = "siren";
ws.Cells[1, 3] = "adh";
..........
.........
ws.Cells[1, 13] = "date";
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
*/
//cas 1
for (int a = 0; a < sirens.Count; a++) //
{
marequete = " select t0.identifiant, " +
"t0.siren, " +
"t0.numentrre, " +
.......
.......
"from xxxx t0 " +
"left join xxxx t1 on t1.xxxxx = t0.xxx " +
"left join xxxx t2 on t2.xxxx = t0.xxxx " +
"left join xxxx t3 on t3.xxxx = t0.xxxxx " +
"left join xxxx t4 on t4.xxxxx = t0.xxxx " +
"where t0.xxxx ='" + sirens[a] + "'";
MySqlCommand cmd = new MySqlCommand(marequete, conn);
monreader = cmd.ExecuteReader();
if (monreader.Read())
{
ws.Cells[cptCol, 1] = ReturnReaderValue(monreader, 0); //code
ws.Cells[cptCol, 2] = ReturnReaderValue(monreader, 1); //siret
ws.Cells[cptCol, 3] = ReturnReaderValue(monreader, 2); //mathadh
ws.Cells[cptCol, 4] = ReturnReaderValue(monreader, 3);
ws.Cells[cptCol, 5] = ReturnReaderValue(monreader, 4);
ws.Cells[cptCol, 6] = ReturnReaderValue(monreader, 5);
ws.Cells[cptCol, 7] = ReturnReaderValue(monreader, 6);
ws.Cells[cptCol,8] = ReturnReaderValue(monreader, 7);
ws.Cells[cptCol, 9] = ReturnReaderValue(monreader, 8);
ws.Cells[cptCol, 10] = ReturnReaderValue(monreader, 9);
ws.Cells[cptCol, 11] = ReturnReaderValue(monreader, 10);
ws.Cells[cptCol, 12] = ReturnReaderValue(monreader, 11);
ws.Cells[cptCol, 13] = ReturnReaderValue(monreader, 12);
cptCol++;
//je rempli mon fichier excel,ligne par ligne cette partie fonctionne bien
}
else
{
ws.Cells[cptCol, 2] = sirens[a];
cptCol++;
}
mabarre.PerformStep(); //j'incremente ma barre
monreader.Close();
//j'incremente mon label, afin d'avoir un visu sur le traitement
compteur++;
LBLRES.Text = compteur.ToString();
}//fin du for
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);
}
else
{
MessageBox.Show("Merci de choisir un fichier et un répretoire de sortie","Message d'information",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
}
} |
Partager