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 261 262 263 264
| using System;
using System.Reflection;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Configuration;
using System.Text;
using System.Drawing;
using System.Drawing.Printing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text.RegularExpressions;
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
namespace DevisBETA3
{
public partial class ImportECP : Form
{
public string file;
public string result;
public ImportECP()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form ImportECP = this.ActiveMdiChild;
if (ImportECP != null)
{
ImportECP.Close();
}
}
private void button1_Click_1(object sender, EventArgs e)
{
openFileDialog1.FileName = "";
openFileDialog1.ShowDialog();
if (openFileDialog1.FileName != "")
{
file = openFileDialog1.FileName;
Plusieurs_Feuilles_ECP(file);
}
}
#region Plusieurs_Feuilles_ECP
/// <summary>
///Permet d'enregistrer le resultat dans le fichier excel
/// </summary>
public void Plusieurs_Feuilles_ECP(string file)
{
for (int i = 1; i <= 6; i++) // nombre de feuilles dans l'appli excel
{
Lire_Excel_ECP(file, i);
}
MessageBox.Show("Les données du constructeur ECP ont été ajoutées correctement dans la Base " + textBox2.Text + ".", "Information");
}
#endregion
#region Lire_Excel_ECP
/// <summary>
///Permet d'enregistrer le resultat dans le fichier excel
/// </summary>
public void Lire_Excel_ECP(string file, int cmpt_f)//string result, string chemin
{
// string [] result_spit = result.Split('§');
// string colonne_fin="";// nom de la dernière colonne pour l'enregistrement sous excel
object M = System.Reflection.Missing.Value;
Excel.Application oXL;
Excel._Workbook xlClasseur;//Classeur qui regroupe les feuilles
Excel._Worksheet xlFeuill1;//Feuille sur laquelle on travaille
try
{
//Start Excel and get Application object.
oXL = new Excel.Application();//nouvelle appli
oXL.Visible = false;// ne pas affciher excel dans notre appli
oXL.AlertBeforeOverwriting = false;
xlClasseur = (Excel._Workbook)(oXL.Workbooks.Open(file, M, M, M, M, M, M, M, M, M, M, M, M, M, M));
xlFeuill1 = (Excel.Worksheet)xlClasseur.Sheets[cmpt_f];//.ActiveSheet
// //Add table headers going cell by cell.
//xlFeuill1.Cells[1, 9] = "Prix unitaire";
//int lignes = Convert.ToInt32(textBox3.Text);
Excel.Range r = xlFeuill1.get_Range("A8", "E3000"); //+ lignes
//Accéder au contenu des deux celules
object[,] cell = (object[,])r.Cells.Value2;
string categorie = "";
//Lire le contenu d'une des cellules
for (int i = 1; i <= cell.Length / 5; i++)
{
if (Convert.ToString(cell[i, 2]) == "Ref. ECP")
{
categorie = (string)cell[i - 1, 1].ToString();
}
if (cell[i, 2] != null && (string)cell[i, 2] != "" && cell[i, 2].ToString().Length == 9)
{
string marque = (string)cell[i, 1].ToString();
string code = (string)cell[i, 2].ToString();
string refconst;
if (Convert.ToString(cell[i, 3]) == "" || cell[i, 3] == null)
{
refconst = code;
}
else
{
refconst = Convert.ToString(cell[i, 3]);
}
string article = (string)cell[i, 4].ToString();
string prix = (string)cell[i, 5].ToString();
code = code.Remove(0, 3);
code = code.Insert(0, "ECP");
Ajouter_valeur_ECP(code, article, prix, refconst, marque, categorie);
//MessageBox.Show(valeur);
//xlFeuill1.Cells[i + 1, 9] = result;
code = "";
article = "";
prix = "";
marque = "";
refconst = "";
}
}
//MessageBox.Show(result);
//string[] prix = result.Split(';');
//for (int i = 2; i <= cell.Length + 1; i++)
//{
// xlFeuill1.Cells[i, 9] = prix[i - 2];
//}
//xlFeuill1.get_Range("I1", M).Font.Bold = true;
//xlFeuill1.get_Range("I2", "I" + cell.Length + 1).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;
//oXL.Visible = true;
// xlClasseur.Close(false, M, M);// ferme le XLS
// // File.Delete(@"c:\\Feuil1.xls");
//oXL.Quit();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Erreur dans la lecture du fichier Excel");
}
finally
{
System.GC.Collect();
// Récupère tous les process EXCEL
Process[] pProcess;
pProcess = Process.GetProcessesByName("EXCEL");
// Pour chaque Process
foreach (Process p in pProcess)
// S'il n'y a pas de fenêtre ouverte
if (p.MainWindowHandle == IntPtr.Zero)
// Tue le process
p.Kill();
}
}
#endregion
#region Ajouter_valeur_ECP
/// <summary>
///Permet d'ajouter la valeur dans la base
/// </summary>
public void Ajouter_valeur_ECP(string code, string article, string prix, string refconst, string marque, string categorie)
{
try
{
double coef = 1.20;
double prix_ach = Convert.ToDouble(prix);
double prix_vte = coef * prix_ach;
string famille = "";
string sfamille = "";
if (categorie.Contains(">"))
{
string[] cat = categorie.Split('>');
famille = cat[0];
sfamille = cat[2];
if (sfamille.StartsWith(" "))
{
sfamille = sfamille.Remove(0, 1);
}
}
string sqlcon = @"Server=" + textBox1.Text + "; Integrated Security=SSPI;Connection Timeout=10; Database=" + textBox2.Text + ";User ID=" + textBox3.Text + ";password=";
//Trusted_Connection=False;
string sqlver = "SELECT code_art FROM Articles WHERE (code_art = '" + code + "') GROUP BY code_art";
string sqlreq = "INSERT INTO Articles (code_art, refcons_art, desig_art, prixachat_art, marque_art, famille_art, sfamille_art) VALUES ('" + code + "', " + "'" + refconst + "', '" + article.Replace("'", "`") + "', '" + prix_ach.ToString().Replace(",", ".") + "', '" + marque + "', '" + famille + "', '" + sfamille + "')";
string sqlupdate = "UPDATE Articles SET refcons_art = '" + refconst + "', desig_art ='" + article.Replace("'", "`") + "', prixachat_art ='" + prix_ach.ToString().Replace(",", ".") + "', marque_art = ' " + marque + "', famille_art = ' " + famille + "', sfamille_art = ' " + sfamille + "' WHERE (code_art = '" + code + "')";
SqlConnection connection = new SqlConnection(sqlcon);// connexion
try
{
connection.Open();
SqlCommand commandverif = new SqlCommand(sqlver, connection); // commande verif
commandverif.CommandType = CommandType.Text;
SqlDataReader readerverif = commandverif.ExecuteReader(); //reader
readerverif.Read();
if (readerverif.HasRows)
{
readerverif.Close();
SqlCommand command = new SqlCommand(sqlupdate, connection); // commande update
command.ExecuteNonQuery();
//command.Dispose();
}
else
{
readerverif.Close();
SqlCommand command = new SqlCommand(sqlreq, connection); // commande insert
command.ExecuteNonQuery();
//command.Dispose();
}
}
catch (System.Data.SqlClient.SqlException ex)
{
MessageBox.Show(ex.Message + "\r\n\r\nVoir méthode 'Ajouter_valeur(...)'", "Erreur SQL n°1");
}
finally
{
connection.Close();// Fermeture connection
connection.Dispose();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Erreur SQL n°2");
}
}
#endregion
}
} |
Partager