I am trying to create an application that would compare two excel sheets. But to start, when i try opening an excel sheet. I am not able to do it. I am getting the following error. Can anyone help me.
I am getting the following Runtime error."Exception from HRESULT: 0x800A03EC" in the line with yellow background
Excel.XlPlatform.xlWindows
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using Excel=Microsoft.Office.Interop.Excel;
using System.Windows.Forms;
namespace WindowsApplication7
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Browse_Click(object sender, EventArgs e)
{
Excel.Application excelApp = new Excel.ApplicationClass();
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "Excel Files|*.xlsx";
openFileDialog1.Title = "Select the excel file";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string filename = openFileDialog1.FileName;
// MessageBox.Show(filename);
string workbookPath = filename;
Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath, 0, false, 5, Excel.XlPlatform.xlWindows, "", false, "", "", true, false, 0, true, false, false);
//MessageBox.Show("Success");
openFileDialog1.OpenFile();
}
}
}
}
Guest UserPosted May 3, 2011, 2:06 PM
if you want to see the Excel window after you open the workbook you need to add the line:
excelApp.Visible = true;
Guest UserPosted May 3, 2011, 2:03 PM
excelApp.Workbooks.Open(workbookPath, 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, false);
If you need to specify values for those parameters, do so, otherwise leave them at Type.Missing.
2) Remove the openFileDialog1.OpenFile() line. Excel has already opened the file at that point so if you try to open it again you'll get an error reporting that the file is already in use.