Log in to like this post! How to export WinChart to Excel and how to print WinChart using WinPrintPreviewDialog Tom Puglisi / Thursday, September 11, 2008 Hey everyone, Have you ever wanted to export WinChart to Excel? How about showing WinChart in a Print Preview dialog? If you would like to know, then have I got the sample for you! Exporting to WinChart: Basically, what I am doing is newing up a Workbook object and adding a Worksheet to it. I then use WinChart's SaveTo( ) method and pass in a MemoryStream. This basically causes WinChart to save an image representation of itself to the MemoryStream. I then use the Image.FromStream( ) method and create an Image which then gets passed to the constructor of a WorkSheetImage. You specify the TopLeftCornerCell as well as the BottomLeftCornerCell so that the image is placed correctly in the Worksheet. You must also specify the point coordinates for the TopLeftCornerPosition property as well as the BottomRightCornerPosition property. This allows the Image to position itself relative to the cells that you specified. We then use the Workbook's Save method to persist it to disk. That's it! Code: /// <summary> /// Clicking this button will export the Chart to Excel /// /// Infragistics Help: Add an Image to a Worksheet /// http://help.infragistics.com/Help/NetAdvantage/NET/2008.2/CLR2.0/html/ExcelEngine_Add_an_Image_to_a_Worksheet.html /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnExport_Click(object sender, EventArgs e) { Workbook w = new Workbook(); Worksheet s = w.Worksheets.Add("Chart"); MemoryStream ms = new MemoryStream(); this.ultraChart1.SaveTo(ms, ImageFormat.Png); WorksheetImage i = new WorksheetImage(Image.FromStream(ms)); i.TopLeftCornerCell = s.Rows[0].Cells[0]; i.TopLeftCornerPosition = new PointF(0.0F, 0.0F); i.BottomRightCornerCell = s.Rows[20].Cells[6]; i.BottomRightCornerPosition = new PointF(0.0F, 0.0F); s.Shapes.Add(i); string theFile = Application.StartupPath + @"\" + DateTime.Now.Ticks.ToString() + ".xls"; w.Save(theFile); ms.Dispose(); Process.Start(theFile); } Printing WinChart using WinPrintPreviewDialog and WinPrintDocument In this example, I added the UltraPrintPreviewDialog component to the main Form as well as an UltraPrintDocument. I then set the PreviewDialog's Document Property to the PrintDocument. Once this is hooked up, all we need to do is call the Dialog's Show( ) or ShowDialog( ) method. This will cause the Document's PrintPage event to fire. You can toss your code in that event handler. In this simple sample, all I am doing is converting the Chart into an image once again and then using the DrawImage( ) method of the Graphics object (exposed through the PrintPageEventArgs) so that I can draw the Chart image to the Page. Code: /// <summary> /// Clicking this button will launch the PrintPreviewDialog /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnPrint_Click(object sender, EventArgs e) { this.ultraPrintPreviewDialog1.Document = this.ultraPrintDocument1; this.ultraPrintPreviewDialog1.ShowDialog(); } /// <summary> /// Once the PrintPreviewDialog launches, it will cause the /// PrintDocument to render its output into the Dialog's preview area. /// Code to draw whatever you want can be implemented in this event. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ultraPrintDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { MemoryStream ms = new MemoryStream(); this.ultraChart1.SaveTo(ms, ImageFormat.Png); e.Graphics.DrawImage(Image.FromStream(ms), new Point(20, 20)); ms.Dispose(); } Download Sample Application (C#) Enjoy!