Bonjour les experts,

Permettez moi d'expliquer mon problem , j'ai fait des fonctions qui permettent de crée un rapport en format Excel (.xls) en ASP.NET MVC qui permet de saisir certaines DATA de la base de donner .

Toutes les data se saisie correctement sauf les dates ( champs vide dans les rapports)

J'aimerais savoir en avantage pourquoi et qu'es ce que je peux faire pour afficher les dates inscrit dans la BD dans mon rapport ...

C'est ici ou je crois avoir fait une erreur....dans la generation du rapport

Mercie en avance pour tout le monde!
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
  public ActionResult GenererRapport(int? cmbClient, int? cmbProjet, int? cmbProbill)
        {
            string fichier;
            //les clients concernes
            var cl = (from a in db.Clients where a.ID_Client == cmbClient select new { Nom = a.Nom, Prenom = a.Prenom }).ToArray();
 
            //les projets concernes
            var pr = (from a in db.Projects where a.ID_Projet == cmbProjet select new { Nom = a.NomProjet, Adresa = a.Ville + "," + a.NoCivique + " " + a.NomRue }).ToArray();
            //les factures idprobill >=0
      var list1 = (from a in db.DetailFDTs
                         where a.Facture == cmbProbill && a.ID_Projet == cmbProjet
                            orderby a.Facture,a.ID_Statut ascending
                            join b in db.VracFournisseurs on a.IdVrac equals b.ID
                            join c in db.Vracs on b.IdVrac equals c.IdVrac
                            join d in db.Fournisseurs on a.ID_Fournisseur equals d.ID_Fournisseur
                            join e in db.Equipements on a.ID_Equipement equals e.ID_Equipement
                            join f in db.FDTs on a.IDFDT  equals f.IDFDT
                            join em in db.Employees on f.ID_Employee  equals em.ID_Employee
                            select new { a.Facture, a.IDFDT, a.Description, a.Duree, c.Materiau, d.NomLegal, e.NomEquipement, a.No_Voyage, a.No_Tonnes,a.ID_Projet_out, a.Doc_in,a.Date,a.Prix_UM,em.Nom,em.Prenom }).ToArray();
 
                //les factures idprobill <0
            var list2 = (from a in db.DetailFDTs
                            where a.ID_Projet==cmbProjet
                            orderby a.Facture,a.ID_Statut ascending
                            join b in db.VracFournisseurs on a.IdVrac equals b.ID
                            join c in db.Vracs on b.IdVrac equals c.IdVrac
                            join d in db.Fournisseurs on a.ID_Fournisseur equals d.ID_Fournisseur
                            join e in db.Equipements on a.ID_Equipement equals e.ID_Equipement
                            join f in db.FDTs on a.IDFDT  equals f.IDFDT
                            join em in db.Employees on f.ID_Employee  equals em.ID_Employee
                            select new { a.Facture, a.IDFDT, a.Description, a.Duree, c.Materiau, d.NomLegal, e.NomEquipement, a.No_Voyage, a.No_Tonnes, a.ID_Projet_out, a.Doc_in, a.Date,a.Prix_UM, em.Nom, em.Prenom }).ToArray();
 
 
            fichier = Server.MapPath(@"\Content\Rapport\rapport.xls");
            #region on vérifie si le fichier existe bien;
            //Si le fichier n'existe pas, erreur
            FileInfo fi = new FileInfo(fichier);
Et voici le code de la page pour vous permettre a mieux comprendre et s'orienter

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ESA.Models;
using System.IO;
using System.Text;
 
using NPOI.HSSF.UserModel;
using OfficeOpenXml;
 
using System.Globalization;
 
namespace ESA.Controllers
{
    public class RapportsController : Controller
    {
        private ESADBContext db = new ESADBContext();
        //
        // GET: /Rapports/
 
       public ActionResult Index()
       {
             return View();
       }
 
 
        public PartialViewResult faitTonChoix()
        {
            List<Object> options = new List<Object>();
            options.Add(new{ID="1",option="Client"});
           // options.Add(new { ID = "2", option = "Projet" });
           // options.Add(new { ID = "3", option = "Probill" });
            ViewBag.options =new SelectList(options,"ID","option");
            return PartialView();
        }
        [HttpPost]
        public JsonResult takeClient()
        {
            var l_client = (from a in db.Clients orderby a.NomCompagnie select new { ID = a.ID_Client, Nom = a.NomCompagnie }).ToArray();
            return Json(l_client);
        }
 
        [HttpPost]
        public JsonResult takeProjet(int searchedValue)
        {
             var l_proj = (from a in db.Projects orderby a.NomProjet where a.ID_Client == searchedValue select new { ID = a.ID_Projet, Nom = a.NomProjet }).ToArray();
             return Json(l_proj);
        }
        [HttpPost]
        public JsonResult takeProbill(int searchedValue)
        {
            var l_proB = (from a in db.DetailFDTs orderby a.Facture where a.ID_Projet == searchedValue select new { ID = a.Facture, Nom = a.Facture }).Distinct().ToArray();
            return Json(l_proB);
        }
        [HttpPost]
        public JsonResult getClient(int id)
        {
            var cl = (from a in db.Clients where a.ID_Client == id select new { Nom = a.Nom, Prenom = a.Prenom }).ToArray();
            return Json(cl);
        }
        [HttpPost]
        public JsonResult getProjet(int id)
        {
            var cl = (from a in db.Projects where a.ID_Projet == id select new { Nom = a.NomProjet, Adresa = a.Ville+","+a.NoCivique+" "+a.NomRue }).ToArray();
            return Json(cl);
        }
        [HttpPost]
        public JsonResult getProbill(int id,int id_projet)
        {
            if (id >= 0)
            {
                var list = (from a in db.DetailFDTs
                            where a.Facture == id&&a.ID_Projet==id_projet
                            orderby a.Facture,a.ID_Statut ascending
                            join b in db.VracFournisseurs on a.IdVrac equals b.ID
                            join c in db.Vracs on b.IdVrac equals c.IdVrac
                            join d in db.Fournisseurs on a.ID_Fournisseur equals d.ID_Fournisseur
                            join e in db.Equipements on a.ID_Equipement equals e.ID_Equipement
                            select new { a.Facture, a.IDFDT, a.Description, a.Duree, c.Materiau, d.NomLegal, e.NomEquipement, a.No_Voyage, a.No_Tonnes, a.Prix_UM, a.Prix_Total, a.Doc_in }).ToArray();
                return Json(list);
            }
            else
            {
                var list = (from a in db.DetailFDTs
                            where a.ID_Projet==id_projet
                            orderby a.Facture,a.ID_Statut ascending
                            join b in db.VracFournisseurs on a.IdVrac equals b.ID
                            join c in db.Vracs on b.IdVrac equals c.IdVrac
                            join d in db.Fournisseurs on a.ID_Fournisseur equals d.ID_Fournisseur
                            join e in db.Equipements on a.ID_Equipement equals e.ID_Equipement
                            select new { a.Facture, a.IDFDT, a.Description, a.Duree, c.Materiau, d.NomLegal, e.NomEquipement, a.No_Voyage, a.No_Tonnes, a.Prix_UM, a.Prix_Total, a.Doc_in }).ToArray();
                return Json(list);
            }
 
        }
 
 
 
        public ActionResult GenererRapport(int? cmbClient, int? cmbProjet, int? cmbProbill)
        {
            string fichier;
            //les clients concernes
            var cl = (from a in db.Clients where a.ID_Client == cmbClient select new { Nom = a.Nom, Prenom = a.Prenom }).ToArray();
 
            //les projets concernes
            var pr = (from a in db.Projects where a.ID_Projet == cmbProjet select new { Nom = a.NomProjet, Adresa = a.Ville + "," + a.NoCivique + " " + a.NomRue }).ToArray();
            //les factures idprobill >=0
      var list1 = (from a in db.DetailFDTs
                         where a.Facture == cmbProbill && a.ID_Projet == cmbProjet
                            orderby a.Facture,a.ID_Statut ascending
                            join b in db.VracFournisseurs on a.IdVrac equals b.ID
                            join c in db.Vracs on b.IdVrac equals c.IdVrac
                            join d in db.Fournisseurs on a.ID_Fournisseur equals d.ID_Fournisseur
                            join e in db.Equipements on a.ID_Equipement equals e.ID_Equipement
                            join f in db.FDTs on a.IDFDT  equals f.IDFDT
                            join em in db.Employees on f.ID_Employee  equals em.ID_Employee
                            select new { a.Facture, a.IDFDT, a.Description, a.Duree, c.Materiau, d.NomLegal, e.NomEquipement, a.No_Voyage, a.No_Tonnes,a.ID_Projet_out, a.Doc_in,a.Date,a.Prix_UM,em.Nom,em.Prenom }).ToArray();
 
                //les factures idprobill <0
            var list2 = (from a in db.DetailFDTs
                            where a.ID_Projet==cmbProjet
                            orderby a.Facture,a.ID_Statut ascending
                            join b in db.VracFournisseurs on a.IdVrac equals b.ID
                            join c in db.Vracs on b.IdVrac equals c.IdVrac
                            join d in db.Fournisseurs on a.ID_Fournisseur equals d.ID_Fournisseur
                            join e in db.Equipements on a.ID_Equipement equals e.ID_Equipement
                            join f in db.FDTs on a.IDFDT  equals f.IDFDT
                            join em in db.Employees on f.ID_Employee  equals em.ID_Employee
                            select new { a.Facture, a.IDFDT, a.Description, a.Duree, c.Materiau, d.NomLegal, e.NomEquipement, a.No_Voyage, a.No_Tonnes, a.ID_Projet_out, a.Doc_in, a.Date,a.Prix_UM, em.Nom, em.Prenom }).ToArray();
 
 
            fichier = Server.MapPath(@"\Content\Rapport\rapport.xls");
            #region on vérifie si le fichier existe bien;
            //Si le fichier n'existe pas, erreur
            FileInfo fi = new FileInfo(fichier);
 
            if (!fi.Exists)
            {
                //c'est pas ça qu'il faut vérifier mais la dm
                ViewBag.Message = "Oops! Problème avec le fichier";
 
                return View("Erreur");
            }
            #endregion
            try
                {
                    // Opening the Excel template...
                    FileStream fs =new FileStream(fichier, FileMode.Open, FileAccess.ReadWrite);
 
                    // Getting the complete workbook...
                    HSSFWorkbook templateWorkbook = new HSSFWorkbook(fs, true);
 
                    // Getting the worksheet by its name...
                    HSSFSheet sheet = templateWorkbook.GetSheet("Feuil1");
 
                    //afficher client
                    HSSFRow clientCell = sheet.GetRow(1);
                    clientCell.GetCell(1).SetCellValue(cl[0].Nom +"  "+ cl[0].Prenom);
 
                    // projet
                    if (cmbProjet!=-1)
                    {
                        HSSFRow projetCell = sheet.GetRow(2);
                        projetCell.GetCell(1).SetCellValue(pr[0].Nom + "  " + pr[0].Adresa);
 
                    }
                    int row = 5;
                    //affichage ligne par ligne
                    if (cmbProbill>=0)
                    {
                        foreach (var item in list1)
                        {
                            row++;
                            HSSFRow valueDateCell = sheet.GetRow(row);
                            valueDateCell.GetCell(0).SetCellValue(item.Date.ToString());
 
                            HSSFRow valuePrenomCell = sheet.GetRow(row);
                            valuePrenomCell.GetCell(1).SetCellValue(item.Nom +" "+ item.Prenom);
 
                            HSSFRow valueEquiCell = sheet.GetRow(row);
                            valueEquiCell.GetCell(2).SetCellValue(item.NomEquipement);
 
                            HSSFRow valueTacheCell = sheet.GetRow(row);
                            valueTacheCell.GetCell(3).SetCellValue(item.Description);
 
                            HSSFRow valueVracCell = sheet.GetRow(row);
                            valueVracCell.GetCell(4).SetCellValue(item.Materiau);
 
                            HSSFRow valueTonneCell = sheet.GetRow(row);
                            valueTonneCell.GetCell(5).SetCellValue(item.No_Tonnes);
 
                            HSSFRow valueVoyageCell = sheet.GetRow(row);
                            valueVoyageCell.GetCell(6).SetCellValue(item.No_Voyage);
 
                            HSSFRow valueSortieCell = sheet.GetRow(row);
                            valueSortieCell.GetCell(7).SetCellValue(item.ID_Projet_out);
 
                            HSSFRow valueFournisseurCell = sheet.GetRow(row);
                            valueFournisseurCell.GetCell(8).SetCellValue(item.NomLegal);
 
                            HSSFRow valueFactureCell = sheet.GetRow(row);
                            valueFactureCell.GetCell(9).SetCellValue(item.Facture);
 
                            HSSFRow valuePrixCell = sheet.GetRow(row);
                            valuePrixCell.GetCell(10).SetCellValue(item.Prix_UM);
                        }
                   }
                else
                   {
                        foreach (var item in list2)
                        {
                            row++;
                            HSSFRow valueDateCell = sheet.GetRow(row);
                            valueDateCell.GetCell(0).SetCellValue(item.Date.ToString());
 
                            HSSFRow valuePrenomCell = sheet.GetRow(row);
                            valuePrenomCell.GetCell(1).SetCellValue(item.Nom +"  "+ item.Prenom);
 
                            HSSFRow valueEquiCell = sheet.GetRow(row);
                            valueEquiCell.GetCell(2).SetCellValue(item.NomEquipement);
 
                            HSSFRow valueTacheCell = sheet.GetRow(row);
                            valueTacheCell.GetCell(3).SetCellValue(item.Description);
 
                            HSSFRow valueVracCell = sheet.GetRow(row);
                            valueVracCell.GetCell(4).SetCellValue(item.Materiau);
 
                            HSSFRow valueTonneCell = sheet.GetRow(row);
                            valueTonneCell.GetCell(5).SetCellValue(item.No_Tonnes);
 
                            HSSFRow valueVoyageCell = sheet.GetRow(row);
                            valueVoyageCell.GetCell(6).SetCellValue(item.No_Voyage);
 
                            HSSFRow valueSortieCell = sheet.GetRow(row);
                            valueSortieCell.GetCell(7).SetCellValue(item.ID_Projet_out);
 
                            HSSFRow valueFournisseurCell = sheet.GetRow(row);
                            valueFournisseurCell.GetCell(8).SetCellValue(item.NomLegal);
 
                         HSSFRow valueFactureCell = sheet.GetRow(row);
                   valueFactureCell.GetCell(9).SetCellValue(item.Facture);
 
                            HSSFRow valuePrixCell = sheet.GetRow(row);
                            valuePrixCell.GetCell(10).SetCellValue(item.Prix_UM);
                        }
                    }
                    if (cmbProjet==-1)
                    {
                        var l_proj = (from a in db.Projects orderby a.NomProjet where a.ID_Client == cmbClient select new { ID = a.ID_Projet, Nom = a.NomProjet }).ToArray();
                        foreach (var item1 in l_proj)
                        {
                            var list3 = (from a in db.DetailFDTs
                                         where a.ID_Projet == item1.ID
                                         orderby a.Facture, a.ID_Statut ascending
                                         join b in db.VracFournisseurs on a.IdVrac equals b.ID
                                         join c in db.Vracs on b.IdVrac equals c.IdVrac
                                         join d in db.Fournisseurs on a.ID_Fournisseur equals d.ID_Fournisseur
                                         join e in db.Equipements on a.ID_Equipement equals e.ID_Equipement
                                         join f in db.FDTs on a.IDFDT equals f.IDFDT
                                         join em in db.Employees on f.ID_Employee equals em.ID_Employee
                                         select new { a.Facture, a.IDFDT, a.Description, a.Duree, c.Materiau, d.NomLegal, e.NomEquipement, a.No_Voyage, a.No_Tonnes, a.ID_Projet_out, a.Doc_in, a.Date, a.Prix_UM, em.Nom, em.Prenom }).ToArray();
                            foreach (var item in list3)
                            {
                                row++;
                                HSSFRow valueDateCell = sheet.GetRow(row);
                                valueDateCell.GetCell(0).SetCellValue(item.Date.ToString());
 
                                HSSFRow valuePrenomCell = sheet.GetRow(row);
                                valuePrenomCell.GetCell(1).SetCellValue(item.Nom + "  " + item.Prenom);
 
                                HSSFRow valueEquiCell = sheet.GetRow(row);
                                valueEquiCell.GetCell(2).SetCellValue(item.NomEquipement);
 
                                HSSFRow valueTacheCell = sheet.GetRow(row);
                                valueTacheCell.GetCell(3).SetCellValue(item.Description);
 
                                HSSFRow valueVracCell = sheet.GetRow(row);
                                valueVracCell.GetCell(4).SetCellValue(item.Materiau);
 
                                HSSFRow valueTonneCell = sheet.GetRow(row);
                                valueTonneCell.GetCell(5).SetCellValue(item.No_Tonnes);
 
                                HSSFRow valueVoyageCell = sheet.GetRow(row);
                                valueVoyageCell.GetCell(6).SetCellValue(item.No_Voyage);
 
                                HSSFRow valueSortieCell = sheet.GetRow(row);
                                valueSortieCell.GetCell(7).SetCellValue(item.ID_Projet_out);
 
                                HSSFRow valueFournisseurCell = sheet.GetRow(row);
                                valueFournisseurCell.GetCell(8).SetCellValue(item.NomLegal);
 
   HSSFRow valueFactureCell = sheet.GetRow(row);
                                 valueFactureCell.GetCell(9).SetCellValue(item.Facture);
 
                                HSSFRow valuePrixCell = sheet.GetRow(row);
                                valuePrixCell.GetCell(10).SetCellValue(item.Prix_UM);
                            }
                        }
 
                    }
                  // Forcing formula recalculation...
                  //sheet.ForceFormulaRecalculation = true;
 
                  MemoryStream ms = new MemoryStream();
 
                   // Writing the workbook content to the FileStream...
                  templateWorkbook.Write(ms);
                  return File(ms.ToArray(), "application/vnd.ms-excel", Server.HtmlEncode("Rapport.xls"));
 
                }
                catch (Exception ex)
                {
                    ViewBag.Message = "Oops! Problème avec le fichier";
 
                    return View("Erreur");
                }
        }
 
 
 
 
        // GET: /Rapports/Details/5
 
        public ActionResult Details(int id)
        {
            return View();
        }
 
        //
        // GET: /Rapports/Create
 
        public ActionResult Create()
        {
            return View();
        } 
 
        //
        // POST: /Rapports/Create
 
        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
 
        //
        // GET: /Rapports/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }
 
        //
        // POST: /Rapports/Edit/5
 
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
 
        //
        // GET: /Rapports/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }
 
        //
        // POST: /Rapports/Delete/5
 
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
 
    }
}