简体   繁体   English

打开模板文件的多个副本(.Net,Excel.Interop)

[英]Open multiple copies of template file (.Net, Excel.Interop)

I have a program where the user selects some data and the program then opens and populates a template Excel Workbook saved in the program directory for the user to then be able to modify further, write to a DB, etc. 我有一个程序,用户在其中选择一些数据,然后程序打开并填充保存在程序目录中的模板Excel工作簿,供用户随后进行进一步修改,写入数据库等。

The code looks something like this: 代码看起来像这样:

Dim ExcelApp As New Microsoft.Office.Interop.Excel.Application
Dim ExcelWorkbook As Microsoft.Office.Interop.Excel.Workbook = Nothing
...
ExcelWorkbook = ExcelApp.Workbooks.Open(PathToTemplateSheet)
...
ExcelWorkbook.SaveAs(Filename:=TempFileNameToSave)

By saving the workbook to a temp location, I'm ensured that the user doesn't over-write the template, but this still creates issues for me since no 2 users can work on this workbook at the same time since the workbook, TempFileNameToSave , will already be open by the other user. 通过将工作簿保存到临时位置,可以确保用户不会覆盖模板,但这仍然为我带来了问题,因为自工作簿TempFileNameToSave以来,没有2个用户可以同时在此工作簿上工作。 ,已经由其他用户打开。

My question is what is the best way to be able to open multiple copies of the template sheet without creating any problems for the users / over-writing an old one in case they want it? 我的问题是,打开模板表的多个副本而不给用户造成任何问题/在需要时覆盖旧文件的最佳方法是什么?

My ideal solution would be to just give the user a new, unsaved workbook only open on their desktop and then they can save it whereever they want / not save it at all if they don't want to. 我理想的解决方案是仅向用户提供一个仅在其桌面上打开的未保存的新工作簿,然后他们可以将其保存在任何需要的地方/根本不需要保存。

I imagine I could do that by: 我想我可以通过以下方式做到这一点:

  1. Creating a new, blank workbook: ExcelApp.Workbooks.Add 创建一个新的空白工作簿: ExcelApp.Workbooks.Add
  2. Opening the template as above 如上所述打开模板
  3. Copying all the worksheets from one to the other 将所有工作表从一个复制到另一个
  4. closing the template 关闭模板

But that would be very processor intensive if the template is of any decent size and I'm hoping there is a nicer way to accomplish this. 但是,如果模板的大小合适,那将占用大量处理器,我希望有一种更好的方法来完成此任务。

Thanks!! 谢谢!!

For those of you who come across the same issue, I figured out how to do what I wanted. 对于那些遇到相同问题的人,我想出了如何做自己想要做的事情。

The trick for me was to save my original template sheet literally as an excel template ( .xlst ). 对我来说,诀窍是字面上救我的原始模板板作为Excel模板( .xlst )。 Then, all that I needed to change was to change this line of code: 然后,我需要更改的就是更改以下代码行:

ExcelWorkbook = ExcelApp.Workbooks.Open(PathToTemplateSheet)

To: 至:

ExcelWorkbook = ExcelApp.Workbooks.Add(PathToTemplateSheet)

What this does is it creates a new workbook based upon the template and so can be created as many times as needed and never causing the actual, original template file to be opened by a specific user. 这是根据模板创建一个新的工作簿,因此可以根据需要创建多次,而不会导致特定用户打开实际的原始模板文件。

Hope this helps others too!! 希望这也能帮助其他人!

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

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