How to export AjaxToolkit control linechart to Excel - c#

In application i have grid view and Ajax Toolkit control linechart i need to export grid view and linechart to excel sheet(open office/Libra office). i tried some code in export button but what happens grid view is exported but line chart is not export to excel. my page appear like this [my chart image]
by clicking the export button then only grid view will export to excel but not linechart it will not export.what i need is chart should be export to excel. how to do this
i tried code is:
.aspx:
<cc1:LineChart ID="LineChart1" runat="server" ChartHeight="300" ChartWidth="900" ChartType="Basic" ChartTitleColor="#0E426C" Visible="true" CategoryAxisLineColor="#D08AD9" ValueAxisLineColor="#D08AD9" BaseLineColor="#A156AB"></cc1:LineChart>
.cs:
protected void btnExport_Click(object sender, EventArgs e)
{
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "filename.xls"));
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
base.OnPreRender(e);
ScriptManager sm = ScriptManager.GetCurrent(Page);
sm.RegisterScriptControl(LineChart1);
LineChart1.Visible = true;
for (int i = 0; i < grdview1.HeaderRow.Cells.Count; i++)
{
grdview1.HeaderRow.Cells[i];
}
grdview1.RenderControl(htw);
LineChart1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
I added OnPreRender() for Registering the scriptmanager control
protected override void OnPreRender(EventArgs e)
{
/* Verifies that the control is rendered */
base.OnPreRender(e);
ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
if (scriptManager == null)
{
scriptManager = new ScriptManager();
scriptManager.ID = "ScriptManager1";
scriptManager.EnablePartialRendering = true;
Controls.AddAt(0, scriptManager);
}
}
Can anyone help me out how can i export linechart.
Thank you

No, you can not export LineChart.

Related

The excel report Settings pop-up doesn't show on getting an excel report

I use Stimulsoft 2017. Once I click on the excel report's option nothing appears, which was supposed to show export settings as it is shown below.And it just download the excel report all of a sudden.
I use this code to generate excel,but these settings don't make any effect on exported excel files.
void StiReportViewer_ReportExport(object sender, StiExportDataEventArgs e)
{
try
{
if (e.Settings.GetExportFormat() == StiExportFormat.Excel || e.Settings.GetExportFormat() == StiExportFormat.Excel2007)
{
// this.StiReportViewer.ExportResponse = false;
Stimulsoft.Report.Export.StiExcelExportSettings stiExcelExportSettings = new Stimulsoft.Report.Export.StiExcelExportSettings();
stiExcelExportSettings.ExportDataOnly = true;
stiExcelExportSettings.UseOnePageHeaderAndFooter = true;
stiExcelExportSettings.ExportPageBreaks = false;
stiExcelExportSettings.ExportObjectFormatting = false;
MemoryStream xlsMemoryStream = new MemoryStream();
e.Report.ExportDocument(StiExportFormat.Excel, xlsMemoryStream, stiExcelExportSettings);
Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=Report.xls");
Response.ContentType = "application/ms-excel";
Response.BinaryWrite(xlsMemoryStream.ToArray());
Response.End();
}
}
catch (Exception ex)
{
}
Finally,I figure out that it runs with js, and there is a line of code that should change this way.
<Sti:StiWebViewer ID="StiReportViewer" runat="server" Width="100%" RenderMode="AjaxWithCache"
ButtonsImagesPath="Images" ToolBarBackColor="WhiteSmoke" OnGetReportData="StiReportViewer_GetReportData" ButtonImagesPath="Images/ReportViewer/" ShowExportDialog="false">
</Sti:StiWebViewer>
should change this way as show below:
<Sti:StiWebViewer ID="StiReportViewer" runat="server" Width="100%" RenderMode="AjaxWithCache"
ButtonsImagesPath="Images" ToolBarBackColor="WhiteSmoke" OnGetReportData="StiReportViewer_GetReportData" ButtonImagesPath="Images/ReportViewer/" ShowExportDialog="true">
</Sti:StiWebViewer>

asp.net don't work grid to excel

Simple program with a button 'btnExportToExcel' when I click this button don't show any response.when I debug this button code after executing all lines shows the form after clicking button at second time cursor goes to another method named GridView1_RowCreated(used for designing grid) but don't response to convert grid to excel.
What should I do now?
protected void btnExportToExcel_Click(object sender, EventArgs e)
{
Response.ClearContent();
Response.AppendHeader("content-disposition", "attachment;
filename=Inventory.xls");
Response.ContentType = "application/ms-excel";
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
GridView1.RenderControl(htmlTextWriter);
Response.Write(stringWriter.ToString());
Response.End();
}
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
//Add CSS class on header row.
if (e.Row.RowType == DataControlRowType.Header)
e.Row.CssClass = "header";
//Add CSS class on normal row.
if (e.Row.RowType == DataControlRowType.DataRow &&
e.Row.RowState == DataControlRowState.Normal)
e.Row.CssClass = "normal";
//Add CSS class on alternate row.
if (e.Row.RowType == DataControlRowType.DataRow &&
e.Row.RowState == DataControlRowState.Alternate)
e.Row.CssClass = "alternate";
}
You need to Rebind the GridView on the post back.
protected void btnExportToExcel_Click(object sender, EventArgs e)
{
Response.ClearContent();
Response.AppendHeader("content-disposition", "attachment; filename=Inventory.xls");
Response.ContentType = "application/ms-excel";
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
GridView1.DataBind();
GridView1.RenderControl(htmlTextWriter);
Response.Write(stringWriter.ToString());
Response.End();
}
try this code working fine
protected void btnExportToExcel_Click(object sender, EventArgs e)
{
// Grid_bindExcel();
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("content-disposition", "attachment;filename=Category.xls");
Response.ContentType = "application/ms-excel";
Response.ContentEncoding = System.Text.Encoding.Unicode;
Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(sw);
Label lblheader = new Label();
lblheader.Font.Size = 14;
lblheader.Font.Bold = true;
lblheader.Text = "Category Detail";
lblheader.RenderControl(hw);
GrdExcel.Visible = true;
GrdExcel.RenderControl(hw);
Response.Write(sw.ToString());
Response.Flush();
Response.End();
GrdExcel.Visible = false;
}
public override void VerifyRenderingInServerForm(Control control)
{
}

ASP.NET Gridview to Export to Excel

I am using Visual Studio 2015, Entity Framework 6 and C#.
I am trying to get my gridview to export to excel. I think I have everything, but when clicking the button my gridview disappears and nothing ever goes to a file.
My gridview is:
gvExOr
The Excel Export code is:
public partial class _Default : Page
{
private void BindFiles()
{
DirectoryInfo di = new DirectoryInfo(tbPath.Text);
gvExOr.DataSource = di.GetFiles();
try {
gvExOr.DataBind();
}
catch { }
}
protected void btnExportToExcel_Click(object sender, EventArgs e)
{
ExportToExcel();
}
//Export to Excel from a GridView
protected void ExportToExcel()
{
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application / vnd.ms - excel";
Response.AddHeader("content - disposition", "attachment; filename = MyFiles.xls");
Response.Charset = "";
this.EnableViewState = false;
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
gvExOr.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
protected void Page_Load(object sender, EventArgs e)
{
BindFiles();
}
}
How do I get the gridview to export to excel?
Looks like you used code from this post https://stackoverflow.com/a/10412559/5786449
I tried the example from that post and it works for me. The only difference I can see is in your Response.ContentType and Response.AddHeader. Try getting rid of the white spaces in those parameters:
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("content-disposition", "attachment;filename=MyFiles.xls");
One more thing, your code doesn't actually give you an Excel spreadsheet. It just writes out the GridView HTML markup. Many Excel programs are not expecting HTML in an XLS file and will throw an error when opening the file. If you want an actual Excel spreadsheet I would recommend looking into utilities like EPPlus http://epplus.codeplex.com/

Saving an asp.net chart to file

I have a bar chart containing information on various machines relating to company.
Basically i want to output this chart to png file but i cant seem to get it output properly.
Ive been searching Google for hours trying to find a good tutorial but most of them use weird third party components to download the image and i really dont wanna do that.
this is my code at the moment:
string tmpChartName = "/MachinesByCompanyChart.png";
protected void GenerateBarChartBut_click(object sender, EventArgs e)
{
Chart1.Visible = false;
Chart2.Visible = true;
DataTable table = new DataTable();
dal.getTotalAssetsByCompany("table", TAB1CompanyDDL.SelectedItem.Text);
table = dal.Results.Tables["table"];
DataView dv = table.DefaultView;
Chart2.Series["Series1"].Points.DataBindXY(dv, "AssetType", dv, "Total");
Chart2.Palette = ChartColorPalette.None;
Chart2.PaletteCustomColors = myGreenColorPalette;
string imgPath2 = Server.MapPath(tmpChartName);
Chart2.SaveImage(imgPath2, ChartImageFormat.Png);
}
protected void ExportAssetsByCompanyBut_click(object sender, EventArgs e)
{
Response.Clear();
Response.ContentType = Chart1.ImageType.ToString();
Response.AddHeader("Content-Disposition", "attachment; filename=" + tmpChartName);
StringWriter stringWrite = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
string headerTable = #"";
Response.Write(headerTable);
Response.Write(stringWrite.ToString());
Response.End();
}
Am i passing the saved image to the writer method properly?
try this,
set this two properties of your chart in aspx.page
EnableViewState="true"
ImageStorageMode="UseImageLocation"
write your code on aspx.cs page
System.IO.MemoryStream imagestream = new System.IO.MemoryStream();
Chart1.SaveImage(imagestream, System.Web.UI.DataVisualization.Charting.ChartImageFormat.Png);
byte[] imageByte = imagestream.ToArray();

Copy and leave out checkboxes on Repeater

I have a repeater which has some controls which are checkboxes. I would like to copy all items in the repeater except the checkboxes, where in their place i will copy the value of teh checkbox. THe purpose is that I would like to export all of the information from the repeater to an excel document in xls format. However, this does not allow me to have checkboxes, thus the reason I want to remove them.
How should I do this please?
I have tried:
for (int j =0; j<repeater1.Items.Count; j++)
{
RepeaterItem repItem = repeater1.Items[j];
foreach (Control c in repItem.Controls)
{
if (!(c is CheckBox))
{
Control c2 = c;
repeater2.Items[j].Controls.Add(c2);
}
}
}
But it gives me this error:
Collection was modified; enumeration operation may not execute.
You wrote in the comment, that you want to export your repeater that has a checkbox. It is not allowed to export a checkbox to excel and that's why you want to remove the checkbox before exporting.
Here is one way you can do this... just add this in your page and try....
public override void VerifyRenderingInServerForm(Control control)
{
}
Alternatively what if you export your DataTable instead of the repeater ?
If the above is not working and you want to iterate and then export then here is code...
protected void btnExport_Click(object sender, EventArgs e)
{
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
string attachment = "attachment; filename=FileName" + DateTime.Now.ToString() + ".xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
foreach (RepeaterItem item in Repeater1.Items)
{
CheckBox chk= item.FindControl("CheckBox") as CheckBox;
chk.Visible = false;
}
Repeater1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}

Categories