简体   繁体   中英

Open Excel file usint Interop using C#

I have this code in C# y used to open a Excel file.

Excel.Application oXL = new Excel.Application();
Excel._Workbook oWB;

String filename = "C:\\plantilla2.xlsx";

oWB = oXL.Workbooks.Open(filename, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);

MessageBox.Show("FIN");

This code worked fine two days ago. I have not changed anything and now I get an error in the line where opens the excel file. First I see a message "Excel is trying to recover information"

And after that I get the exception "Error in remote procedure call. (Excepción de HRESULT: 0x800706BE)"

I don't know what is going wrong. I have checked the references as explained in this web. http://csharp.net-informations.com/excel/csharp-open-excel.htm

Looks like RPC service down. Are you insure that service "Remote Procedure Call (RPC)" here "Control Panel\\Administrative Tools\\Services" have a "Running" state ?

I don't know what has happened. I have restarted the computer and now it works.

This should help.

using System;
using System.Drawing;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel; 

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                System.Data.OleDb.OleDbConnection MyConnection ;
                System.Data.DataSet DtSet ;
                System.Data.OleDb.OleDbDataAdapter MyCommand ;
                MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='c:\\csharp.net-informations.xls';Extended Properties=Excel 8.0;");
                MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection);
                MyCommand.TableMappings.Add("Table", "TestTable");
                DtSet = new System.Data.DataSet();
                MyCommand.Fill(DtSet);
                dataGridView1.DataSource = DtSet.Tables[0];
                MyConnection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show (ex.ToString());
            }
        }
   }
}

See this link.

http://csharp.net-informations.com/excel/csharp-excel-oledb.htm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM