473,382 Members | 1,329 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Cannot work with Excel component

Hi life saver,
I am using excel component for the development.
The purpose is to connect to excel through the odbc
connection string. Then through the connection extract
data into a dataset and then save the data into a client
machine (Intranet) as excel page using the excel
component (using Excel = Microsoft.Office.Interop.Excel;)
in the code.

Please help me. I have wasted so many days on this.

I have 2 problems here
1. When I establish connection to the excel page I am
getting hell of problems like unspecified errors and dies

public void btnConnect_Click(object
sender, System.EventArgs e)
{
if
(this.ddlFileName.SelectedValue != "None")
{
try
{
sFile =
Server.MapPath(Request.ApplicationPath) + "\\Data\\" +
this.ddlFileName.SelectedItem;
DataTable dt =
new DataTable("TablesList");
if
(this.ddlFileName.SelectedValue.EndsWith(".xml"))
{

dsTables.ReadXmlSchema(sFile);

dsTables.ReadXml(sFile);
for (int
iTab = 0;iTab < dsTables.Tables.Count; iTab++)
{

if (lbxTableList.Items.FindByText(dsTables.Tables
[iTab].TableName.ToString()) == null)
{

this.lbxTableList.Items.Add(dsTables.Tables
[iTab].TableName.ToString());
}
}
strFFlag
= "xml";
}
else if
(this.ddlFileName.SelectedValue.EndsWith(".xls"))
{
<<Dies here: unspecified error>>
string[] SheetName =
LoadSchemaFromFile(sFile);
for (int
i=0;i<SheetName.Length;i++)
{

this.lbxTableList.Items.Add(SheetName[i].ToString
());
}
strFFlag
= "xls";
}
else if
(this.ddlFileName.SelectedValue.EndsWith(".mdb"))
{
string[]
SheetName = LoadSchemaFromFile(sFile);
for (int
i=0;i< SheetName.Length;i++)
{

this.lbxTableList.Items.Add(SheetName[i].ToString
());
}
strFFlag
= "mdb";
}
else
{

this.Msg.Text = "Error getting tables list :-(";
return;
}
if (Cache
["strFFlagCc"] != null)
{

Cache.Remove("strFFlagCc");
}
Cache.Add
("strFFlagCc", strFFlag,null,DateTime.Now.AddSeconds(90),
TimeSpan.Zero ,
System.Web.Caching.CacheItemPriority.High, null);

}
catch
(ApplicationException sqlExc)
{
this.Msg.Text =
sqlExc.ToString() + "\n" + "SQL Exception Error!";
return;
}
}
else
{
this.Msg.Text = "Please
select source for data access";
return;
}

}
private string[] LoadSchemaFromFile
(string fileName)
{
string[] SheetNames = null;
OleDbConnection conn = new
OleDbConnection();
conn.ConnectionString
= "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
fileName + "; Jet OLEDB:Engine
Type=5;";//this.ReturnConnection(fileName);

try
{

conn.Open();

DataTable SchemaTable =
conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables ,new
object[]{null});
//conn.GetOleDbSchemaTable
(OleDbSchemaGuid.Tables,new object[] {null, null,
null, "TABLE"});

if
(SchemaTable.Rows.Count > 0)
{
SheetNames = new
string[SchemaTable.Rows.Count];
int i=0;
foreach(DataRow
TmpRow in SchemaTable.Rows)
{
SheetNames
[i] = TmpRow["TABLE_NAME"].ToString(); // string.Format
("{0}",myRow["TABLE_NAME"]);
i++;
}

}
conn.Close();
}
catch
(System.Data.OleDb.OleDbException e)
{
Msg.Text = "Error code= "
+ e.ErrorCode.ToString("X")+"\nError Description=" +
e.Message +"\n Error Source=" + e.InnerException + "\n
Error Method=" + e.TargetSite + "\n =" +
e.Errors;//,TitleText);
Trace.WriteLine
(e.StackTrace);
}
finally
{
if ( conn!= null &&
conn.State == ConnectionState.Open)
{
conn.Close ();
conn.Dispose();
}
}
return SheetNames;

}

private OleDbConnection ReturnConnection
(string fileName)
{
//if(checkBox2.Checked) //First
row as column name
//{
if (fileName.EndsWith(".xls"))
{
return new OleDbConnection
("Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" +
fileName + ";" +
"Extended
Properties=\"Excel 8.0;\"");// Jet OLEDB:Engine Type=5;"
}
else if (fileName.EndsWith
(".mdb"))
{

//"Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=D:\\BIRoot\\WebClient\\Data\\Northwind.mdb"
return new OleDbConnection
("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
fileName + "; Jet OLEDB:Engine Type=5;");
}
else
{
return new OleDbConnection
("Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" +
fileName + "; Jet OLEDB:Engine Type=5;");
}
//}
//else
//{
// return new OleDbConnection
("Provider=Microsoft.Jet.OLEDB.4.0;" +
// "Data Source=" +
fileName + "; Jet OLEDB:Engine Type=5;"+
// "Extended
Properties=\"Excel 8.0;HDR=NO\"");
//}
}

2. I have data from a dataset and want to pump into an
excel sheet and the component activates some time and
works but doesn't work some time. (from some application
projects it works some doesn't).
I am using windows XP for development.
I have already gone to START>>SETTINGS>CONTROL
PANEL>>ADMIN TOOLS>>COMPONENT SERVICES>>
COMPONENT SERVICES MENU>>MY COMPUTER>> DCOM
CONFIG>>MICROSOFT EXCEL APPLICATION>>SECURITY TAB>>ADDED
ALL THREE (lAUNCH,ACCESS,CONFIGURATION PERMISSIONS)
ADDED MY LOGIN AND .NET PERMITTED TO ACCESS

still when I tried to open the application it just hangs
in there!!!

Below is a templated excel file creation from dataset.
Below code worked 3 days ago now it doesn't work.
Hangs at the code line of creation of component instance

public void btnSaveXl_Click(object
sender, System.EventArgs e)
{
<<hangs here>> Excel.Application oExcel
= new Excel.ApplicationClass();

string sFileXl = Server.MapPath
(Request.ApplicationPath) + "\\Data\\MyExcel2.xls";
string sTemplate = Server.MapPath
(Request.ApplicationPath) + "\\Data\\MyTemplate.xls";

oExcel.Visible = false;
oExcel.DisplayAlerts = false;

//Start a new workbook
Excel.Workbooks oBooks =
oExcel.Workbooks;
//Change the language culture
info to match the office culture else you get error
oBooks.Open
(sTemplate,0,true,5,"","",true,Excel.XlPlatform.xl Windows,
"\t",false,false,0,true,false,false);

//Load colorful template with
chart
Excel.Workbook oBook =
oBooks.get_Item(1);
Excel.Sheets oSheets =
oBook.Worksheets;
Excel.Worksheet oSheet =
(Excel.Worksheet)(oSheets.get_Item(1));
oSheet.Name = "First Sheet";
Excel.Range oCells = oSheet.Cells;

DataTable dt = ds.Tables[0];
DumpData(dt, oCells); //Fill in
the data
oSheet.SaveAs(sFileXl,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value,Missing.Value,
Missing.Value,
Missing.Value, Missing.Value, Missing.Value); //Save in
a temporary file
//oBook.Close
(true,"MyExcel.xls",Missing.Value);
//Quit Excel and thoroughly
deallocate everything
oExcel.Quit();
System.GC.Collect();
Response.Redirect
(sFileXl); //Send the user to the file
}

//Outputs a DataTable to an Excel
Worksheet
private string DumpData
(System.Data.DataTable dt, Excel.Range oCells)
{
int iRow;
int iCol;

//Output Column Headers
for (iCol=0; iCol <
dt.Columns.Count;iCol++)
{
oCells.Cells.set_Item(2,
iCol + 1,dt.Columns[iCol].ToString());
}

//Output Data
for (iRow=0;iRow <
dt.Rows.Count;iRow++)
{
DataRow row;
row = dt.Rows[iRow];
for (iCol = 0; iCol <
dt.Columns.Count; iCol++)
{
DataColumn col;
col = dt.Columns
[iCol];
{

oCells.Cells.set_Item(iRow + 3, iCol + 1,row
[col].ToString());

Response.Write(oCells.Cells.get_Item(iRow + 3,
iCol + 1).ToString() + "\t");
}

}
}
return "";
//byte[] info = new UTF8Encoding
(true).GetBytes(value);
//fs.Write(info, 0, info.Length);

}

Nov 15 '05 #1
0 2795

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Frank X | last post by:
Excel 2002 introduced a capability to add custom worksheet functions to Excel direct from a COM/ActiveX object. I can use C# to develop a COM object which I can use fine from Excel/VBA, however...
6
by: Steve Richter | last post by:
I am getting error in a vbscript: ActiveX component cant create object: Excel.Application. The vbscript code is: Dim objExcel Set objExcel = CreateObject("Excel.Application") I am pretty...
2
by: Alex D. | last post by:
Hi .I cant seem to find a way to authorize asp.net user to create an Excel application using this: Microsoft.Office.Interop.Excel.ApplicationClass excel = new...
4
by: Patrick | last post by:
I have a middle tier object that generates Excel spreadsheet here is the code moExcel = CreateObject("Excel.Application") moWorkBook = moExcel.Workbooks.Add moSheet =...
4
by: Irfan | last post by:
hi, Two questions, i dont know if the cause if these two problems is the same. 1. As soon as i Add->Reference -> Microsoft Excel 11.0 Object library. It gives an error 'The referenced...
0
by: Thanks | last post by:
I would like to output a dataset to excel from a text file but did not work. I alreay add the microsoft active component 2.7 and 2.8 and microsoft excel 11 component library but no luck. Please...
3
by: Administrator | last post by:
Hi, My web development server is a Windows 2000 SP4 system. Had created an aspx page with the following code which I had extracted from 1 of the Microsoft websites: Dim oExcel As Object Dim...
1
by: HiSam | last post by:
Hi I am using CreateObject("Application.Excel") in my class which is deployed as an exe on server which basically reads the excel file and creates the xml of it. Few days back it was working...
1
by: mahesh123 | last post by:
Hi Folks, I am getting an Error "Activex Component Cannot Create Object" While I am exporting the Data from the MSFlexgrid to the MS Excel. Here In my Client System MSOffice is not Installed....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.