日常Office编程知识积累!方便速查,使用!
1(1)打开现有工作簿
2 this.Application.Workbooks.Open(@"C:\YourPath\YourWorkbook.xls",
3 missing, missing, missing, missing, missing, missing, missing,
4 missing, missing, missing, missing, missing,missing, missing);
5 以工作簿形式打开文本文件
6 this.Application.Workbooks.OpenText(@"C:\Test.txt",
7 missing, 3,
8 Excel.XlTextParsingType.xlDelimited,
9 Excel.XlTextQualifier.xlTextQualifierNone,
10 missing, missing, missing, true, missing, missing, missing,
11 missing, missing, missing, missing, missing, missing);
12(2)激活工作簿
13 使用 Workbooks 集合的 Activate 方法。
14 ((Microsoft.Office.Interop.Excel._Workbook)
15 this.Application.Workbooks[1]).Activate();
16 使用 ThisWorkbook 激活工作簿。
17 Globals.ThisWorkbook.Activate();
18 引用各个工作簿
19 Excel.Workbook wb = this.Application.Workbooks[1];
20 wb = this.Application.Workbooks["Book1"];
21 wb = this.Application.Workbooks["Book1.xls"];
22(3)保存所有打开的工作簿
23 foreach (Excel.Workbook wkb in this.Application.Workbooks)
24 {
25 wkb.Save();
26 }
27 SaveAs 方法
28 SaveAs 方法比 Save 方法复杂。使用此方法可以将指定的工作簿保存到新位置,或以新名称保存,并可选择指定文件格式、密码、访问模式等。
29 this.SaveAs(@"C:\Book1.xml", Excel.XlFileFormat.xlXMLSpreadsheet,
30 missing, missing, missing, missing, Excel.XlSaveAsAccessMode.xlNoChange,
31 missing, missing, missing, missing, missing);
32(4)以编程方式通过电子邮件发送工作簿
33 将工作簿作为电子邮件附件发送
34 调用工作簿的 SendMail 方法并指定收件人,还可以选择指定主题。
35 Globals.ThisWorkbook.SendMail("someone@example.com", "July Sales Figures", missing);
36(5)显示对话框以打开 Excel 文件
37 Microsoft.Office.Core.FileDialog fd =
38 this.Application.get_FileDialog(Microsoft.Office.Core.MsoFileDialogType.msoFileDialogOpen);
39
40 fd.AllowMultiSelect = true;
41 fd.Filters.Clear();
42 fd.Filters.Add("Excel Files", "*.xls;*.xlw", missing);
43 fd.Filters.Add("All Files", "*.*", missing);
44
45 if (fd.Show() != 0)
46 {
47 fd.Execute();
48 }
49(6)列出最近使用的工作簿文件
50 Microsoft.Office.Tools.Excel.NamedRange nr;
51 nr = this.Controls.AddNamedRange(this.Range["A1", missing], "Files");
52
53 for(int i=1; i<=this.Application.RecentFiles.Count; i++)
54 {
55 nr.Offset[i - 1, 0].Value2 = this.Application.RecentFiles.get_Item(i).Name;
56 }
2 this.Application.Workbooks.Open(@"C:\YourPath\YourWorkbook.xls",
3 missing, missing, missing, missing, missing, missing, missing,
4 missing, missing, missing, missing, missing,missing, missing);
5 以工作簿形式打开文本文件
6 this.Application.Workbooks.OpenText(@"C:\Test.txt",
7 missing, 3,
8 Excel.XlTextParsingType.xlDelimited,
9 Excel.XlTextQualifier.xlTextQualifierNone,
10 missing, missing, missing, true, missing, missing, missing,
11 missing, missing, missing, missing, missing, missing);
12(2)激活工作簿
13 使用 Workbooks 集合的 Activate 方法。
14 ((Microsoft.Office.Interop.Excel._Workbook)
15 this.Application.Workbooks[1]).Activate();
16 使用 ThisWorkbook 激活工作簿。
17 Globals.ThisWorkbook.Activate();
18 引用各个工作簿
19 Excel.Workbook wb = this.Application.Workbooks[1];
20 wb = this.Application.Workbooks["Book1"];
21 wb = this.Application.Workbooks["Book1.xls"];
22(3)保存所有打开的工作簿
23 foreach (Excel.Workbook wkb in this.Application.Workbooks)
24 {
25 wkb.Save();
26 }
27 SaveAs 方法
28 SaveAs 方法比 Save 方法复杂。使用此方法可以将指定的工作簿保存到新位置,或以新名称保存,并可选择指定文件格式、密码、访问模式等。
29 this.SaveAs(@"C:\Book1.xml", Excel.XlFileFormat.xlXMLSpreadsheet,
30 missing, missing, missing, missing, Excel.XlSaveAsAccessMode.xlNoChange,
31 missing, missing, missing, missing, missing);
32(4)以编程方式通过电子邮件发送工作簿
33 将工作簿作为电子邮件附件发送
34 调用工作簿的 SendMail 方法并指定收件人,还可以选择指定主题。
35 Globals.ThisWorkbook.SendMail("someone@example.com", "July Sales Figures", missing);
36(5)显示对话框以打开 Excel 文件
37 Microsoft.Office.Core.FileDialog fd =
38 this.Application.get_FileDialog(Microsoft.Office.Core.MsoFileDialogType.msoFileDialogOpen);
39
40 fd.AllowMultiSelect = true;
41 fd.Filters.Clear();
42 fd.Filters.Add("Excel Files", "*.xls;*.xlw", missing);
43 fd.Filters.Add("All Files", "*.*", missing);
44
45 if (fd.Show() != 0)
46 {
47 fd.Execute();
48 }
49(6)列出最近使用的工作簿文件
50 Microsoft.Office.Tools.Excel.NamedRange nr;
51 nr = this.Controls.AddNamedRange(this.Range["A1", missing], "Files");
52
53 for(int i=1; i<=this.Application.RecentFiles.Count; i++)
54 {
55 nr.Offset[i - 1, 0].Value2 = this.Application.RecentFiles.get_Item(i).Name;
56 }