Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Creating an Excel spreadsheet
Message
 
To
25/05/2004 14:16:40
General information
Forum:
ASP.NET
Category:
Other
Miscellaneous
Thread ID:
00905836
Message ID:
00907198
Views:
13
I have created a class from code I have downloaded from the net.

you make a call like this in your c#
// pass a Page reference and dataset as parameters from an ASP.net page
createexcel oexcel = new createexcel(this,localds);

// include files for your class
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;

namespace mynamespace
{
	public class createexcel
	{
		public createexcel(Page opage,DataSet odataset)
		{
			
			string strFile ="c:\\report" + System.DateTime.Now.Ticks.ToString();

			if (odataset.Tables[0].Rows.Count>0)
			{	
				StreamWriter sw = new StreamWriter(strFile+".txt");
				sw.Write("<html>");
				sw.Write("<head>");
				sw.Write("<title>ASP.NET Excel Sample</title>");
				sw.Write("</head>");
				sw.Write("<body>");
				sw.Write("<table>");
				sw.Write("<thead>");
				sw.Write("<tr>");
				for(int x=0;x<odataset.Tables[0].Columns.Count;x++)
				{
					
					sw.Write("<th bgcolor='blue'><font color='white'>"+odataset.Tables[0].Columns[x].ColumnName.ToString()+"</font></th>");

				}
				sw.Write("</tr>");
				sw.Write("</thead>");
				for(int x=0;x<odataset.Tables[0].Rows.Count;x++)
				{
					sw.Write("<tr>");
					for(int y=0;y<odataset.Tables[0].Columns.Count;y++)
					{
						
						sw.Write("<td>"+odataset.Tables[0].Rows[x][y].ToString()+"</td>");
					}
					sw.Write("</tr>");
				}
				sw.Write("</tbody>");
				sw.Write("</table>");
				sw.Write("</body>");
				sw.Write("</html>");
				sw.Close();
			
				FileStream MyFileStream = new FileStream(strFile+".txt", FileMode.Open);
				long FileSize;
				FileSize = MyFileStream.Length;
				byte[] Buffer = new byte[(int)FileSize];
				MyFileStream.Read(Buffer, 0, (int)MyFileStream.Length);
				MyFileStream.Close();
				System.IO.File.Delete(strFile+".txt");
				opage.Response.ContentType="application/vnd.ms-excel";
				opage.Response.AddHeader( "content-disposition","attachment; filename=MyXLS.XLS");
				opage.Response.BinaryWrite(Buffer);
			}

			//
			// TODO: Add constructor logic here
			//
		}
	}

}
>Tks, Eric. I'll look into those.
>
>
>
>>Have you seen these:
>>http://support.microsoft.com/default.aspx?scid=kb;EN-US;306022
>>http://support.microsoft.com/default.aspx?scid=kb;en-us;307021&Product=vbNET
Fred Besterwitch, MCP

The harder you work. The luckier you get.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform