VSTO开发之一 工作簿的操作
1、创建一个新的工作簿
Excel.Workbook newWorkbook = this.Application.Workbooks.Add();
2、打开一个工作簿
this.Application.Workbooks.Open(@"C:\Test\YourWorkbook.xlsx");
3、关闭一个工作簿
Globals.ThisWorkbook.Close(false); this.Application.ActiveWorkbook.Close(false, missing, missing);
4、保存一个工作簿
this.Save(); this.Application.ActiveWorkbook.Save();
5、以编程的方式保护文档
this.Protect(getPasswordFromUser, missing, missing); this.Unprotect(getPasswordFromUser);
6、编程方式列出最近使用的工作簿文件
Excel.Range rng = this.Application.get_Range("A1"); for(int i=1; i<=this.Application.RecentFiles.Count; i++) { rng.get_Offset(i - 1,0).Value2 = this.Application.RecentFiles.get_Item(i).Name; }
7、以编程方式及工作簿形式打开文本文件
this.Application.Workbooks.OpenText(@"C:\Test.txt", missing, 3, Excel.XlTextParsingType.xlDelimited, Excel.XlTextQualifier.xlTextQualifierNone, missing, missing, missing, true, missing, missing, missing, missing, missing, missing, missing, missing, missing);