How to export view to the excel ASP.NET MVC

6.1k views Asked by At

I'm not an ASP.NET MVC expert but I need to handle a small project and need some help.

Basically what I'm doing is an MVC project for submitting or listing some text reports and I used database first approach on the model. So I have this model of the tables I've created before, without doing a good plan. After starting the project, I happened to be obliged to add some images to it which wasn't a case while I was designing the data model and I solved this issue in a very poor way, via going out of the MVC philosophy but it worked.

So I added an "img" folder to the project and put the .jpeg files inside.

..and I showed the images in view like that among the data coming from the model:

...@foreach (var item in Model) {
<tr>
    <td>            
       <img src="~/img/@string.Format("{0}.jpeg",item.emailid)"/>
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.emailid)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.date)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.task1)
    </td>.......

And then I exported that view to the excel file using this code:

public ActionResult ExportData()
    {
        GridView gv = new GridView();
        gv.DataSource = db.web_weeklyreports.Include(w => w.web_kategori).Include(w => w.web_kategori1).Include(w => w.web_kategori2).Include(w => w.web_kategori3).Include(w => w.web_kategori4).ToList();
        gv.DataBind();
        Response.ClearContent();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment; filename=WeeklyReports.xls");
        Response.ContentType = "application/ms-excel";
        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("adminreports");
    }

This code worked well except the image because on DataSource there is only model information and my images are not located in Model, they are in a folder inside the solution as shown above.

So I'm able to export my view except for images and I need to get those images to excel report too.

So this is my view with images

Any help?

1

There are 1 answers

0
Furkan Keskin On BEST ANSWER

I solved it with this class

        public ActionResult ExportData()
    {
        GridView gv = new GridView();
        var data = db.web_weeklyreports.Include(w => w.web_kategori).Include(w => w.web_kategori1).Include(w => w.web_kategori2).Include(w => w.web_kategori3).Include(w => w.web_kategori4).ToList();

        gv.AutoGenerateColumns = false;
        gv.Columns.Add(new ImageField { HeaderText="IMG",  DataImageUrlField = "Imagepath", DataImageUrlFormatString = "https://localhost:44353/img/{0}.jpeg",  });
        gv.Columns.Add(new BoundField { HeaderText="ID", DataField = "ID" });
        gv.Columns.Add(new BoundField { HeaderText="Email", DataField = "emailid" });
        gv.Columns.Add(new BoundField { HeaderText="Date", DataField = "date" });
        gv.Columns.Add(new BoundField { HeaderText="Task 1", DataField = "task1" });
        gv.Columns.Add(new BoundField { HeaderText="", DataField = "t1kategoriid" });
        gv.Columns.Add(new BoundField { HeaderText="", DataField = "task2" });
        gv.Columns.Add(new BoundField { HeaderText="", DataField = "t2kategoriid" });
        gv.Columns.Add(new BoundField { HeaderText="", DataField = "task3" });
        gv.Columns.Add(new BoundField { HeaderText="", DataField = "t3kategoriid" });
        gv.Columns.Add(new BoundField { HeaderText="", DataField = "task4" });
        gv.Columns.Add(new BoundField { HeaderText="", DataField = "t4kategoriid" });
        gv.Columns.Add(new BoundField { HeaderText="", DataField = "task5" });
        gv.Columns.Add(new BoundField { HeaderText="", DataField = "t5kategoriid" });

        DataTable dt = new DataTable();
        dt.Columns.Add("Imagepath");
        dt.Columns.Add("ID");
        dt.Columns.Add("emailid");
        dt.Columns.Add("date");
        dt.Columns.Add("task1");
        dt.Columns.Add("t1kategoriid");
        dt.Columns.Add("task2");
        dt.Columns.Add("t2kategoriid");
        dt.Columns.Add("task3");
        dt.Columns.Add("t3kategoriid");
        dt.Columns.Add("task4");
        dt.Columns.Add("t4kategoriid");
        dt.Columns.Add("task5");
        dt.Columns.Add("t5kategoriid");

        foreach (var item in data)
        {                 
            dt.Rows.Add(item.emailid, item.ID, item.emailid, item.date, item.task1, item.t1kategoriid, item.task2, item.t2kategoriid, item.task3, item.t3kategoriid, item.task4, item.t4kategoriid, item.task5, item.t5kategoriid);                
        }

        gv.DataSource = dt;
        gv.DataBind();

        for (int i = 0; i < data.Count; i++)
        {
            gv.Rows[i].Height = 40;
        }

        Response.ClearContent();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment; filename=WeeklyReports.xls");
        Response.ContentType = "application/ms-excel";
        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("adminreports");
    }

Now I can export images like this: