简体   繁体   English

Excel Interop“新建”对象

[英]Excel Interop “New Up” objects

I'm working on an MVC application where users can upload an excel document and output HTML but I'm having some difficulty with the Interop libraries and I hope someone can help. 我正在开发一个MVC应用程序,用户可以在其中上载excel文档并输出HTML,但是我在使用Interop库时遇到了一些困难,希望有人能提供帮助。

To keep this really simple, I have reached a stage where I want to instantiate or "new up" a worksheet and use the id of the chosen sheet. 为了使这一过程非常简单,我已经达到了一个要实例化或“新建”工作表并使用所选工作表的ID的阶段。 The user has selected the sheet they wish to use but there doesn't seem to be any method of doing this. 用户已经选择了他们希望使用的工作表,但是似乎没有任何方法可以这样做。

I have tried code such as workbookModel.workbook.Sheets.get_Item(1); 我尝试过诸如workbookModel.workbook.Sheets.get_Item(1);代码workbookModel.workbook.Sheets.get_Item(1); which should get the current workbook in the model and get the sheet with id 1 - I believe this is 1 based index as opposed to 0-based. 这应该获取模型中的当前工作簿并获取ID为1的工作表-我认为这是基于1的索引,而不是基于0的索引。 Bear in mind I have some viewModels in play hence above code, but all I'm really doing here is Workbook work = new Workbook().Sheets.get_Item(1); 请记住,因此上面的代码中有一些viewModels在起作用,但是我在这里真正要做的只是Workbook work = new Workbook().Sheets.get_Item(1);

I have also tried Workbook work = new Workbook().Worksheets.get_Item(1); 我也尝试过Workbook work = new Workbook().Worksheets.get_Item(1); in case I was incorrectly using Worksheets instead of sheets but I don't believe so. 万一我不正确地使用工作表而不是工作表,但我不这么认为。

But nothing I've tried seems to work. 但是我尝试过的任何方法似乎都无效。

If anyone can point me in the right direction that will be much appreciated and if this hasn't made sense and you need more info please let me know. 如果有人可以向我指出正确的方向,将不胜感激,如果这没有任何意义,并且您需要更多信息,请告诉我。

Thanks a lot 非常感谢

First of all, Interop only works if you have an excel application running. 首先,Interop仅在运行excel应用程序时才有效。

So, if you don't have it running, you will need to: 因此,如果您没有运行它,则需要:

Application ExcelApp = new Application();

To get a worksheet from the application, you will need the workbook first. 要从应用程序获取工作表,首先需要工作簿。 You can get the workbook with one of the following: 您可以使用以下之一获取工作簿:

//to get the first workbook of an application
Workbook MyBook = ExcelApp.Workbooks[1];

//to get a workbook by name
Workbook MyBook = ExcelApp.Workbooks["bookname"];

//to open a workbook from a xls file
Workbook MyBook = ExcelApp.Workbooks.Open(name and other parameters);

//to add a new workbook to the application
Workbook MyBook = ExcelApp.Workbooks.Add(optional template);

Note that a workbook cannot exist without the running application, nor a sheet can exist without a parent workbook in a running application 请注意,没有正在运行的应用程序,工作簿将不存在,而没有父工作簿的工作表也将不存在。

To get a worksheet from a workbook, you will need to cast it, because it comes as object . 要从工作簿中获取工作表,您需要对其进行强制转换,因为它as object

//to get an existing sheet
Worksheet MySheet = (Worksheet)MyBook.Worksheets["sheetname"];

//to create a new sheet
Worksheet MySheet = (Worksheet)MyBook.Worksheets.Add(some parameters);

But if you could manage to get the names from the sheets to populate the view with links, you could store those sheet instances and pass them to the method that creates the html. 但是,如果您可以设法从工作表中获取名称以使用链接填充视图,则可以存储这些工作表实例并将其传递给创建html的方法。 Or store them in global variables. 或将它们存储在全局变量中。

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

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