简体   繁体   English

使用C#打开Excel文件usint Interop

[英]Open Excel file usint Interop using C#

I have this code in C# y used to open a Excel file. 我在C#中有此代码,用于打开Excel文件。

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. 我没有做任何更改,现在在打开excel文件的行中出现错误。 First I see a message "Excel is trying to recover information" 首先,我看到一条消息“ Excel正在尝试恢复信息”

And after that I get the exception "Error in remote procedure call. (Excepción de HRESULT: 0x800706BE)" 然后,我得到异常“远程过程调用中的错误。(Excepciónde 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 http://csharp.net-informations.com/excel/csharp-open-excel.htm

Looks like RPC service down. 看起来像RPC服务关闭。 Are you insure that service "Remote Procedure Call (RPC)" here "Control Panel\\Administrative Tools\\Services" have a "Running" state ? 您是否确保“控制面板\\管理工具\\服务”此处的“远程过程调用(RPC)”服务具有“正在运行”状态?

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 http://csharp.net-informations.com/excel/csharp-excel-oledb.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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