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
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Excel;
using System.Data.SqlClient;
namespace ConsoleApplication1
{
class Program
{
static Microsoft.Office.Interop.Excel.Application excel;
static Workbooks lesWorkbooks;
static Workbook classeur;
static Worksheet feuille;
static Range monRange;
static object M = System.Reflection.Missing.Value;
public enum TypeEncadrement { ToutesCellulesFin, ToutesCellulesMoyen, Aucun, ExtérieurRangeFin, ExtérieurRangeMoyen };
static void Main(string[] args)
{
Console.Write("hello !!!!!");
Console.ReadKey();
GestionExcel();
Console.WriteLine("fin de la mission.....");
Console.ReadKey();
}
private static SqlDataReader GetRead(SqlCommand com)
{
return com.ExecuteReader();
}
public static bool GestionExcel()
{
bool test = false;
try
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=FABRICE-PC;Initial Catalog=LearningCompany;Integrated Security=True;Pooling=False";
excel = new Application();
excel.Visible = true;
Console.WriteLine("ouverture du fichier dans excel.....");
classeur = excel.Workbooks.Open("d:\\Classeur1.xlsx",0);
feuille = classeur.Sheets[1];
try
{
con.Open();
Console.WriteLine("ouverture de la connection avec la BDD");
SqlCommand com = new SqlCommand("SELECT * FROM Client", con);
for (int i = 1; i < GetRead(com).FieldCount; i++)
{
// classeur.Styles.Add("toto");
// classeur.Styles["toto"].Interior.Color =
// feuille.Range["A1", "D1"].Style.Interior.Color = "#FFFF00";
//Ici je voudrait définir la couleur de chaque cellule.....
feuille.Cells[1, i] = GetRead(com).GetName(i);
}
int ligne = 2;
while (GetRead(com).Read())
{
feuille.Cells[ligne, 1] = GetRead(com).GetString(1);
feuille.Cells[ligne, 2] = GetRead(com).GetString(2);
feuille.Cells[ligne, 3] = GetRead(com).GetString(3);
feuille.Cells[ligne, 4] = GetRead(com).GetString(4);
feuille.Cells[ligne, 5] = GetRead(com).GetString(5);
feuille.Cells[ligne, 6] = GetRead(com).GetString(6);
feuille.Cells[ligne, 7] = GetRead(com).GetString(7);
feuille.Cells[ligne, 8] = GetRead(com).GetString(8);
feuille.Cells[ligne, 9] = GetRead(com).GetString(9);
ligne++;
}
}
catch (Exception e)
{
Console.WriteLine("erreur de connection :" + e.Message.ToString());
}
excel.DisplayAlerts = false;
Console.WriteLine("sauvegarde du fichier excel......");
Console.ReadKey();
classeur.Close(true);
Console.WriteLine("fermeture de excel....");
excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
test = true;
}
catch (Exception e)
{
Console.Write("erreur !!!!\n"+e.Message.ToString());
test = false;
}
return test;
}
}
} |
Partager