Click here to Skip to main content
15,894,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to import data from excel to data set.

I done it by using following code it works when i am runing in localhost.

But It is showing error while running in IIS or any Hosting.

Error:


System.InvalidOperationException: GetOleDbSchemaTable requires an open and available Connection. The connection's current state is closed. at System.Data.OleDb.OleDbConnection.CheckStateOpen(String method) at System.Data.OleDb.OleDbConnection.GetOleDbSchemaTable(Guid schema, Object[] restrictions)


My code as follows


public DataSet GetExcel(string fileName)
    {
        Application oXL;
        Workbook oWB;
        Worksheet oSheet;
        Range oRng;
        try
        {    
            //  creat a Application object    
            oXL = new ApplicationClass();    
            //   get   WorkBook  object    
            oWB = oXL.Workbooks.Open(fileName, Missing.Value, Missing.Value,
                Missing.Value, Missing.Value, Missing.Value,
                Missing.Value, Missing.Value, Missing.Value,
                Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                Missing.Value, Missing.Value);    
            //   get   WorkSheet object   
            oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oWB.Sheets[1];  
            System.Data.DataTable dt = new System.Data.DataTable("dtExcel");  
            DataSet ds = new DataSet();   
            ds.Tables.Add(dt);  
            DataRow dr;  
            StringBuilder sb = new StringBuilder();   
            int jValue = oSheet.UsedRange.Cells.Columns.Count;   
            int iValue = oSheet.UsedRange.Cells.Rows.Count;   
            //  get data columns   
            for (int j = 1; j <= jValue; j++)  
            {        dt.Columns.Add("column" + j, System.Type.GetType("System.String"));   
            }   
            //  get data in cell  
            for (int i = 1; i <= iValue; i++)   
            {        dr = ds.Tables["dtExcel"].NewRow();        
                for (int j = 1; j <= jValue; j++)   
                {          
                    oRng = (Microsoft.Office.Interop.Excel.Range)oSheet.Cells[i, j];  
                    string strValue = oRng.Text.ToString();         
                    dr["column" + j] = strValue;    
                }      
                ds.Tables["dtExcel"].Rows.Add(dr); 
            }  
            return ds;
        }
        catch (Exception ex)
        {
            lbl_Result.Text = lbl_Result.Text + ex.ToString(); ;
                return null;
            }
        finally
        {   
            Dispose();
        }
    }


Please Tell me Where I am Doing Worng?
Thanks In advance
Posted
Comments
murali0808 29-May-10 1:35am    
I am Getting the Following error while Running in webserver or IIS

It is Fine on My LocalHost


System.Runtime.InteropServices.COMException (0x800A03EC):

The built in Excel Object that allows you to access and manipulate excel files requires that excel be installed on the machine you are using it on. If your server doesn't have excel installed on it then you won't be able to use the Excel object. Also, Microsoft doesn't recommend installing excel on the server for this use since it is single threaded and has other issues associated with it. I personally have had a lot of trouble trying to make a program like this work on the server.(It is possible though) The company I work for ended up buying an excel api that doesn't require excel to be installed on the machine.


If you would like to discuss this further I'd be more than glad to.
 
Share this answer
 
v2
Comments
Manjunath Nayak 12-Aug-13 3:26am    
excel(office 2000) is not working on IIS 8, without IIS8 it i working.. getting error as RPC failed...
murali0808 29-May-10 0:41am    
Actually I got the problem even in Excel installed machine also..
GregWyatt 29-May-10 19:56pm    
yeah there are a lot of tweaks to make it work even if excel is installed. I also had it get into a fail state quite often, and the only fix I had was to restart the server.
GregWyatt 29-May-10 19:57pm    
I don't remember entirely but I think you have enable various settings to let it run.
check your Application Pool -> Advanced Setting -> enable 32 Applications -> True
 
Share this answer
 
Comments
umeshfaq 9-May-13 16:21pm    
Thanks a lot ,It is work for me.
Member 14643468 3-Nov-19 23:50pm    
Thank you so much, it work for me too.
sushils943 18-Nov-19 14:50pm    
thanks brother, it saved my life.God bless :)
Murali,

You must be passing a wrong filename, check how you are sending the filename, is it a physical path or virtual path. Post the code from where you are calling this method.
 
Share this answer
 
Comments
Manjunath Nayak 12-Aug-13 3:29am    
excel(office 2000) is not working in IIS 8..
protected void Expgo_Click(object sender, EventArgs e)
{
/*************************************
* On export click, select date range
* Generate weekley TimeSheet Report
************************************/

QueryRet Value = new QueryRet();
String StartCalDate;
String EndCalDate;
StartCalDate = ExpCalStart.SelectedDate.ToString();
EndCalDate = ExpCalEnd.SelectedDate.ToString();

if (StartCalDate == EndCalDate)
{
Weekview.SetActiveView(Blank);
lblerr.Text = "Please Select Range of Dates.";
lblerr.Visible = true;
}
else
{
ExcelApp = new Excel.Application();
ExcelApp.Visible = false;
ExcelWork = (Excel._Workbook)(ExcelApp.Workbooks.Add(Missing.Value));
string strCurrentDir = Server.MapPath("~/TimeSheets/");
RemoveFiles(strCurrentDir);//removes all .xls files in directory
DateTime Daylist;
String Query;

/********************************************************
* Select all WeekStartDates in the given range of dates
********************************************************/
Query = "SELECT DISTINCT WeekStartDate FROM TimeSheetBreakDown";
Query += " Where Date >= '" + StartCalDate + "' AND Date<='" + EndCalDate + "'";
ArrayList DistinctStart = Value.ExecuteQry(Query);
if (ExcelWork.Worksheets.Count <= DistinctStart.Count)
{
ExcelWork.Worksheets.Add(Missing.Value, Missing.Value, DistinctStart.Count - ExcelWork.Worksheets.Count + 1, Missing.Value);
}
for (int s = 0; s < DistinctStart.Count; s++)
{
/******************************************************
* For each of the week which come under date selected
* Generate One Excel Work Sheet
******************************************************/
try
{
Query = "SELECT distinct Date FROM TimeSheetBreakDown WHERE (WeekStartDate = '" + DistinctStart[s] + "' AND Day='1') order by date";
Daylist = DateTime.Parse(Value.SelectQry(Query));
/******************************************************************
* From the Distinct StartDate Get (Date of) Monday of that week
* From Monday Calculate other Days From TimeSpan
* ****************************************************************/
mon = Daylist;
tue = Daylist + new TimeSpan(1, 0, 0, 0);
wed = Daylist + new TimeSpan(2, 0, 0, 0);
thu = Daylist + new TimeSpan(3, 0, 0, 0);
fri = Daylist + new TimeSpan(4, 0, 0, 0);
sat = Daylist + new TimeSpan(5, 0, 0, 0);
sun = Daylist + new TimeSpan(6, 0, 0, 0);
}
catch (Exception X)
{
lblerr.Text = X.Message + " Looks like you are Exporting Blank TimeSheet";
lblerr.Visible = true;
}
finally
{
GC.Collect();
/**************************************************************
* Generates one Excel WorkSheet (Gives TimeSheet of that Week)
**************************************************************/
GenerateExcel(s + 1);
}
}
DistinctStartCount = DistinctStart.Count + 1;
SummarySheet();
ExcelApp.Visible = true;
ExcelApp.UserControl = true;
String FileName = "" + User.Identity.Name + ".xlsx";
ExcelWork.SaveC
murali0808 28-May-10 13:10pm    
protected void btn_Uplaod_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(file_UploadXL.PostedFile.FileName))
{
string strFilePath;
// strFilePath ="C:\Documents and Settings\jmuralimohan\Desktop\DataSample2.xls";
strFilePath=file_UploadXL.PostedFile.FileName;
lbl_Result.Text = strFilePath;
DataSet ds = new DataSet();
try
{
ds = GetExcel(strFilePath);
grid_UpLogSheets.DataSource = ds.Tables[0];
grid_UpLogSheets.DataBind();
}
catch(Exception EX)
{
lbl_Result.Text = lbl_Result.Text + EX.ToString();
}
}
}

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900