Impression d'un document excel

Résolu
cs_warzet Messages postés 99 Date d'inscription jeudi 17 janvier 2008 Statut Membre Dernière intervention 25 juillet 2013 - 11 févr. 2013 à 08:20
Whismeril Messages postés 19028 Date d'inscription mardi 11 mars 2003 Statut Non membre Dernière intervention 24 avril 2024 - 12 févr. 2013 à 09:58
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.

5 réponses

dodo7263 Messages postés 614 Date d'inscription mercredi 10 septembre 2008 Statut Membre Dernière intervention 9 février 2017 6
12 févr. 2013 à 08:42
Salut,

La fonction est simple. Elle te permet d'imprimer dans un fichier PDF (via pdfCreator).

La variable copies est le nombre de copies que tu veux faire,
PrinterName est le nom de l'imprimante
(Arrête moi si je dis des bêtises wishmeril)

Dans ton cas tu devrais avoir après adaptation de cette méthode quelquechose comme :
oWB.PrintOut(M, M, Copies, M, PrinterName, M, Vrai, M);

C'est même la seule ligne importante.

Procède par étape :
Dans un premier temps essaye d'imprimer dans un fichier PDF.
Ensuite adapte cette méthode si tu veux utiliser une "vraie imprimante".

Mais pour utiliser cette fonction (celle ci dessus) tu dois juste l'appeler comme ceci (pas de paramètres en entrée):

imprime();


Après tu peux imaginer une méthode à laquelle tu lui passes le nom du classeur à imprimer du genre :

imprime("Nom du classeur");


Une autre avec le chemin de l'imprimante et le nom du classeur:

imprime("chemin imprimante", "nom du classeur");


etc etc...

@++

SD
3
Whismeril Messages postés 19028 Date d'inscription mardi 11 mars 2003 Statut Non membre Dernière intervention 24 avril 2024 656
11 févr. 2013 à 12:42
Bonjour,

j'ai posté une source qui permet en autre d'imprimer une feuille excel. Dans l'exemple c'est avec pdf creator, mais ça marche avec de "vraie" imprimante.
Il ne me semble pas nécessaire d'enregistrer le fichier et si tu ne veux pas l'afficher il suffit que oXL.Visible = False. Cependant cela risque de poser des soucis le temps que tu debugues, car si tu arrête le programme, excel est actif, invisible et avec accès à ta feuille. Et là il faut jouer du CTL ATL SUPPR


Whismeril
0
cs_warzet Messages postés 99 Date d'inscription jeudi 17 janvier 2008 Statut Membre Dernière intervention 25 juillet 2013 1
12 févr. 2013 à 08:05
Merci Whismeril,
je vais voir le code, je te tiens informé
0
cs_warzet Messages postés 99 Date d'inscription jeudi 17 janvier 2008 Statut Membre Dernière intervention 25 juillet 2013 1
12 févr. 2013 à 08:12
Voici, il se trouve que j'ai déjà connaissance de ce code, mais justement je ne sais comment utiliser ta fonction [code=cs] public void Imprime()
{
object Copies = (object)1;
object PrinterName = (object)"PDFCreator sur Ne00:";
object Vrai = (object)true;

_ApplicationXL.ActivePrinter = PrinterName.ToString();
_MonClasseur.PrintOut(_M, _M, Copies, _M, PrinterName, _M, Vrai, _M);
}
[code=cs]
alors si tu as un tuyau, il serait le bien venu.
Merci
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
Whismeril Messages postés 19028 Date d'inscription mardi 11 mars 2003 Statut Non membre Dernière intervention 24 avril 2024 656
12 févr. 2013 à 09:58
Pas mieux!


Whismeril
0
Rejoignez-nous