Click here to Skip to main content
15,891,597 members
Articles / Web Development / HTML

Utilize Internet Explorer to display reports using XML and XSL from a Windows application

Rate me:
Please Sign up or sign in to vote.
4.74/5 (23 votes)
11 Jan 2005CPOL4 min read 80.8K   1.1K   50  
How to create a report from XML, format it using an XSL style sheet and display the report in Internet Explorer, all done without saving anything to file.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace ListCtrlReport
{
	/// <summary>
	/// Summary description for FormFormat.
	/// </summary>
	public class FormFormat : System.Windows.Forms.Form
	{
		private System.Windows.Forms.ComboBox comboBoxFont;
		private System.Windows.Forms.ComboBox comboBoxColor;
		private System.Windows.Forms.Label labelFont;
		private System.Windows.Forms.Label labelColor;
		private System.Windows.Forms.CheckBox checkBoxGridlines;
		private System.Windows.Forms.Label labelDirection;
		private string colorText = "Red";
		private string font = "Times New Roman";
		private int gridlines = 0;
		private bool vertical = true;
		private System.Windows.Forms.ComboBox comboBoxDirection;
		private System.Windows.Forms.Button buttonOK;
		private System.Windows.Forms.Button buttonCancel;

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public FormFormat()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.comboBoxFont = new System.Windows.Forms.ComboBox();
			this.comboBoxColor = new System.Windows.Forms.ComboBox();
			this.labelFont = new System.Windows.Forms.Label();
			this.labelColor = new System.Windows.Forms.Label();
			this.checkBoxGridlines = new System.Windows.Forms.CheckBox();
			this.labelDirection = new System.Windows.Forms.Label();
			this.comboBoxDirection = new System.Windows.Forms.ComboBox();
			this.buttonOK = new System.Windows.Forms.Button();
			this.buttonCancel = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// comboBoxFont
			// 
			this.comboBoxFont.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.comboBoxFont.ImeMode = System.Windows.Forms.ImeMode.NoControl;
			this.comboBoxFont.Items.AddRange(new object[] {
															  "Arial",
															  "Times New Roman",
															  "Courier New"});
			this.comboBoxFont.Location = new System.Drawing.Point(104, 8);
			this.comboBoxFont.Name = "comboBoxFont";
			this.comboBoxFont.Size = new System.Drawing.Size(168, 21);
			this.comboBoxFont.TabIndex = 0;
			// 
			// comboBoxColor
			// 
			this.comboBoxColor.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.comboBoxColor.Items.AddRange(new object[] {
															   "Yellow",
															   "Red",
															   "Blue",
															   "Green"});
			this.comboBoxColor.Location = new System.Drawing.Point(104, 48);
			this.comboBoxColor.Name = "comboBoxColor";
			this.comboBoxColor.Size = new System.Drawing.Size(168, 21);
			this.comboBoxColor.TabIndex = 1;
			// 
			// labelFont
			// 
			this.labelFont.Location = new System.Drawing.Point(8, 8);
			this.labelFont.Name = "labelFont";
			this.labelFont.Size = new System.Drawing.Size(64, 23);
			this.labelFont.TabIndex = 2;
			this.labelFont.Text = "Font";
			// 
			// labelColor
			// 
			this.labelColor.Location = new System.Drawing.Point(8, 48);
			this.labelColor.Name = "labelColor";
			this.labelColor.Size = new System.Drawing.Size(64, 23);
			this.labelColor.TabIndex = 3;
			this.labelColor.Text = "Text Color";
			// 
			// checkBoxGridlines
			// 
			this.checkBoxGridlines.Checked = true;
			this.checkBoxGridlines.CheckState = System.Windows.Forms.CheckState.Checked;
			this.checkBoxGridlines.Location = new System.Drawing.Point(8, 80);
			this.checkBoxGridlines.Name = "checkBoxGridlines";
			this.checkBoxGridlines.Size = new System.Drawing.Size(152, 24);
			this.checkBoxGridlines.TabIndex = 4;
			this.checkBoxGridlines.Text = "Display Gridlines";
			// 
			// labelDirection
			// 
			this.labelDirection.Location = new System.Drawing.Point(8, 120);
			this.labelDirection.Name = "labelDirection";
			this.labelDirection.Size = new System.Drawing.Size(64, 23);
			this.labelDirection.TabIndex = 6;
			this.labelDirection.Text = "Direction";
			// 
			// comboBoxDirection
			// 
			this.comboBoxDirection.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.comboBoxDirection.Items.AddRange(new object[] {
																   "Vertical layout",
																   "Horizontal layout"});
			this.comboBoxDirection.Location = new System.Drawing.Point(104, 120);
			this.comboBoxDirection.Name = "comboBoxDirection";
			this.comboBoxDirection.Size = new System.Drawing.Size(168, 21);
			this.comboBoxDirection.TabIndex = 5;
			// 
			// buttonOK
			// 
			this.buttonOK.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.buttonOK.Location = new System.Drawing.Point(59, 160);
			this.buttonOK.Name = "buttonOK";
			this.buttonOK.TabIndex = 7;
			this.buttonOK.Text = "OK";
			this.buttonOK.Click += new System.EventHandler(this.buttonOK_Click);
			// 
			// buttonCancel
			// 
			this.buttonCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.buttonCancel.Location = new System.Drawing.Point(147, 160);
			this.buttonCancel.Name = "buttonCancel";
			this.buttonCancel.TabIndex = 8;
			this.buttonCancel.Text = "Cancel";
			// 
			// FormFormat
			// 
			this.AcceptButton = this.buttonOK;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.CancelButton = this.buttonCancel;
			this.ClientSize = new System.Drawing.Size(280, 198);
			this.Controls.Add(this.buttonCancel);
			this.Controls.Add(this.buttonOK);
			this.Controls.Add(this.labelDirection);
			this.Controls.Add(this.comboBoxDirection);
			this.Controls.Add(this.checkBoxGridlines);
			this.Controls.Add(this.labelColor);
			this.Controls.Add(this.labelFont);
			this.Controls.Add(this.comboBoxColor);
			this.Controls.Add(this.comboBoxFont);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
			this.Name = "FormFormat";
			this.Text = "Format";
			this.Load += new System.EventHandler(this.FormFormat_Load);
			this.ResumeLayout(false);

		}
		#endregion

		private void FormFormat_Load(object sender, System.EventArgs e)
		{
			// Initialize the form from member variables.

			comboBoxDirection.SelectedIndex = vertical ? 0 : 1;
			
			comboBoxFont.SelectedIndex = comboBoxFont.FindString(font);

			comboBoxColor.SelectedIndex = comboBoxColor.FindString(colorText);

			checkBoxGridlines.Checked = gridlines == 1;
		}

		private void buttonOK_Click(object sender, System.EventArgs e)
		{
			// Update member variables from the form

			vertical = comboBoxDirection.SelectedIndex == 0;

			font = comboBoxFont.SelectedItem.ToString();

			colorText = comboBoxColor.SelectedItem.ToString();

			gridlines = checkBoxGridlines.Checked ? 1 : 0;

			DialogResult = DialogResult.OK;
		}

		public string StyleSheet
		{
			get
			{
				// Generate the style sheet according to user preferences.
			
				string xsl;
				
				if (vertical)
				{
					// vertical rows

					xsl = 
						@"<?xml version='1.0'?>" + Environment.NewLine +
						@"<xsl:stylesheet xmlns:xsl=""http://www.w3.org/1999/XSL/Transform"" version=""1.0"">" + Environment.NewLine +
						@"<xsl:template match=""/"">" + Environment.NewLine +
						@"<HTML>" + Environment.NewLine +
						@"<TITLE>Utilize Internet Explorer to display reports using XML and XSL from a windows application</TITLE>" + Environment.NewLine +
						@"<BODY>" + Environment.NewLine +
						@"<FONT face=""" + font + @""">" + Environment.NewLine +
						@"<TABLE border=""" + gridlines + @""">" + Environment.NewLine +
						@"  <xsl:attribute name=""style"">" + Environment.NewLine +
						@"    <xsl:text>color:" + colorText + " </xsl:text>" + Environment.NewLine +
						@"  </xsl:attribute>" + Environment.NewLine +
						@"  <xsl:for-each select=""AUTHORS/AUTHOR"">" + Environment.NewLine +
						@"  <TR>" + Environment.NewLine +
						@"    <TD>" + Environment.NewLine +
						@"      Author Id: <B><xsl:value-of select=""@ID""/></B><BR></BR>" + Environment.NewLine +
						@"      First Name: <B><xsl:value-of select=""FNAME""/></B><BR></BR>" + Environment.NewLine +
						@"      Last Name: <B><xsl:value-of select=""LNAME""/></B><BR></BR>" + Environment.NewLine +
						@"      Phone Number: <B><xsl:value-of select=""PHONE""/></B><BR></BR>" + Environment.NewLine +
						@"    </TD>" + Environment.NewLine +
						@"  </TR>" + Environment.NewLine +
						@"  </xsl:for-each>" + Environment.NewLine +
						@"</TABLE>" + Environment.NewLine +
						@"</FONT>" + Environment.NewLine +
						@"</BODY>" + Environment.NewLine +
						@"</HTML>" + Environment.NewLine +
						@"</xsl:template>" + Environment.NewLine +
						@"</xsl:stylesheet>";								
				}
				else
				{
					// horizontal rows

					xsl = 
						@"<?xml version='1.0'?>" + Environment.NewLine +
						@"<xsl:stylesheet xmlns:xsl=""http://www.w3.org/1999/XSL/Transform"" version=""1.0"">" + Environment.NewLine +
						@"<xsl:template match=""/"">" + Environment.NewLine +
						@"<HTML>" + Environment.NewLine +
						@"<TITLE>Utilize Internet Explorer to display reports using XML and XSL from a windows application</TITLE>" + Environment.NewLine +
						@"<BODY>" + Environment.NewLine +
						@"<FONT face=""" + font + @""">" + Environment.NewLine +
						@"<TABLE border=""" + gridlines + @""">" + Environment.NewLine +
						@"  <xsl:attribute name=""style"">" + Environment.NewLine +
						@"    <xsl:text>color:" + colorText + " </xsl:text>" + Environment.NewLine +
						@"  </xsl:attribute>" + Environment.NewLine +
						@"  <TR>" + Environment.NewLine +
						@"    <TD class=""heading""><B>Author ID</B></TD>" + Environment.NewLine +
						@"    <TD class=""heading""><B>First Name</B></TD>" + Environment.NewLine +
						@"    <TD class=""heading""><B>Last Name</B></TD>" + Environment.NewLine +
						@"    <TD class=""heading""><B>Phone Number</B></TD>" + Environment.NewLine +
						@"  </TR>" + Environment.NewLine +
						@"  <xsl:for-each select=""AUTHORS/AUTHOR"">" + Environment.NewLine +
						@"  <TR>" + Environment.NewLine +
						@"    <TD>" + Environment.NewLine +
						@"       <xsl:value-of select=""@ID""/>" + Environment.NewLine +
						@"    </TD>"+ Environment.NewLine +
						@"    <TD>" + Environment.NewLine +
						@"       <xsl:value-of select=""FNAME""/>" + Environment.NewLine +
						@"    </TD>"+ Environment.NewLine +
						@"    <TD>" + Environment.NewLine +
						@"      <xsl:value-of select=""LNAME""/>" + Environment.NewLine +
						@"    </TD>" + Environment.NewLine +
						@"    <TD>" + Environment.NewLine +
						@"      <xsl:value-of select=""PHONE""/>" + Environment.NewLine +
						@"    </TD>" + Environment.NewLine +
						@"  </TR>" + Environment.NewLine +
						@"  </xsl:for-each>" + Environment.NewLine +
						@"</TABLE>" + Environment.NewLine +
						@"</FONT>" + Environment.NewLine +
						@"</BODY>" + Environment.NewLine +
						@"</HTML>" + Environment.NewLine +
						@"</xsl:template>" + Environment.NewLine +
						@"</xsl:stylesheet>";	
				}
				return xsl;
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
I'm a software developer from Sweden who got tired of snow and cold weather and moved to USA. I choose New York City, so I wouldn't totally miss out on snow and cold weather. I work on Wall Street with financial systems (not much else to do in this neighborhood). I primarily use Visual C++/MFC or C#/.NET as development tool.

The picture is of my wife and me in Cannes, France, drinking the most expensive Coke we ever had.

Comments and Discussions