如何改进代码(C#)以打开指定的excel文件而不仅仅是excel应用程序
我对这个很陌生。我想创建一个Microsoft Excel加载项,用于打开特定的Excel文件并写入一些数据。问题是我的代码只打开excel应用程序,而不是指定的文件 这是我的密码:如何改进代码(C#)以打开指定的excel文件而不仅仅是excel应用程序,c#,excel,vsto,office-addins,excel-addins,C#,Excel,Vsto,Office Addins,Excel Addins,我对这个很陌生。我想创建一个Microsoft Excel加载项,用于打开特定的Excel文件并写入一些数据。问题是我的代码只打开excel应用程序,而不是指定的文件 这是我的密码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Linq; using Excel = Microsoft.Office.Interop.Excel; usi
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Excel;
namespace ExcelAddInTrial
{
public partial class ThisAddIn
{
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
xlWorkBook = xlApp.Workbooks.Open(@"C:\Users\User\Desktop\Projects\Microsoft\B1.xlsx");
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
#region VSTO generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml.Linq;
使用Excel=Microsoft.Office.Interop.Excel;
使用Office=Microsoft.Office.Core;
使用Microsoft.Office.Tools.Excel;
命名空间卓越附加中心
{
公共部分类ThisAddIn
{
私有void ThisAddIn_启动(对象发送方,System.EventArgs e)
{
Excel.applicationxlapp;
Excel.工作簿;
xlWorkBook=xlApp.Workbooks.Open(@“C:\Users\User\Desktop\Projects\Microsoft\B1.xlsx”);
}
私有void ThisAddIn_关闭(对象发送方,System.EventArgs e)
{
}
#区域VSTO生成的代码
///
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
///
私有void InternalStartup()
{
this.Startup+=new System.EventHandler(ThisAddIn\u启动);
this.Shutdown+=new System.EventHandler(ThisAddIn\u Shutdown);
}
#端区
}
}
我知道一些python,但C#肯定是另一个挑战
谢谢你的帮助 您声明了一个
Excel.xlApp应用程序代码>但不要初始化它。CLR不知道什么是xlApp
行xlWorkBook=xlApp.Workbooks.Open(@“C:\Users\User\Desktop\Projects\Microsoft\B1.xlsx”)代码>
替换Excel.Application xlApp代码>带有Excel.Application xlApp=应用程序代码>要写入Excel文件,可以使用OfficeOpenXmlI之类的库。我正在Visual Studio中使用VSTO,是否也可以在其中集成它?抱歉,如果这是一个愚蠢的问题:pI不知道VSTO,但在我看来,它有另一个目的,而不是你试图实现的。它旨在创建将Excel作为应用程序的解决方案,但您只需读取Excel文件,一个简单的库就足够了