Here is my Export ActionResult to PDF:
List<DailySum> lstDailySum = db.DailySum.Include(x => x.codeACl).Include(x => x.codeCou).Include(x => x.codeAgc).Include(x => x.AInfo).ToList();
GridView gv = new GridView();
if (Session["Paging"] == null)
{
gv.DataSource = lstDailySum;
}
else
{
gv.DataSource = Session["Paging"];
}
gv.DataBind();
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment; filename=DailySumExport.pdf");
Response.ContentType = "application/pdf";
Response.Charset = "";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gv.RenderControl(htw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
Now this works fine when I try and export to Excel but when I try and export to PDF I get this error message:
Adobe Acrobat Reader DC could not open 'DailySumExport.pdf' because it is either not supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded).
Any help on how to resolve this issue would be greatly appreciated.
Thanks.
Related
I have written the below code to generate the excel file.
private void ExportReport(IList ApproveListData, string fileName)
{
var gv = new GridView();
gv.AllowPaging = false;
gv.DataSource = ApproveListData;
gv.DataBind();
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
Response.ContentType = "application/ms-excel";
Response.Charset = "";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gv.RenderControl(htw);
Response.Output.Write("<b> Pending approval report </b>");
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
I have checked data in the grid view but excel file is not generating.
Please help.
Thanks in advance.
I have used the below code. importing the data from List<> to excel works fine but it add the Black line border into a excel file.how can I remove that border from excel file ?
if (id != null)
{
int mid = Convert.ToInt32(clsPasswordHelper.Decryptdata(id));
var objMySavedMedicinePricesResults = db.MedicineRequestDownload(mid).ToList();
GridView gv = new GridView();
gv.DataSource = objMySavedMedicinePricesResults;
gv.DataBind();
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment; filename=" + DateTime.Now.ToShortDateString() + "_MedicinePrice.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(new { success = true }, JsonRequestBehavior.AllowGet);
}
The way you've used generate html output and client open html file as excel.On this way you can't style excel cells and table.
Use ClosedXML library.with this library you can create a valid excel file and you'll able to modify the style of exported excel.
Example code:
var workbook = new XLWorkbook();
var worksheet = workbook.Worksheets.Add("Sample Sheet");
worksheet.Cell("A1").Value = "Hello World!";
workbook.SaveAs("HelloWorld.xlsx");
I am trying to export Grid View in excel and I get formatting issue like number is not getting proper(e.g -1.523E+11)
also 0 is missing, where value is leading with 0.
Response.ClearContent();
Response.ClearHeaders();
DataTable dtGetReportBank = new DataTable();
dtGetReportBank = objPO.GetReportBank(id);
gridData.DataSource = dtGetReportBank;
gridData.DataBind();
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "BankCMS" + ".xls"));
Response.ContentType = "application/ms-excel";
StringWriter sw1 = new StringWriter();
HtmlTextWriter htw1 = new HtmlTextWriter(sw1);
gridData.AllowPaging = false;
dvContent.RenderControl(htw1);
Response.Write(sw1.ToString());
Response.End();
Btn.Visible = false;
Amol,
The best to way to handle this is using OpenXML.
Create a SpreadsheetDocument -> WorkbookPart -> WorksheetPart -> Cell
While creating a Cell, specify the Cell.DataType and you will get whatever you expect.
Once the Document is filled, use SpreadsheetDocument.Save() method to save the same
Hope this helps!
I really appreciate if anyone can help me on how can I enable the filter feature on Excel when exporting the data from gridview.( Excel Autofilter)
public ActionResult ExportToExcel(List<EventViewModel> list)
{
try
{
// Main
GridView gv = new GridView();
gv.DataSource = list.ToList();
gv.DataBind();
Response.ClearContent();
Response.Buffer = true;
Response.Charset = "";
Response.AddHeader("content-disposition", "attachment; filename=filename.xls");//Response.AddHeader("content-disposition", "inline; filename=Excel.xls");
Response.ContentType = "application/excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gv.RenderControl(htw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
return RedirectToAction("Index");
}
catch (System.Exception e)
{
return View("Error");
}
}
Your code exports an HTML file with XLS extension. It is not a binary XLS file and you cannot have an Excel autofilter, not using your code.
If you can't use Microsoft.Office.Interop.Excel library, you can search for another Excel library that exports real Excel files.
I use EasyXLS Excel library. This code exports the datatable of the gridview and adds an autofilter and it use this library:
// Create an instance of the class that exports Excel files, having one sheet
ExcelDocument xls = new ExcelDocument(1);
// Get the sheet
ExcelWorksheet xlsWorksheet = (ExcelWorksheet)xls.easy_getSheetAt(0);
// Create a dataset that keeps the gridview datatable
DataSet dataSet = new DataSet();
dataSet.Tables.Add((DataTable)gridView.DataSource);
// Insert the dataset into sheet
xlsWorksheet.easy_insertDataSet(dataSet, true);
//Add AutoFilter
ExcelFilter xlsFilter = xlsWorksheet.easy_getFilter();
xlsFilter.setAutoFilter("A1:E1");
// Choose a name for the xls file
string fileName = "filename.xls";
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
Response.ContentType = "application/vnd.ms-excel";
// Export Excel file and prompt the "Open or Save Dialog Box"
xls.easy_WriteXLSFile(Response.OutputStream);
// Dispose memory
xls.Dispose();
Response.End();
For more details on formatting read this link about export gridview to Excel.
I am using following function to export my data to excel sheet:
string filename = "Test.xls";
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
//Get the H`enter code here`TML for the control.
yourGrid.RenderControl(hw);
//Write the HTML back to the browser.
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
Response.Write(tw.ToString());
but excel sheet is not created. Although it shows in chrome browser bottom that excel is downloading. When it finishs downloading, I click it and it says file cant be opened. Please suggest me what is wrong in it?
[EDIT]
Response.Clear();
Response.Buffer = true;
//use Response.AddHeader
Response.AddHeader("content-disposition", "attachment;filename=Test.xlsx");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
gvLogs.RenderControl(hw);
gvLogs.AllowPaging = false;
gvLogs.DataBind(); // bind data
Response.Output.Write(sw.ToString());
//need to call Flush and End methods
Response.Flush();
Response.End();
give same error
Epplus is a good library for excel import/export. You can check it out.
You can try this one
private void ExportToExcel(DataTable dt)
{
if (dt.Rows.Count > 0)
{
string filename = "DownloadReport.xls";
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
DataGrid dgGrid = new DataGrid();
dgGrid.DataSource = dt;
dgGrid.DataBind();
//Get the HTML for the control.
dgGrid.RenderControl(hw);
//Write the HTML back to the browser.
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
this.EnableViewState = false;
Response.Write(tw.ToString());
Response.End();
}
}
Change the 'Response.ContentType' value in your code as like below.
Response.ContentType = "application/vnd.xls";
Hope this will help you.
check below code
Response.Clear();
Response.Buffer = true;
//use Response.AddHeader
Response.AddHeader("content-disposition", "attachment;filename=Test.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.RenderControl(hw);
GridView1.AllowPaging = false;
GridView1.DataBind(); // bind data
Response.Output.Write(sw.ToString());
//need to call Flush and End methods
Response.Flush();
Response.End();