Bonjour à tous,
je fais comme suis pour générer un fichier Excel
private void Click_Exporter(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel.Application oXL;
Microsoft.Office.Interop.Excel._Workbook oWB;
Microsoft.Office.Interop.Excel._Worksheet oSheet;
Microsoft.Office.Interop.Excel.Range oRng;
object M = System.Reflection.Missing.Value;
int i = 0;
int j = 0;
int k = 0;
try
{
//Start Excel and get Application object.
oXL = new Microsoft.Office.Interop.Excel.Application();
//oXL.Visible = true;
//Get a new workbook.
//oWB = (Microsoft.Office.Interop.Excel._Workbook)(oXL.Workbooks.Add(System.Reflection.Missing.Value));
oWB = oXL.Workbooks.Open(Application.StartupPath + @"/Impressions/EmploiDuTemps.xls", M, M, M, M, M, M, M, M, M, M, M, M, M, M);
oSheet = (Microsoft.Office.Interop.Excel._Worksheet)oWB.ActiveSheet;
oSheet = (Microsoft.Office.Interop.Excel._Worksheet)oWB.Sheets[1];
oSheet.Name = "Feuil1";
oSheet.Activate();
foreach (DataGridViewColumn col in dgvClassesEDT.Columns)
{
if (oSheet.Cells[4, 1] != null)
{
oRng = oSheet.get_Range(Convert.ToChar(65 + i).ToString() + "1", M);
//oSheet.Cells[3, i + 1] = col.Name.Trim();
oRng.Interior.ColorIndex = Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic;
oRng.Font.Bold = true;
//oRng.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, Missing.Value);
//oRng.EntireColumn.AutoFit();
i++;
}
}
oSheet.Cells[1, 4] = string.Concat("PROFESSEUR PRINCIPAL ", this.txtTitre.Text, " ", this.txtNomEns.Text, " ", this.txtPrenomEns.Text);
oSheet.Cells[1, 3] = DateTime.Now.ToLongDateString();
oSheet.Cells[1, 1] = this.txtClasse.Text;
oSheet.Cells[1, 2] = this.txtAnnee.Text;
j = 3;
foreach (DataGridViewRow uneLigne in dgvClassesEDT.Rows)
{
i = 2;
int h = 0;
if (oSheet.Cells[6, 1] != null)
{
oRng = oSheet.get_Range(Convert.ToChar(65 + h).ToString() + j.ToString(), M);
for (k = 0; k < dgvClassesEDT.Rows.Count - 1; k++)
{
oSheet.Cells[k + 3, 1] = k + 1; //col.Name.Trim();
oRng.Interior.ColorIndex = Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic;
oRng.Font.Bold = false;
//oRng.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, Missing.Value);
//oRng.EntireColumn.AutoFit();
//k++;
}
}
foreach (DataGridViewColumn uneColonne in dgvClassesEDT.Columns)
{
if (oSheet.Cells[j, i] != null)
{
oRng = oSheet.get_Range(Convert.ToChar(65 + i - 1).ToString() + j.ToString(), M);
if (uneLigne.Cells[uneColonne.Name].Value != null) oSheet.Cells[j, i] = "'" + uneLigne.Cells[uneColonne.Name].Value.ToString().Trim();
oRng.Font.Bold = false;
//oRng.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, Missing.Value);
//oRng.EntireColumn.AutoFit();
i++;
}
}
oSheet.Columns.AutoFit();
j++;
}
oXL.Visible = true;
//_MaFeuille = (_Worksheet)_MonClasseur.Sheets[NumeroFeuille];
oSheet = (Microsoft.Office.Interop.Excel._Worksheet)oWB.Sheets[2];
oSheet.Name = "EmploiDuTemps";
oSheet.Activate();
//_MaFeuille.Name = NouveauNom;
//_MaFeuille.Activate();
//this.btnExporter.Text = "&Aperçu";
}
catch (FileNotFoundException Lg)
{
MessageBox.Show(Lg.Message, "Erreur");
}
}
Je voudrais savoir, s'il est possible d'imprimer directement ce fichier sans l'afficher et sans le sauvegarder. Merci de me répondre.