日常Office编程知识积累!方便速查,使用!
1
(1)在工作表单元格中显示字符串
2
可创建一个命名的范围以引用该单元格。
3
this.message.Value2 = "Hello world";
4
使用 Excel 对象
5
Excel.Range rng = Globals.Sheet1.Range["A1", missing];
6
rng.Value2 = "Hello world";
7![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
(2)在工作簿中移动工作表
9
Move 方法的工作方式与 Copy 方法基本相同,不过最后只有表的一个实例。如果不为移动的工作表指定位置,
10
Excel将创建新的工作簿来容纳它。
11
移动工作表
12
将工作簿中的工作表的总数分配给一个变量,然后移动第一个工作表,使其成为最后一个工作表。
13
int totalSheets = this.Application.ActiveWorkbook.Sheets.Count;
14
Globals.Sheet1.Move(missing, Globals.ThisWorkbook.Sheets[totalSheets]);
15![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
16
(3)列出工作簿中的所有工作表
17
循环访问 Worksheets 集合,将每个表的名称发送到通过 NamedRange 控件偏移的单元格。
18
private void ListSheets()
19
{
20
int index = 0;
21![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
Microsoft.Office.Tools.Excel.NamedRange NamedRange1 =
23
Globals.Sheet1.Controls.AddNamedRange(
24
Globals.Sheet1.Range["A1", missing], "NamedRange1");
25![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
foreach (Excel.Worksheet displayWorksheet in Globals.ThisWorkbook.Worksheets)
27
{
28
NamedRange1.Offset[index, 0].Value2 = displayWorksheet.Name;
29
index++;
30
}
31
}
32![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
33
(4)PrintOut方法使您能够打印指定的对象。您可以包含许多可选参数,
34
其中包括要打印的页码范围、份数、打印前是否预览、
35
要使用的打印机的名称、是否需要打印到文件、是否需要逐份打印以及要打印到的文件的名称。
36
打印工作表
37
调用 Sheet1 的 PrintOut 方法,要求打印两份,并在打印前预览文档。
38
Globals.Sheet1.PrintOut(1, 1, 2, true, missing, missing, missing, missing);
39![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)