如何将asp.net图表控件导出为ex​​cel / word? [英] How to export asp.net chart control to excel/word?

查看:64
本文介绍了如何将asp.net图表控件导出为ex​​cel / word?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试导出我创建的图表时遇到此错误。该错误显示无法下载图表图像。我该怎么办?我不是在创建一个动态图表,它与datareader绑定。可以任意建议一种以最简单的方式导出图表的方法吗?



代码:

  protected   void  ExportToPdf_Click( object  sender,EventArgs e)
{
Exportchart( this .Chart1);
}
受保护 void 出口图(图表)
{
chart.SaveImage(Server.MapPath( ./ Image / chart.png)) ;
iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.LETTER, 72 72 82 72 < /跨度>);
MemoryStream msReport = new MemoryStream();

尝试
{
iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance (document,msReport);
document.AddAuthor( Test);
document.AddSubject( 导出为PDF);
document.Open();
iTextSharp.text.Chunk c = new iTextSharp.text.Chunk( 将图表导出为PDF,iTextSharp.text.FontFactory.GetFont( VERDANA 15 ));
iTextSharp.text.Paragraph p = new iTextSharp.text.Paragraph();
p.Alignment = iTextSharp.text.Element.ALIGN_CENTER;
iTextSharp.text.Image hImage;
hImage = iTextSharp.text.Image.GetInstance(Server.MapPath( ./ Image / chart.png ));

float NewWidth = 500 ;
float MaxHeight = 400 ;

if (hImage.Width < = NewWidth){NewWidth = hImage 。宽度; } float NewHeight = hImage.Height * NewWidth / hImage.Width; if (NewHeight > MaxHeight)
{
NewWidth = hImage.Width * MaxHeight / hImage.Height;
NewHeight = MaxHeight;
}

float ratio = hImage.Width / hImage.Height;
hImage.ScaleAbsolute(NewWidth,NewHeight);
document.Add(p);
document.Add(hImage);
document.Close();

Response.AddHeader( Content-type application / pdf);
Response.AddHeader( Content-Disposition attachment; filename = chart.pdf);
Response.OutputStream.Write(msReport.GetBuffer(), 0 ,msReport.GetBuffer()。Length);

}
catch (System.Threading.ThreadAbortException ex)
{
throw new 异常( 发生错误: + ex);
}
}

解决方案

以下是将图表导出为PDF和excel的解决方案



http://haseet.blogspot.in/2013/02/develop-chart-in-aspnet-with-export-to-excel-pdf-msoffice-openoffice.html

您可以查看CP文章以供参考:使用Report Viewer 2010将.NET MSChart导出为Excel / PDF [ ^ ]


文档pdfDoc =新文件(PageSize.A4,10f,10f,10f,0f); 
PdfWriter.GetInstance(pdfDoc,Response.OutputStream);
pdfDoc.Open();
using(MemoryStream stream = new MemoryStream())
{
Chart1.SaveImage(stream,ChartImageFormat.Png);
iTextSharp.text.Image chartImage = iTextSharp.text.Image.GetInstance(stream.GetBuffer());
chartImage.ScalePercent(75f);
pdfDoc.Add(chartImage);
pdfDoc.Close();

Response.ContentType =application / pdf;
Response.AddHeader(content-disposition,attachment; filename = Chart.pdf);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();


Hi, i am facing this error when i tried to export a chart that i created. The error shows that "could not download chart image". What should i do? I am not creating a dynamic chart, it is bind with datareader..can any1 suggest a way to export chart at e most easy way?

code:

protected void ExportToPdf_Click(object sender, EventArgs e)
        {
            Exportchart(this.Chart1);
        }
protected void Exportchart(Chart chart)
        {
            chart.SaveImage(Server.MapPath("./Image/chart.png"));
            iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.LETTER, 72, 72, 82, 72);
            MemoryStream msReport = new MemoryStream();

            try
            {
                iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, msReport);
                document.AddAuthor("Test");
                document.AddSubject("Export to PDF");
                document.Open();
                iTextSharp.text.Chunk c = new iTextSharp.text.Chunk("Export chart to PDF", iTextSharp.text.FontFactory.GetFont("VERDANA", 15));
                iTextSharp.text.Paragraph p = new iTextSharp.text.Paragraph();
                p.Alignment = iTextSharp.text.Element.ALIGN_CENTER;
                iTextSharp.text.Image hImage;
                hImage = iTextSharp.text.Image.GetInstance(Server.MapPath("./Image/chart.png"));

                float NewWidth = 500;
                float MaxHeight = 400;

                if (hImage.Width <= NewWidth) { NewWidth = hImage.Width; } float NewHeight = hImage.Height * NewWidth / hImage.Width; if (NewHeight > MaxHeight)
                {
                    NewWidth = hImage.Width * MaxHeight / hImage.Height;
                    NewHeight = MaxHeight;
                }

                float ratio = hImage.Width / hImage.Height;
                hImage.ScaleAbsolute(NewWidth, NewHeight);
                document.Add(p);
                document.Add(hImage);
                document.Close();

                Response.AddHeader("Content-type", "application/pdf");
                Response.AddHeader("Content-Disposition", "attachment; filename=chart.pdf");
                Response.OutputStream.Write(msReport.GetBuffer(), 0, msReport.GetBuffer().Length);

            }
            catch (System.Threading.ThreadAbortException ex)
            {
                throw new Exception("Error occured: " + ex);
            }
        }

解决方案

Here is the solution for exporting chart to PDF and excel

http://haseet.blogspot.in/2013/02/develop-chart-in-aspnet-with-export-to-excel-pdf-msoffice-openoffice.html


Well you can have a look on a CP article for reference: Export .NET MSChart to Excel/PDF Using Report Viewer 2010[^]


 Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
using (MemoryStream stream = new MemoryStream())
{
    Chart1.SaveImage(stream, ChartImageFormat.Png);
    iTextSharp.text.Image chartImage = iTextSharp.text.Image.GetInstance(stream.GetBuffer());
    chartImage.ScalePercent(75f);
    pdfDoc.Add(chartImage);
    pdfDoc.Close();

    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=Chart.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.Write(pdfDoc);
    Response.End();


这篇关于如何将asp.net图表控件导出为ex​​cel / word?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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