2011-11-05 52 views

回答

1

你可以像這樣.....

注:這是樣品例如如何打印MSChart的在asp.net的web應用程序,你可以改變這取決於你的要求.......

using System; 
using System.Web; 
using System.Web.Services; 
using System.Web.Services.Protocols; 
using System.ComponentModel; 
using System.Web.UI.DataVisualization.Charting; 
using System.IO; 
using System.Web.UI.WebControls; 

namespace Avatar_Reports 
{ 
    public class ExtendedChart : Chart 
    { 
     /// <summary> 
     /// This property was added to keep a serialized version of the Chart at a given fixed time. 
     /// </summary> 
     public string SerializedString { get; set; } 

     /// <summary> 
     /// Updates the SerializedString property with current state of the object. 
     /// </summary> 
     public void UpdateSerializedString() 
     { 
      // Serialize the Chart into serialized Chart variable. 
      using (MemoryStream writer = new MemoryStream()) 
      { 
       this.Serializer.Content = SerializationContents.All; 
       this.Serializer.Save(writer); 
       this.SerializedString = GetStringFromStream(writer); 
      } 
     } 

     /// <summary> 
     /// Obtain an ExtendedChart with state and data as saved in the SerializedString property. 
     /// </summary> 
     /// <returns></returns> 
     public ExtendedChart GetChartFromSerialized() 
     { 
      // Deserialize the Chart into a Chart variable. 
      ExtendedChart clonedChart = new ExtendedChart(); 
      byte[] chartAsArray = System.Text.Encoding.Default.GetBytes(this.SerializedString); 
      using (MemoryStream reader = new MemoryStream(chartAsArray)) 
      { 
       clonedChart.Serializer.Content = SerializationContents.All; 
       clonedChart.Serializer.Load(reader); 
      } 

      return clonedChart; 
     } 

     private string GetStringFromStream(Stream stream) 
     { 
      using (StreamReader reader = new StreamReader(stream)) 
      { 
       return reader.ReadToEnd(); 
      } 
     } 
    } 
} 

然後你創建你的頁面與噸他類型的對象是這樣的:

.....

ExtendedChart ReportChart = new ExtendedChart(); 
Series Series1 = new Series("Series1"); 
ChartArea ChartArea1 = new ChartArea("ChartArea1"); 

....... 然後你就可以在你的菜單使用調用一個方法是這樣

SerializeChartControls(outputTable); 

Session["CtrlToPrint"] = outputTable; 
        Page.ClientScript.RegisterStartupScript(this.GetType(), 
         "Print", "<script language='javascript'>window.open('Print.aspx','PrintMe','height=1000px,width=900px,scrollbars=1');</script>"); 

......

private void SerializeChartControls(Control root) 
{ 
    foreach (Control c in root.Controls) 
    { 
     if (c is Chart) 
     { 
      ExtendedChart theChart = c as ExtendedChart; 
      theChart.UpdateSerializedString(); 
     } 
     else if ((c != null) && (c.HasControls() == true)) 
     { 
      SerializeChartControls(c); 
     } 
    } 
}  

你PRINT.ASPX可以是這樣的:

.....

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.IO; 
using System.Web.UI.DataVisualization.Charting; 

namespace Avatar_Reports 
{ 
    public partial class Print : System.Web.UI.Page 
    { 
     private WebControl ctrlToPrint; 
     private string footerText = String.Empty; 

     protected void Page_PreInit(object sender, EventArgs e) 
     { 
      if (!IsPostBack) 
      { 
       ctrlToPrint = (WebControl)Session["CtrlToPrint"]; 
       DeserializeChartControls(ctrlToPrint); 
       ExpandAllGridViews(ctrlToPrint); 
      } 
     } 

     protected void Page_Init(object sender, EventArgs e) 
     { 
      if (!IsPostBack) 
      { 
       Panel titlePanel = new Panel(); 
       Label reportTitle = new Label(); 

       // reportTitle.Text = Session["ReportTitle"].ToString(); 
       titlePanel.CssClass = "ReportTitlePrinted"; 

       // titlePanel.Controls.Add(reportTitle); 
       // Page.Form.Controls.Add(titlePanel); 

       ctrlToPrint.Style.Add("text-align", "center"); 
       Page.Form.Controls.Add(ctrlToPrint); 
      } 
     } 

     protected void Page_Load(object sender, EventArgs e) 
     { 
      this.RegisterStartupScript("printPopup", "<script>window.print();</script>"); 
     } 


     private void DeserializeChartControls(Control root) 
     { 
      foreach (Control c in root.Controls) 
      { 
       if (c is ExtendedChart) 
       { 
        ExtendedChart theChart = c as ExtendedChart; 
        ExtendedChart newChart = theChart.GetChartFromSerialized(); 

        // Replace the serialized version by the actual Chart. 
        WebControl chartParent = theChart.Parent as WebControl; 
        int chartIndex = chartParent.Controls.IndexOf(theChart); 
        chartParent.Controls.AddAt(chartIndex, newChart); 
        chartParent.Controls.Remove(theChart); 
       } 
       else if ((c != null) && (c.HasControls() == true)) 
       { 
        DeserializeChartControls(c); 
       } 
      } 
     } 

我希望它會幫助你....