I have tried itextsharp samples and the serializing to xml . Am using Entity frame work 6.0 and asp.net mvc4
Asked
Active
Viewed 5,606 times
-1
-
Please explain why iTextSharp is not working for you. Show us the code you tried and give any errors you had. – Amedee Van Gasse Aug 03 '16 at 07:30
2 Answers
1
I'm using following for export to excel:
In your Controller:
public ActionResult ExportData()
{
var datasource = db.Products.ToList();
GridView gv = new GridView();
gv.DataSource = datasource;
gv.DataBind();
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment; filename=Report.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 Json("Success");
}
In View:
@using (Html.BeginForm("ExportData", "Home", FormMethod.Post))
{
<button type="Submit">Export to Excel</button>
}
//OR:
@Html.ActionLink("Export to Excel", "ExportData", "Home")
As to export to PDF, I'd recommend to use Rotativa. Works ok for me.
EDITED
I'm using filters too. You may send it's value from View
to controller action
and modify datasource
. For example:
public ActionResult ExportData(DateTime fromDate)
{
var datasource = db.Products.Where(g=>g.Date <= fromDate).ToList();

Andrey Gubal
- 3,481
- 2
- 18
- 21
-
am populating the data in MVC Webgrid with some filters is there any approach the only populated(binded ) data of wegrid data to export dynamically. Thanks in advance – amshekar Sep 29 '13 at 18:22
-
Yes I can able to export the data to excel by using gridview but in my web page am displaying the data in webgrid the data what ever I populated want to export to excel randomly. in one action method of controller instead of having different action method for populating data into WEBGRID and one more action method for exporting data using GRIDVIEW. – amshekar Oct 02 '13 at 17:49
1
Try this:
var data = GetFeedbackDetailsExport();
var gridWeb = new WebGrid(source: data, canPage: false, canSort: false);
string exportData = gridWeb.GetHtml(
columns: gridWeb.Columns(
gridWeb.Column("EnterpriseId", "EnterpriseId"),
gridWeb.Column("GroupName", "Group Name"),
gridWeb.Column("Geography", "Geography"),
gridWeb.Column("Country", "Country"),
gridWeb.Column("DeliveryCentre", "Delivery Centre"),
gridWeb.Column("Vendor", "Vendor Name"),
gridWeb.Column("Category", "Category"),
gridWeb.Column("ModelName", "ModelName"),
gridWeb.Column("Status", "Status"),
gridWeb.Column("Date_Submitted", "Submitted On", format: (item) => item.Date_Submitted != null ? item.Date_Submitted.ToString("dd/MMM/yyyy") : ""),
gridWeb.Column("UpdatedBy", "Feedback Given By", style: "col-lg-1"),
gridWeb.Column("UpdatedOn", "Feedback Given On", format: (item) => item.UpdatedOn != null ? item.UpdatedOn.ToString("dd/MMM/yyyy") : "")
)
).ToHtmlString();
return File(new System.Text.UTF8Encoding().GetBytes(exportData),
"application/vnd.ms-excel",
"FeedbackReport.xls");

Snehlata Shaw
- 97
- 5