Click here to Skip to main content
15,923,374 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am using Microsoft.Interop dll Version 14.
OLEDB Connection string 12.0

My code,
C#
try
            {
                string connStr = CommonConstants.ExcelConnectionString.Replace("FILENAME", InputFileWithPath);
                Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
                Microsoft.Office.Interop.Excel.Workbook wb = null;
                excel.Visible = false;
                string SheetName = string.Empty;
                try
                {
                    object missing = System.Reflection.Missing.Value;
                    wb = excel.Workbooks.Open(InputFileWithPath, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
                    foreach (Microsoft.Office.Interop.Excel.Worksheet sheet in wb.Sheets)
                    {
                        SheetName = sheet.Name + "$";
                        break;
                    }
                    excel.Quit();
                }
                finally
                {
                    if (excel != null)
                    {
                        excel.Quit();
                        ClsCommon.ExcelClose(excel);
                    }
                }
                using (conn = new OleDbConnection(connStr))
                {
                    conn.Open();
                    System.Data.DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                    String[] excelSheets = new String[dt.Rows.Count];
                    adapter = new OleDbDataAdapter("SELECT F1,F3,F5,F7,F8,F9,F10,F14,F16,F17,F18 FROM [" + SheetName + "]", conn);
                    adapter.Fill(ClsParameter.dtInputFile);
                    conn.Close();

                    for (int j = 0; j < 10; j++)
                    {
                        ClsParameter.dtInputFile.Rows.RemoveAt(0);
                    }
                    ClsParameter.dtInputFile.AcceptChanges();

                }

            }
            catch (Exception ex)
            {
                ClsParameter.TotalErrorCount++;
                ClsLog.WriteLogFile("Error while Reading Input File. Error : " + ex.Message.ToString(), CommonConstants.LogType_Error);
                return false;
            }
            finally
            {
                if (conn != null && conn.State == ConnectionState.Open)
                {
                    conn.Close();
                    conn.Dispose();
                    conn = null;
                    System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName("Excel");
                    foreach (System.Diagnostics.Process p in process)
                    {
                        if (!string.IsNullOrEmpty(p.ProcessName))
                        {
                            try
                            {
                                p.Kill();
                            }
                            catch { }
                        }
                    }
                }

            }

When i am trying to run the same on system having Microsoft Office 2013 i am getting error as described below :

Exception from HRESULT: 0x800AC472.

I have installed 2007 office System Data Drivers but it dint work.

Kindly help.
Posted
Updated 28-Apr-15 20:03pm
v2

1 solution

You need to install the right interop assmeblies on the machine - https://msdn.microsoft.com/en-us/library/15s06t57.aspx[^]. If you are use interop version 14 then I guess you need to install interop for office 2010 and not office 2007.
 
Share this answer
 
v2

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