[英]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.