rudhf
Messages postés62Date d'inscriptionmercredi 26 avril 2006StatutMembreDernière intervention30 juin 20072 16 nov. 2006 à 21:00
if (saveFD1.ShowDialog() == DialogResult.OK)
{
monFichier3 = saveFD1.FileName;
try
{
xlApp3 = new Microsoft.Office.Interop.Excel.ApplicationClass();
xlApp3.Visible = false;
//Get a new workbook.
xlClasseur3 = (Microsoft.Office.Interop.Excel._Workbook)(xlApp3.Workbooks.Add(M));
xlFeuil1 = (Microsoft.Office.Interop.Excel._Worksheet)xlClasseur3.ActiveSheet;
// Changer la valeur max de la barre de progression
nbLign = dtRapprochement.Rows.Count;
// Copier les noms des colonnes
int i = 0;
foreach (DataColumn uneColonne in dtRapprochement.Columns)
{
xlRng1 = xlFeuil1.get_Range(Convert.ToChar(65 + i).ToString() + "1", M);
xlFeuil1.Cells[1, i + 1] = uneColonne.ColumnName.Trim();
xlRng1.Interior.ColorIndex = Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic;
xlRng1.Font.Bold = true;
xlRng1.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, M);
xlRng1.EntireColumn.AutoFit();
i++;
}
// Copier toutes les cellules du datagridview
// j=2 on commence à la 2eme ligne dans le fichier excel
int j = 2;
foreach (DataRow uneLigne in dtRapprochement.Rows)
{
i = 1; // si i=1 alors 65-1+1 donne 65<=>A et ainsi on aura la lettre de la colonne puis on juxtapose le numero de la ligne
foreach (DataColumn uneColonne in dtRapprochement.Columns)
{
xlRng1 = xlFeuil1.get_Range(Convert.ToChar(65 + i - 1).ToString() + j.ToString(), M);
xlFeuil1.Cells[j, i] = uneLigne[uneColonne];
xlRng1.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, M);
xlRng1.EntireColumn.AutoFit();
i++;
}
xlFeuil1.Columns.AutoFit();
j++;
}
try
{
xlClasseur3.SaveAs(monFichier3, M, M, M, M, M, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlShared, M, M, M, M, M);
private void SaveAs(string monFichier3)
{
try
{
xlClasseur3.SaveAs(monFichier3, M, M, M, M, M, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlShared, M, M, M, M, M);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}