Beispiel #1
0
    private void GenerateRapport()
    {
        string fileName = Server.MapPath(@"~\Downloadss\RapportProjet " + DateTime.Now.ToString("ddMMyyyy") + ".pdf");

        DirectoryInfo dossierDownloadSvr = new DirectoryInfo(Server.MapPath(@"~\Downloadss\"));

        foreach (FileInfo file in dossierDownloadSvr.GetFiles())
        {
            file.Delete();
        }

        //Sources
        //https://www.c-sharpcorner.com/article/create-a-pdf-file-and-download-using-Asp-Net-mvc/
        //https://dotnet.developpez.com/articles/itextsharp/

        Document doc = new Document();

        doc.SetMargins(3f, 3f, 3f, 3f); //Marges du PDF
        PdfWriter.GetInstance(doc, new FileStream(fileName, FileMode.Create, FileAccess.Write));
        doc.Open();

        doc.Add(new Phrase("Rapport par Projet"));


        float[]   test    = { 1, 1, 100000 };
        PdfPTable tableau = new PdfPTable(test);

        tableau.SetWidths(new float[] { 0.1f, 0.8f, 0.2f });

        string          nom      = Request.QueryString["nom"];
        string          idRep    = Request.QueryString["idRep"];
        string          idStat   = Request.QueryString["idStat"];
        string          descript = Request.QueryString["descript"];
        List <T_Projet> Projets  = Utilitaires.GetListeProjetsTri(nom, idRep, idStat, descript);

        Projets = Projets.OrderBy(o => o.nom).ToList();

        //Formatage du tableau

        for (int i = 0; i < Projets.Count; i++)                                          //Lignes
        {
            tableau.AddCell(Projets[i].idProjet.ToString());                             //ID
            tableau.AddCell(Projets[i].nom);                                             //NOM
            tableau.AddCell(BD_CoEco.GetStatusProjetById(Projets[i].idStatus).descript); //STATUS
        }



        doc.Add(tableau); //On ajoute le tableau au document
        doc.Close();

        Response.ContentType = "text/txt";
        string jour  = DateTime.Now.ToString("dd");
        string mois  = DateTime.Now.ToString("MM");
        string annee = DateTime.Now.ToString("yyyy");

        Response.AppendHeader("Content-Disposition", "attachment; filename=Rapport Projet " + jour + "-" + mois + "-" + annee + ".pdf");
        Response.TransmitFile(@"~\Downloadss\RapportProjet " + DateTime.Now.ToString("ddMMyyyy") + ".pdf");
        Response.End();

        if (File.Exists(fileName))
        {
            File.Delete(fileName);
        }
    }