Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

exporting into excel in .net

Status
Not open for further replies.

bcardona

Programmer
May 10, 2001
103
US
i can export my web page into excel fine in office 97, but when it is blank when using office 2000, any ideas?
thanks
 
How are you exporting your data?


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Are you simply writing a table to the response and changing the content type to "application/ms-excel"? That's the easy way and it should be very portable across versions.

Here's a good resource to check your approach against:


MCP, MCTS - .NET Framework 2.0 Web Applications
 
here is the actual code for the display page.
 
Where?


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
sorry, here it is

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.Data.OracleClient;
using System.Configuration;

namespace Magellen2.SALESHISTORY
{
/// <summary>
/// Summary description for SalesHistoryExcel2.
/// </summary>
public class SalesHistoryExcel2 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Literal ExcelData;
public string Sql = "";
private MagUtils myUtils = new MagUtils();

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
Server.ScriptTimeout = 360 ;
Sql = "select division, cat_seg_1, sum(quantity_invoiced) quantity_invoiced, sum(extended_price) extended_price";
Sql += " from sales_data_info_curr";
Sql += " group by division, cat_seg_1";

BuildExcel();

}


private void BuildExcel()
{
DBConnect.dbUtils dbTools = new DBConnect.dbUtils();
OracleConnection db = dbTools.openDB(ConfigurationSettings.AppSettings["HistConnString"]);
DataSet ds = dbTools.GetDataSet(Sql,db);


//OracleConnection dbCon = myUtils.openAppsDB();
//DataSet ds = myUtils.GetDataSetFromAppsOrPPPP(Sql,"APPS",dbCon);



System.Text.StringBuilder MySB = new System.Text.StringBuilder();
MySB.Append("<TABLE width='100%' border=0 cellspacing=2 cellpadding=2>");
MySB.Append("<TR CLASS=clsBoldText>");
MySB.Append("<TD nowrap><B>Division</B></TD>" );
MySB.Append("<TD nowrap><B>CAT_SEG_1</B></TD>" );
MySB.Append("<TD nowrap><B>QUANTITY_INVOICED</B></TD>");
MySB.Append("<TD nowrap><B>EXTENDED_AMOUNT</B></TD></TR>" );

foreach(DataTable myTable in ds.Tables)
{
if (myTable.Rows.Count > 0 )
{
foreach(DataRow myRow in myTable.Rows)
{
MySB.Append("<TR CLASS=clsText>");
MySB.Append("<TD VALIGN=TOP nowrap>" + myRow["DIVISION"].ToString() + "</TD>");
MySB.Append("<TD nowrap>" + myRow["CAT_SEG_1"].ToString() + "</TD>");
MySB.Append("<TD VALIGN=TOP nowrap>" + myRow["QUANTITY_INVOICED"].ToString() + "</TD>");
MySB.Append("<TD VALIGN=TOP nowrap>" + myRow["EXTENDED_PRICE"].ToString() + "</TD></TR>");

}
}
else//In Case of No Records found
{
MySB.Append("<TR><TD COLSPAN=7 CLASS=clsErrorText>No Records found.</TD></TR>");
}
}
ds.Dispose();
MySB.Append("</TABLE>");
ExcelData.Text = Convert.ToString(MySB.ToString());

db.Close();
db.Dispose();
}


#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
 
then it displays on this page.
code below.

<%@ Page language="c#" Codebehind="SalesHistoryExcel2.aspx.cs" AutoEventWireup="false" Inherits="Magellen2.SALESHISTORY.SalesHistoryExcel2" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

<html>
<head>
<title>SalesHistoryExcel2</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">

<meta name=vs_targetSchema content=" </head>
<body MS_POSITIONING="datagrid">

<asp:literal ID=ExcelData Runat =server></asp:literal>

</body>
</html>
 
Sorry, I must be missing something. I see that you build a String with HTML markup in it but I can't see where you are setting the output type to Excel. How does this page know the data is for excel?


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Response.Clear();
Response.ContentType = "application/vnd.ms-excel";
this.EnableViewState = true;
 
That code isn't in the original code you posted. Please post the full code of all files you are using so we can see exactly what you are doing. Also, please use [ignore]
Code:
[/ignore] tags when posting code.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
heres the entire page, sorry.

Code:
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.Data.OracleClient;
using System.Configuration;

namespace Magellen2.SALESHISTORY
{
	/// <summary>
	/// Summary description for SalesHistoryExcel2.
	/// </summary>
	public class SalesHistoryExcel2 : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Literal ExcelData;
		public string Sql = "";
		private MagUtils myUtils = new MagUtils();

		private void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
			Server.ScriptTimeout = 360 ;
			Sql = "select division, cat_seg_1, sum(quantity_invoiced) quantity_invoiced, sum(extended_price) extended_price";
			Sql += " from sales_data_info_curr";
			Sql += " group by division, cat_seg_1";
			
			BuildExcel();

		}


		private void BuildExcel()
		{
			DBConnect.dbUtils dbTools = new DBConnect.dbUtils();
			OracleConnection db = dbTools.openDB(ConfigurationSettings.AppSettings["HistConnString"]);
			DataSet ds = dbTools.GetDataSet(Sql,db);


			//OracleConnection dbCon = myUtils.openAppsDB();
			//DataSet ds = myUtils.GetDataSetFromAppsOrPPPP(Sql,"APPS",dbCon);

			
			Response.Clear();
			Response.ContentType = "application/vnd.ms-excel";
			this.EnableViewState = false;

			System.Text.StringBuilder MySB = new System.Text.StringBuilder();
			MySB.Append("<TABLE width='100%' border=0 cellspacing=2 cellpadding=2>");
			MySB.Append("<TR CLASS=clsBoldText>");
			MySB.Append("<TD nowrap><B>Division</B></TD>" );
			MySB.Append("<TD nowrap><B>CAT_SEG_1</B></TD>" );
			MySB.Append("<TD nowrap><B>QUANTITY_INVOICED</B></TD>");
			MySB.Append("<TD nowrap><B>EXTENDED_AMOUNT</B></TD></TR>" );
			
			foreach(DataTable myTable in ds.Tables)
			{
				if (myTable.Rows.Count > 0 )
				{
					foreach(DataRow myRow in myTable.Rows)
					{
						MySB.Append("<TR CLASS=clsText>");
						MySB.Append("<TD VALIGN=TOP nowrap>" + myRow["DIVISION"].ToString() + "</TD>");
						MySB.Append("<TD nowrap>" + myRow["CAT_SEG_1"].ToString() + "</TD>");
						MySB.Append("<TD VALIGN=TOP nowrap>" + myRow["QUANTITY_INVOICED"].ToString() + "</TD>");
						MySB.Append("<TD VALIGN=TOP nowrap>" + myRow["EXTENDED_PRICE"].ToString() + "</TD></TR>");
						
					}
				}
				else//In Case of No Records found 
				{
					MySB.Append("<TR><TD COLSPAN=7 CLASS=clsErrorText>No Records found.</TD></TR>");
				}
			}
			ds.Dispose();
			MySB.Append("</TABLE>");
			ExcelData.Text = Convert.ToString(MySB.ToString());
			
			db.Close();
			db.Dispose();
		}


		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new System.EventHandler(this.Page_Load);
		}
		#endregion
	}
}


then the next code is the page that opens with the results.
<%@ Page language="c#" Codebehind="SalesHistoryExcel.aspx.cs" AutoEventWireup="false" Inherits="Magellen2.SalesHistoryExcel" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
	<HEAD>
		<title>SalesHistoryExcel</title>
		<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
		<meta name="CODE_LANGUAGE" Content="C#">
		<meta name="vs_defaultClientScript" content="JavaScript">
		<meta name="vs_targetSchema" content="[URL unfurl="true"]http://schemas.microsoft.com/intellisense/ie5">[/URL]
		<script language="javascript">
		function closeWin(){
			/*try {
				self.close()
			}catch(e){}
			*/
		}
		</script>
	</HEAD>
	<body MS_POSITIONING="GridLayout" onUnload="javascript:closeWin()">
		   <asp:Literal ID=ExcelData Runat =server></asp:Literal>
	</body>
</HTML>
 
OK, there doesn't seem to be anything in your code that would stop the page from generating the correct HTML. Can you provide the HTML output that your page generates and we can see if that provides any clues?

Also, please use [ignore]
Code:
[/ignore] tags when posting code.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
thats just it, using office 97 its fine, office 2000, it displays nothing.
 
ca8msm said:
Can you provide the HTML output that your page generates and we can see if that provides any clues?

Also, please use
Code:
tags when posting code.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top