1

I created a program in C # to create an Excel file containing the data of a database. As you can see in my code, I indicated the title that I wanted to have and I also specified the destination path for the Excel file that I want to create. Except that it does not work because the Excel file is not created. Can you help me please ? Thanks :)

Here is my Hand:

        public static void Main(string[] args)
        {
         
            if (args.Length > 0)
            {
               

                if (args[0] == "T1" || args[0] == "T2")
                {
                    var dossierDestination = args[1];

                    EnumTraitements t;

                   
                    if (args[0]=="T1")
                    {
                        t = EnumTraitements.ExportNouveauxMessages;
                        
                    }
                    else
                    {
                        t = EnumTraitements.ExportMessagesModifies;
                       
                    }

                  
                    SqlConnection myConnection = null;

                    myConnection = OuvrirBaseReferentiel();

                    switch (t)
                    {
                        case EnumTraitements.ExportNouveauxMessages:
                           
                            List<Messages> listeNouveauxMsg;
                            listeNouveauxMsg = new List<Messages>();
                            SLDocument sl = new SLDocument();


                            List<Messages> listeDesMessagesVersionPrecedente = DonneMoiTousLesMessages(11, 1, "FR", myConnection);
                            List<Messages> listeDesMessagesVersionActuelle = DonneMoiTousLesMessages(12, 1, "FR", myConnection);

                            foreach (Messages msgAct in listeDesMessagesVersionActuelle)
                            {
                              

                                bool estCeQueLeMsgExistaitDansLaVersionPrecedente = listeDesMessagesVersionPrecedente.Exists(m => m.Application == msgAct.Application && m.Langue == msgAct.Langue && m.Chapitre == msgAct.Chapitre && m.NumeroMessage == msgAct.NumeroMessage);


                                if (estCeQueLeMsgExistaitDansLaVersionPrecedente == true)
                                {
                                    listeNouveauxMsg.Add(msgAct);
                                }    
                            }

                            
                            sl = new SLDocument();

                    
                            EcireLigneTitreExcel_NouveauxMessages(sl);

                            
                            int i = 1;
                            foreach (Messages m in listeNouveauxMsg)
                            {
                                CreerLigneExcelNouveauxMsg(m, sl, i);
                            }

                      
                            sl.SaveAs(dossierDestination);


                            break;

                        case EnumTraitements.ExportMessagesModifies:

                           
                            List<Messages> listeDesMessagesVersionPrecedentee = DonneMoiTousLesMessages(11, 1, "FR", myConnection);
                            List<Messages> listeDesMessagesVersionActuellee = DonneMoiTousLesMessages(12, 1, "FR", myConnection);

                            List<Messages> listeMsgModifies;
                            listeMsgModifies = new List<Messages>();

                       
                            foreach (Messages msgAct in listeDesMessagesVersionActuellee)
                            {
                 

                                bool estCeQueLeLibelleAChange = listeDesMessagesVersionPrecedentee.Exists(m => m.Libelle != msgAct.Libelle &&
                                m.Application == msgAct.Application && m.Langue == msgAct.Langue && m.Chapitre == msgAct.Chapitre && m.NumeroMessage == msgAct.NumeroMessage);

                                if (estCeQueLeLibelleAChange == true)
                                {
                                    
                                    Messages msgMessage = listeDesMessagesVersionPrecedentee.Find(m => m.Application == msgAct.Application && m.Langue == msgAct.Langue && m.Chapitre == msgAct.Chapitre && m.NumeroMessage == msgAct.NumeroMessage);

                                    msgAct.LibelleAvant = msgMessage.Libelle;
                                    listeMsgModifies.Add(msgAct);
                                }
                            }

                            SLDocument sl2 = new SLDocument();
                            EcrireLigneTitreExcel_MessagesModifies(sl2);

                            int j = 1;
                            foreach (Messages m in listeMsgModifies)
                            {
                                CreerLigneExcelMsgModifies(m, sl2, j);
                            }

                       
                            string annee = DateTime.Now.Year.ToString("0000");
                            string mois = DateTime.Now.Month.ToString("00");
                            string day = DateTime.Now.Day.ToString("00");
                            string hour = DateTime.Now.Hour.ToString("00");
                            string minute = DateTime.Now.Minute.ToString("00");

                            string testDateHeureCOuranteConvertieEnstring = "EN_NEW_MSG_OM" + annee + mois + day + hour + minute+ ".xlsx";


                            sl2.SaveAs(testDateHeureCOuranteConvertieEnstring);

                            break;
                    }

                    FermerBaseReferentiel(myConnection);

                }

            }
            



            
        }
    }
}
  • 3
    [quite good tutorial on OpenXML](https://www.c-sharpcorner.com/article/creating-excel-file-using-openxml/) . Even though OpenXML is way more verbose than office interop, the former doesn't need installed MS Office suite – Андрей Саяпин Jun 22 '20 at 15:16

1 Answers1

0

You specify a directory instead of a file name in the SaveAs() method. By default with the parameterless method SaveAs() the Excel file is saved as "Book1.xlsx". If you specify a name, specify the name of the file. The file will probably be saved in the current folder you're application is running. It may be possible to specify a more complex path probably: "directory\subdirectory\excel.xlsx".

SLDocument class

Hazrelle
  • 758
  • 5
  • 9