2017-05-28 54 views
0

我不是ASP.NET MVC專家,但我需要處理一個小型項目並需要一些幫助。如何將視圖導出到Excel中ASP.NET MVC

基本上我正在做的是提交或列出一些文本報告的MVC項目,我在模型上使用了數據庫第一種方法。所以我有這個我之前創建的表格的模型,沒有制定好計劃。在開始項目之後,我碰巧被迫添加了一些圖像,但在設計數據模型時,這並不是一個案例,我通過走出MVC理念,以非常糟糕的方式解決了這個問題,但它工作正常。

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

..和我發現在查看圖像一樣,從模型傳來的數據中:

[email protected] (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>....... 

然後,我用這個代碼導出的以Excel文件:

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"); 
    } 

此代碼運行良好,除了圖像,因爲在DataSource上只有模型信息和我的圖像不在模型中,他們在一個folde如上圖所示。

所以我能夠導出我的觀點,除了圖像,我需要得到這些圖像優秀的報告。

So this is my view with images

任何幫助嗎?

+1

你爲什麼不使用客戶端的出口解決了嗎? – msd

回答

0

我與此類

 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"); 
    } 

現在,我可以通過使用HTML表導出相似圖片this:

相關問題