-1

I am creating an application in which user has to upload the excel file, using below code i am validating the file whether it is valid or corrupted. For this i am using mircrosoft.Office.interop.excel dll. In below code, I am getting exception " Exception from HRESULT: 0x800A03EC"

static void openfile()
        {
            try
            {
                string mySheet = @"E:\7.xlsx";
                var excelApp = new Excel.Application();
                excelApp.Visible = true;
                Excel.Workbooks books = excelApp.Workbooks;
                Excel.Workbook sheet = books.Open(mySheet);
            }

            catch (Exception ex)
            {

                Console.WriteLine(ex.Message);
            }



        }


        static void Main(string[] args)
        {
            openfile();
        } 

What is the issue? Could you please help on this issue?

Sandeep Kushwah
  • 590
  • 1
  • 18
  • 35
Bheeshamteche
  • 47
  • 4
  • 9

2 Answers2

0

Try to use this instead:

string mySheet = @"E:\7.xlsx";
var excelApp = new Excel.Application();
excelApp.Visible = true;
Excel.Workbook sheet = excelApp.Workbooks.Open(mySheet,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, true);

I guess your file E:\7.xlsx is somewhat corrupt. Also you can try to open it normally and see what happens.

But if you just want to know, whether the file is corrupt or not, this exception gives you the information: yes, the file is corrupt.

Vojtěch Dohnal
  • 7,867
  • 3
  • 43
  • 105
0

open console project and Try this... This is working properly.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using Microsoft.Office.Interop.Excel;

namespace ConsoleApplication1
{
class Program
{
    static void Main(string[] args)
    {

        try
        {
            string mySheet = @"E:\7.xlsx";
            Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
            excelApp.Visible = true;
            Workbook wb = excelApp.Workbooks.Open(mySheet);
        }
        catch (Exception ex)
        {

            Console.WriteLine(ex.Message);
        }

        Console.ReadLine();
    }
 }
 }
Nalaka
  • 1,165
  • 7
  • 12