944 messaggi dal 11 febbraio 2013
Ciao
come mi è stato suggerito tempo fa nel forum sto cercando di modificare l'esportazione delle view
senza usare una session

Il problema è che non ottengo il modello filtrato bensi tutto il modello

public ActionResult DownloadXLS(String filtro, ecc)
        {
            var model = db.Table.AsQueryable();

            if (!string.IsNullOrEmpty(filtro))
                model = model.Where(i => i.campo == filtro);

           ...

            GridView gv = new GridView();
            gv.DataSource = model.ToList();
            gv.DataBind();

            Response.Clear();
            Response.Buffer = true;
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";  
            Response.AddHeader("content-disposition", "attachment;filename=file.xls");
            Response.Charset = "";

            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);

            gv.RenderControl(htw);

            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();

            //return RedirectToAction("Index");
            return View(model);

        }


 <a href="@Url.Action("DownloadXLS", "controller")" class="btn btn-primary " id="btnXLS">
            XLS
            <span class="fa fa-file-excel-o" aria-hidden="true"></span>
        </a>
       


tra l'altro non mi fa entrare in debug per vedere cosa accade

qualche suggerimento?
944 messaggi dal 11 febbraio 2013
nessuno ?
nemmeno tu Moreno che me lo hai suggerito ? :)

tranqui so che avrai molto da fare
11.886 messaggi dal 09 febbraio 2002
Contributi
Ciao,
sì, effettivamente è un periodo un po' pieno.

Dato che stai usando ASP.NET MVC, non dovresti usare controlli da ASP.NET WebForms né usare direttamente l'oggetto Response.

Il file Excel dovresti produrlo con una libreria tipo NPOI, e poi inviarlo al client usando l'esempio che vedi qui, ovviamente adeguando il mimetype.
http://www.prideparrot.com/blog/archive/2012/8/uploading_and_returning_files#filestreamresult

ciao,
Moreno

Enjoy learning and just keep making
944 messaggi dal 11 febbraio 2013
Ho fatto cosi e funziona
La libreria è fantastica ...ne conoscevo alcune ma anche questa sembra eccellente
public  ActionResult DownloadXLS(String Provincia, String Stato)
        {         
            //filtro modello
            var model = db.ANAGRAFICA.AsNoTracking().AsQueryable();

            if (!string.IsNullOrEmpty(Provincia)) { 
                model = model.Where(item => item.Prov == Provincia);
            }
            if (!string.IsNullOrEmpty(Stato)) { 
                model = model.Where(item => item.Stato == Stato);
            }
            try
            {
                var workbook = new HSSFWorkbook();

                var sheet = workbook.CreateSheet();

                var headerRow = sheet.CreateRow(0);

                headerRow.CreateCell(0).SetCellValue("RagSociale");
                headerRow.CreateCell(1).SetCellValue("Indirizzo");
                headerRow.CreateCell(2).SetCellValue("Comume");
                headerRow.CreateCell(3).SetCellValue("Cap");
                headerRow.CreateCell(4).SetCellValue("Provincia");

                //(Optional) freeze the header row so it is not scrolled
                sheet.CreateFreezePane(0, 1, 0, 1);

                int rowNumber = 1;

                //Populate the sheet with values from the grid data
                foreach (ANAGRAFICA a in model)
                {
                    //Create a new row
                    var row = sheet.CreateRow(rowNumber++);

                    //Set values for the cells
                    row.CreateCell(0).SetCellValue(a.RagSoc);
                    row.CreateCell(1).SetCellValue(a.Indiri);
                    row.CreateCell(2).SetCellValue(a.Comune);
                    row.CreateCell(3).SetCellValue(a.Cap);
                    row.CreateCell(4).SetCellValue(a.Prov);

                }

                MemoryStream output = new MemoryStream();
                workbook.Write(output);

                return File(output.ToArray(), "application/vnd.ms-excel", "Anagrafica.xls"); 
               
            }

            catch (Exception ex)
            {
                TempData["Message"] = "Oops! Some error occurs.";

                return RedirectToAction("Index");
            }

            
        }


html attribute
@Html.ActionLink("XLS", "DownloadXLS", new { Provincia = Request["Provincia"], Stato = Request["Stato"] }, new { id = "exportLink" , @class="btn btn-success"})


Dici che va bene ?

poi se dovesse servire a qualcuno...

ciao
e grazie assai
Modificato da jjchuck il 04 ottobre 2017 12.47 -
11.886 messaggi dal 09 febbraio 2002
Contributi
Ottimo Lavoro! E grazie per la condivisione :)

Enjoy learning and just keep making

Torna al forum | Feed RSS

ASPItalia.com non è responsabile per il contenuto dei messaggi presenti su questo servizio, non avendo nessun controllo sui messaggi postati nei propri forum, che rappresentano l'espressione del pensiero degli autori.