简体   繁体   English

如何将模板工作表从Excel文件复制到活动工作簿

[英]How to copy templated sheet from excel file to active workbook

I'm writing a VSTO for excel. 我正在为VSTO写VSTO。 I have the following problem: All this is going in the excel application 我有以下问题:这一切都将在excel应用程序中进行

I need to programmatically add a new worksheet that must look the same as a template. 我需要以编程方式添加一个必须与模板相同的新工作表。 I've got a template from which I can copy cells and formatting to the new added cell. 我有一个模板,可以从中复制单元格并将其格式化到新添加的单元格中。

How can I do this? 我怎样才能做到这一点?

I have tried the following way, to open an excel application making it invisible to user and opening the necessary template in that application. 我尝试了以下方法,打开一个excel应用程序,使其对用户不可见,并在该应用程序中打开必要的模板。 I'm going through the used range rows and trying to copy row by row. 我正在检查使用的范围行,并尝试逐行复制。 However, I encounter problems in opening the template. 但是,我在打开模板时遇到问题。 Once it gets opened, the other time it throws COM Exception(don't know what kind of that is). 一旦打开,其他时候它将引发COM异常(不知道那是什么)。

var activeExcel = (Workbook)Globals.ThisAddIn.Application.ActiveWorkbook;
            Sheet = (Worksheet) activeExcel.Worksheets.Add();
            Sheet.Name = "Счёт-фактура";

            var sourcePath = LocationHelperTool.GetTemplatePathByName("SystemInvoice.xlsx");
            try
            {
                var excelApp = new Application() { Visible = false };
                var workbook = excelApp.Workbooks.Open(sourcePath);
                var workSheets = workbook.Worksheets;
                const string sourceSheetName = "Счёт-фактура";
                var sourceSheet = (Worksheet)workSheets.Item[sourceSheetName];

                var sourceRange = sourceSheet.UsedRange;

                for (var i = 1; i <= sourceRange.Rows.Count; i++)
                {
                    var soRange = sourceRange.Rows[i];
                    var deRange = Sheet.Rows[i];
                    soRange.Copy(Type.Missing);
                    deRange.pasteSpecial(XlPasteType.xlPasteFormats);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                Clipboard.Clear();
                excelApp.Quit();
            }

I want to open a new sheet in the excel instance that the user is interacting and that sheet should be an exact clone of the template 我想在与用户进行交互的excel实例中打开一个新工作表,该工作表应该是模板的精确克隆

The reason the code runs only once appears to be because the COM objects it instantiates are not being released - so they can't be re-used: 该代码仅运行一次的原因似乎是因为未实例化其实例化的COM对象-因此无法重复使用它们:

   var excelApp = new Application() { Visible = false };
   var workbook = excelApp.Workbooks.Open(sourcePath);
   var workSheets = workbook.Worksheets;
   const string sourceSheetName = "Счёт-фактура";
   var sourceSheet = (Worksheet)workSheets.Item[sourceSheetName];

In the clean-up for this section of code you need something along these lines, where the objects are released in the reverse order they were instantiated (when the later depends on the earlier). 在本节代码的清理中,您需要沿着这些方向进行操作,其中对象以相反的顺序释放(实例化时,后者取决于前者)。 Then the released objects need to be garbage-collected in order to ensure the code can no longer "see" them. 然后,需要对释放的对象进行垃圾回收,以确保代码不再“看到”它们。

sourceRange = null;
soRange = null;
deRange = null;
workbook.Close(false); //do not save changes
sourceSheet = null;
workSheets = null;
workbook = null;
excelApp.Quit();
excelApp = null;

GC.Collect();
GC.AwaitPendingFinalizers();
GC.Collect();
GC.AwaitPendingFinalizers();

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

相关问题 如何将Excel工作表从工作簿A复制到工作簿B - How to copy an excel sheet from workbook A to workbook B 使用c#从活动Excel工作簿更新活动工作表的方法? - A way to update the active sheet from the active Excel workbook using c#? 将Excel DropDown从WorkBook复制到新工作簿 - Copy Excel DropDown from WorkBook to a new Workbook 从Excel表格复制Excel格式的Excel文件格式 - Excel Sheet copy from gridview in formated excel file 重命名Excel Active Sheet(使用VSTO)会禁用WorkBook的“添加”按钮 - Renaming Excel Active Sheet (using VSTO) disables “Add” button of WorkBook 如何使用C#从excel文件中仅获取活动工作表名称? - how to get only active sheet name from excel file using c#? C# OpenXML 将 Excel 工作表复制并粘贴到新工作簿 - C# OpenXML copy and paste excel sheet to new workbook 如何检查 Excel 工作簿或工作表是否受密码保护? - how to check Excel Workbook or sheet is password protected or not? 如何使用C#将一个Excel工作簿中特定范围的单元格复制到另一工作簿中 - how to copy cells from a specific range from one excel workbook to another workbook using c# 如何在不打开c#winforms中的excel文件的情况下将excel工作表复制到另一个excel工作簿中? - How to copy excel worksheets Into another excel workbook without opening the excel file in c# winforms?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM