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
|
private void btn_start_ficExcel_Click(object sender, EventArgs e)
{
// 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));
}
}
// Set Maximum - correspond au nombre de ligne du fichier
mabarre.Maximum = File.ReadAllLines(FileToRead).Length;
// Set the initial value of the ProgressBar.
mabarre.Value = 1;
// Set the Step property - 1 chaque ligne lu
//mabarre.Step = 1;
/*
Connexion à la base mysql
*/
string server = "xxxxxxxxx";
string database = "xxxxxx";
string user = "xxxxxxx";
string password = "xxxxxxxx";
string port = "xxxxxxx";
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;
Worksheet ws = (Worksheet)xla.ActiveSheet;
ws.Cells[1, 1] = "colonne 1";
ws.Cells[1, 2] = "colonne 2";
ws.Cells[1, 3] = "colonne 3";
...............
...............
//je créé les différents entete
UneCellule = ws.get_Range("A1", "M1");
ws.Columns.Font.Name = "MS Sans Serif";
ws.Columns.Font.Size = 12;
string nomcol1 = "";
string nomcol2 = "";
string nomcol3 = "";
........
....... //je créé les différentes variables
/*
Parcours de la base de données et récupération du résultat
*/
for (int a = 2; a < sirens.Count; a++) //
{
marequete=" select t0.tesccpcod, "+
"t0.col1, "+
"t0.col2, " +
"t0.col3, " +
"t0.xxxx, " +
"t0.xxxx, " +
"t1.xxxx," +
"t2.xxxx," +
"t2.xxxx," +
"t2.xxxx," +
"t1.xxxx," +
"t1.xxxxx," +
"t1.xxxx " +
"from matable0 t0 " +
"INNER join matable1 t1 on t1.xxxx = t0.xxx " +
"inner join matable2 t2 on t2.xxxx = t0.xxxx " +
"inner join matable3 t3 on t3.xxxx = t0.xxxx " +
"inner join matable4 t4 on t4.xxxx = t0.xxxx " +
"where t0.num ='" + sirens[a] + "'";
MySqlCommand cmd = new MySqlCommand(marequete, conn);
monreader = cmd.ExecuteReader();
if (monreader.Read())
{
nomcol1 = monreader.GetString(0);
nomcol2 = monreader.GetInt64(1).ToString();
nomcol3 = monreader.GetString(2);
........ //jusque là pas de soucis je recupère mon résultat
//je rempli mon tableau pour rempli le fichier excel
ws.Cells[a, 1] = nomcol1;
ws.Cells[a, 2] = nomcol2;
...............
}
else
{
ws.Cells[a, 2] = sirens[a];//si ko, dans mon fichier excel je ne met que l'identifiant
}
mabarre.PerformStep(); //je rempli ma barre
monreader.Close();
}//fin du for
//monreader.Close();
ws.SaveAs(repexc, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
xla.Quit();
}
}//fin méthode |
Partager