简体   繁体   中英

How to copy templated sheet from excel file to active workbook

I'm writing a VSTO for excel. I have the following problem: All this is going in the excel application

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. 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).

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

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:

   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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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