从MVC5中的局部视图将数据导出到PDF / Excel [英] Exporting Data to PDF/Excel in from a partial view in MVC5

查看:91
本文介绍了从MVC5中的局部视图将数据导出到PDF / Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

my partialview code:

Note: My partialview is loaded in pop up(jquery dialog box) and

I have 2 html buttons on click of them am callign the actionmethod which would to write the grid data to pdf/excel

@using System.Dynamic
@model List<System.Collections.IDictionary>
@{
Layout = null;
}
@{
var result = new List<dynamic>();

foreach (var emprow in Model)
{
var row = (IDictionary<string, object>)new ExpandoObject();
Dictionary<string, object> eachEmpRow = (Dictionary<string, object>)emprow;

foreach (KeyValuePair<string, object> keyValuePair in eachEmpRow)
{
row.Add(keyValuePair);
}
result.Add(row);
}
var gridReport = new WebGrid(result, canSort: true, ajaxUpdateContainerId: "gridReport",
rowsPerPage: 5);
gridReport.Pager(WebGridPagerModes.NextPrevious); ;
}
<script type="text/javascript">

function Print() {
//debugger;
$.ajax({
type: "POST",
url: '@Url.Action("GetPdf")',
data: {
strEMPID: $("#hdnEMPID").val(),
//presetName: $('#listboxStyle option:selected')
strpresetID: $('#lstFLDPresets option:selected').val()

},

error: function (ex) {
}
});

}
function GetExcel() {
//debugger;
$.ajax({
type: "POST",
url: '@Url.Action("GetExcel")',
data: {
strEMPID: $("#hdnEMPID").val(),
//presetName: $('#listboxStyle option:selected')
strpresetID: $('#lstFLDPresets option:selected').val()

},

error: function (ex) {
}
});

}
</script>
<div id="gridReport">
@if (@Model != null)
{
@gridReport.GetHtml(tableStyle: "webGrid", headerStyle: "head", alternatingRowStyle: "alt");
}
</div>

<div>
@* Dowload to Excel : @Html.ActionLink("Get Excel in Download Folder", "GetExcel")*@
<input id="btnExcel" type="button" value=" Export to Excel " onclick="GetExcel(); return false" />
</div>
<div>
@* Download as PDF : @Html.ActionLink("Open in PDF ", "GetPdf")*@
<input id="btnPdf" type="button" value="Export to PDF" onclick="Print(); return false" />
</div>
<div id="content" style="display: none; background: #c9dbff; align-content:center">
</div>

My Controller Action method

public void GetPdf(string strEMPID, int strpresetID)
{
try
{

WebGrid wd = new WebGrid(source: fldHomeDAL.GetReports(strEMPID, strpresetID), canPage: false, canSort: false);
string griddata = wd.GetHtml().ToString();

Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=report_grid.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter s_w = new StringWriter();
HtmlTextWriter h_w = new HtmlTextWriter(s_w);
StringReader sr = new StringReader(griddata);
Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();


}
catch (Exception ex)
{
logger.Error(ex);
}
}

public void GetExcel(string strEMPID, int strpresetID)
{
try
{

//var result = fldHomeDAL.GetReports(strEMPID, strpresetID);

WebGrid wd = new WebGrid(source: fldHomeDAL.GetReports(strEMPID, strpresetID), canPage: false, canSort: false);

string griddata = wd.GetHtml().ToString();

WebGrid wd = new WebGrid(source: fldHomeDAL.GetReports(strEMPID, strpresetID), canPage: false, canSort: false);

string griddata = wd.GetHtml().ToString();
//string griddata = fldHomeDAL.GetReports(strEMPID, strpresetID).ToString();
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=FLDReports.xls");
Response.ContentType = "application/excel";
Response.Write(griddata);
Response.End();
}
catch (Exception ex)
{
logger.Error(ex);
}

}

I have done narrow debugging, everything evrything is working fine but pdf /excel is not getting showed.

Any help would be greatly appreciated!!  TIA.

推荐答案

.ajax({
type:POST,
url:'@ Url。动作(GetPdf)',
数据:{
strEMPID:
.ajax({ type: "POST", url: '@Url.Action("GetPdf")', data: { strEMPID:


(#hdnEMPID)。val(),
// presetName :
("#hdnEMPID").val(), //presetName:


('#listboxStyle选项:选中')
strpresetID:
('#listboxStyle option:selected') strpresetID:


这篇关于从MVC5中的局部视图将数据导出到PDF / Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆