CECI POURRA PEUT ETRE T'AIDER, C UNE CLASSE DE MA CREATION QUI PERMET DE RECOPIER UN DATAGRIDVIEWBOX DANS UNE FEUILLE EXCEL QUEL QUE SOIENT LE NOMBRE DE LIGNES ET DE COLONNES ; CE QUI EST COMPLIQUE C QU'IL Y A 36000 COULEUR DE CASES DE DATAGRID ET SEULEMENT UNE 50AINE DE COULEURS DANS EXCEL, DONC CA RECOPIE LA COMPOSANTE PRINCIPALE DE LA COULEUR;
using
System;
using
System.Collections;
using
System.Text;
using
System.Data.Common;
using
System.Data.OleDb;
using
System.Data.Sql;
using
System.Data.SqlClient;
using
System.Drawing;
using
System.Data;
using
System.Windows.Forms;
using
System.IO;
using
System.ComponentModel;
namespace
OptiInfo{
class
Impression
{
//string OPTI_INFO_A = "c:\\Program Files\\Opti Info Gestion\\OPTI_INFO_A.txt";
private
ConfigForm
CF =
new
ConfigForm
();
public
string
CheminDocs(){
//string Chemin = "";
//try
//{
// using (StreamReader sr = new StreamReader(OPTI_INFO_A))
// {
// string line;
// while ((line = sr.ReadLine()) != null)
// {
// Chemin = line.ToString();
// }
// }
//}
//catch { }
return
CF.getCheminDOCS();}
private
string
LettreFromIndice(
int
Indice){
string
Colonne=
""
;
int
j = Indice;
if
(j == 0)Colonne =
"A"
;
else
if
(j == 1)Colonne =
"B"
;
else
if
(j == 2)Colonne =
"C"
;
else
if
(j == 3)Colonne =
"D"
;
else
if
(j == 4)Colonne =
"E"
;
else
if
(j == 5)Colonne =
"F"
;
else
if
(j == 6)Colonne =
"G"
;
else
if
(j == 7)Colonne =
"H"
;
else
if
(j == 8)Colonne =
"I"
;
else
if
(j == 9)Colonne =
"J"
;
else
if
(j == 10)Colonne =
"K"
;
else
if
(j == 11)Colonne =
"L"
;
else
if
(j == 12)Colonne =
"M"
;
else
if
(j == 13)Colonne =
"N"
;
else
if
(j == 14)Colonne =
"O"
;
else
if
(j == 15)Colonne =
"P"
;
else
if
(j == 16)Colonne =
"Q"
;
else
if
(j == 17)Colonne =
"R"
;
else
if
(j == 18)Colonne =
"S"
;
else
if
(j == 19)Colonne =
"T"
;
else
if
(j == 20)Colonne =
"U"
;
else
if
(j == 21)Colonne =
"V"
;
else
if
(j == 22)Colonne =
"W"
;
else
if
(j == 23)Colonne =
"X"
;
else
if
(j == 24)Colonne =
"Y"
;
else
if
(j == 25)Colonne =
"Z"
;
return
Colonne.ToString();}
public
Impression(
string
Titre,
DataGridView
Data){
try
{
//UseWaitCursor = true;
object
Missing = System.Reflection.
Missing
.Value;
String
fileName = CheminDocs() +
"Vierge.xls"
;Microsoft.Office.Interop.Excel.
_Application
xlApp;Microsoft.Office.Interop.Excel.
_Workbook
xlClasseur;Microsoft.Office.Interop.Excel.
_Worksheet
xlFeuill;xlApp =
new
Microsoft.Office.Interop.Excel.
Application
();xlApp.Visible =
true
;xlClasseur = xlApp.Workbooks.Open(fileName,
Missing, Missing, Missing, Missing,
Missing, Missing, Missing, Missing,
Missing, Missing, Missing, Missing,
Missing, Missing);
Microsoft.Office.Interop.Excel.
Sheets
xlFeuilles = xlClasseur.Sheets;xlFeuill = (Microsoft.Office.Interop.Excel.
_Worksheet
)xlFeuilles[
"A"
];Microsoft.Office.Interop.Excel.
Range
r;r = xlFeuill.get_Range(
"A1"
, Missing);r.set_Value(Microsoft.Office.Interop.Excel.
XlRangeValueDataType
.xlRangeValueDefault, Titre.ToString());r = xlFeuill.get_Range(
"A2"
, Missing); r.set_Value(Microsoft.Office.Interop.Excel.
XlRangeValueDataType
.xlRangeValueDefault,
"Imprim, le "
+
DateTime
.Now.ToString().Substring(0,10));
int
ligne=6;
int
Cl=0;
string
champ=
""
;
//.Affichage des noms de colonne
foreach
(
DataGridViewColumn
clm
in
Data.Columns){
if
(clm.Visible){
champ = LettreFromIndice(Cl) +
"4"
;r = xlFeuill.get_Range(champ, Missing);
r.set_Value(Microsoft.Office.Interop.Excel.
XlRangeValueDataType
.xlRangeValueDefault, clm.HeaderText.ToString());Cl++;
}
}
//.Remplissage des lignes
foreach
(
DataGridViewRow
dr
in
Data.Rows){
try
{
for
(
int
j = 0; j < Data.ColumnCount; j++){
champ = LettreFromIndice(j) + ligne.ToString();
r = xlFeuill.get_Range(champ, Missing);
try
{
r.set_Value(Microsoft.Office.Interop.Excel.
XlRangeValueDataType
.xlRangeValueDefault, dr.Cells[j].Value.ToString());
//MessageBox.Show(dr.Cells[j].Style.BackColor.ToArgb().ToString());
r.Interior.ColorIndex =
this
.getCouleurExcel(System.
Convert
.ToInt32(dr.Cells[j].Style.BackColor.R),System.
Convert
.ToInt32(dr.Cells[j].Style.BackColor.G),System.
Convert
.ToInt32(dr.Cells[j].Style.BackColor.B));
//r.Font.FontStyle = Color.FromName(dr.Cells[j].Style.Font.Style.ToString());
}
catch
{
if
(System.
Convert
.ToBoolean(dr.Cells[j]))r.set_Value(Microsoft.Office.Interop.Excel.
XlRangeValueDataType
.xlRangeValueDefault,
"Oui"
);
else
r.set_Value(Microsoft.Office.Interop.Excel.
XlRangeValueDataType
.xlRangeValueDefault,
"Non"
); }
}
}
catch
{ }ligne++;
}
//xlFeuill.Columns.Width
//UseWaitCursor = false;
}
catch
{
MessageBox
.Show(
"Impossible d'ouvrir le document Excel type : Vierge.xls"
); }}
public
int
getCouleurExcel(
int
R,
int
G,
int
B){
//MessageBox.Show(R.ToString()+"/"+G.ToString()+"/"+B.ToString());
if
(R == 0 && G == 0 && B == 0)
return
2;
else
if
(R > G && R > B)
return
3;
else
if
(G > B)
return
4;
else
return
5;}
}
}
M
M
N