简体   繁体   English

vsto:使用C#在相同的解决方案中从Windows窗体应用程序迁移不同的excel工作簿

[英]vsto: Caling different excel workbooks from windows form application in same solutions using c#

I have 3 projects consists of one windows form application(WFA) and two excel workbooks in 1 solutions.. 我有3个项目,其中一个解决方案包含一个Windows窗体应用程序(WFA)和两个Excel工作簿。

I would like to call/open both the workbooks by clicking different buttons in the WFA from the same solutions.. And I am stuck on the coding.. Have no idea how to code to open, activate, ...etc 我想通过单击来自同一解决方案的WFA中的不同按钮来调用/打开这两个工作簿。而且我被困在编码上了。不知道如何编写代码以打开,激活等

Anyone have any idea can help? 任何人有什么想法可以帮助您吗?

*ps: I am very new to vsto/c#.net * ps:我对vsto / c#.net非常陌生

I tried this.. But the workbook does not open.. 我试过了..但是工作簿没有打开..

private void Button_Click(object sender, EventArgs e)
{
     Microsoft.Office.Interop.Excel.Application xlApp;
     Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
     Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
     object misValue = System.Reflection.Missing.Value;

     xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
     xlWorkBook = xlApp.Workbooks.Open(@"C:\Document\PROJECT\abc.xlsx");
     xlWorkSheet = (Microsoft.Office.Interop.Excel.xlWorkSheet)xlWorkBook.Worksheets.get_Item(1);

     ((Microsoft.Office.Interop.Excel._Worksheet)xlWorkBook).Activate();

     xlWorkBook.Close(true, misValue, misValue);
     xlApp.Quit(); 
    }

Simple tweak on your code worked 简单调整代码即可正常工作

Microsoft.Office.Interop.Excel.Application xlApp;
            Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
            Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
            object misValue = System.Reflection.Missing.Value;

            xlApp = new Microsoft.Office.Interop.Excel.Application();
            xlApp.Visible = true;
            xlWorkBook = xlApp.Workbooks.Open(@"C:\Users\knm\Documents\Book2.xlsx");
            xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            xlWorkSheet.Activate();


            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();

VB.net VB.net

   Private Sub Button1_Click(sender As System.Object, e As    Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles Button1.Click
    Dim Dirdata As String = "\\drive\folder\Template.xlsb"

    Dim xlApp As Microsoft.Office.Interop.Excel.Application
    Dim xlWorkbook As Microsoft.Office.Interop.Excel.Workbook
    Dim xlWorksheet  as Microsoft.Office.Interop.Excel.Worksheet
    xlApp = New Microsoft.Office.Interop.Excel.Application()
    xlWorkbook = xlApp.Workbooks.Open(Dirdata, , True, , "password")
    xlApp.Visible = True

    xlWorksheet = xlWorkbook.Worksheets(1)
    xlWorksheet.Activate()

    MsgBox(xlWorkbook.Name)

    xlWorkbook.Close(False)

End Sub

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

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