25. 使用 Excel 数据实现自动化
在 Excel 中提供数据后,您可以使用 UpdateChart
和 PresentationFromTemplate
函数通过编程对该数据的使用进行控制。
通过 UpdateChart
,您可以对特定元素的数据表和您的 Excel 数据进行交换。通过 PresentationFromTemplate
,您可以根据 PowerPoint 模板,使用数据创建新的演示文稿(将 think-cell 元素链接到 21. Excel 数据链接 中描述的 Excel 中的数据范围)。
这两个函数的接口都已集成到 Office Automation 模型中,因此可以通过能对 Office 编程的任何语言(例如 Visual Basic for Applications (VBA) 或 C#)进行访问。请查阅 入门 了解详细说明。
think-cell 的入口点是 think-cell 加载项对象。可以通过 Application.COMAddIns
集合对其进行访问。调用 think-cell 始终采用后期绑定。如需说明,请参阅 Microsoft 的知识库:
因此,think-cell 加载项对象的类型只是 Object
,没有要添加的类型库或引用。只需获取对象,即可进行调用。例如,在 Excel 内的 VBA 中:
Dim tcXlAddIn As Object
Set tcXlAddIn = Application.COMAddIns("thinkcell.addin").Object
在 C# 中,您可以通过宣布 think-cell 加载项对象的引用为 dynamic
,从而实现后期绑定;这也是在宣布引用为 var
时编译程序推断的类型,这样您只需编写即可:
var tcXlAddIn = xlapp.COMAddIns.Item("thinkcell.addin").Object;
在这里,xlapp
是加载了 think-cell 的 Excel.Application
对象的引用。
- 25.1
- UpdateChart
- 25.2
- PresentationFromTemplate
25.1 UpdateChart
25.1.1 签名
VBA
tcXlAddIn.UpdateChart( _
target As Object, _
strName As String, _
rgData As Excel.Range, _
bTransposed As Boolean _
)
C#
void tcXlAddIn.UpdateChart(
object target,
string strName,
Excel.Range rgData,
bool bTransposed
);
25.1.2 描述
此函数会使用 rgData
中包含的数据,更新 target
中所有名称为 strName
的元素。为了正确地解读图表中的数据,范围 rgData
必须符合其默认数据表布局,或其转置版本,另请参阅 从 Excel 创建图表 和 适合数据布局。通过将 bTransposed
设置成 false
或 true
,以分别表示使用默认或转置版本。对于图表之外的元素,例如表格,会忽略 bTransposed
值。
target
必须为 Presentation
或 SlideRange
,或单一的 Slide
、Master
或 CustomLayout
。
名称 strName
的匹配不区分大小写。先前必须已经按照中所述在 PowerPoint 中使用 UpdateChart 名称24. 自动化简介属性控件分配该名称。
为了确保针对了正确的元素,请确保在作为 pres
传递的对象中将它们的 UpdateChart 名称设置为 strName
。
若已将目标元素链接到 Excel 数据范围,则调用此函数时,链接将中断。此后,不会将元素链接到任何 Excel 范围。
25.1.3 示例
若要使用这些示例,请根据 24. 自动化简介 中的描述准备演示文稿,并将其保存为 C:\Samples\UpdateChart\template.pptx
。
VBA
若要使用此示例,请将其添加到 Excel 工作簿中的模块。
它需要引用 Microsoft PowerPoint 16.0 对象库(请参阅 Visual Basic for Applications 了解详细信息)。
在工作簿中运行 UpdateChart_Sample
后,将会以其第一张图表的范围 A1:D5
中包含的数据更新演示文稿模板内的图表。
Option Explicit
Sub UpdateChart_Sample()
' Get the range containing the new data
Dim rng As Excel.Range
Set rng = ActiveWorkbook.Sheets(1).Range("A1:D5")
' Get the think-cell add-in object
Dim tcXlAddIn As Object
Set tcXlAddIn = Application.COMAddIns("thinkcell.addin").Object
' Get a PowerPoint instance. Hold on to this
' object as long as you want to access the
' generated presentations. There can only be a
' single PowerPoint instance. If there is no
' PowerPoint running, one will be started.
' Otherwise the existing one is used.
Dim ppapp As Object
Set ppapp = New PowerPoint.Application
Dim pres As PowerPoint.Presentation
' PowerPoint window visible
' Set pres = ppapp.Presentations.Open( _
' Filename:="C:\\Samples\\UpdateChart\\template.pptx", _
' Untitled:=msoTrue)
' PowerPoint window invisible
Set pres = ppapp.Presentations.Open( _
Filename:="C:\\Samples\\UpdateChart\\template.pptx", _
Untitled:=msoTrue, _
WithWindow:=msoFalse)
Call tcXlAddIn.UpdateChart(pres, "Chart1", rng, False)
' Save the updated presentation
pres.SaveAs ("C:\\Samples\\UpdateChart\\template_updated.pptx")
pres.Close
ppapp.Quit
End Sub
C#
若要使用此模板,请用它更换 C# 控制台应用程序项目模板的 Program.cs
中的代码。
它需要引用 Microsoft PowerPoint 16.0 对象库、Microsoft Excel 16.0 对象库和 Microsoft Office 16.0 对象库(请参阅 C# 了解详细信息)。
运行生成的应用程序后,将会更新演示文稿模板中的图表,使其包含带有值 1、2 和 3,名为“系列 1”的单一系列,并将结果保存为 template_updated.pptx
。
using Excel = Microsoft.Office.Interop.Excel;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Office = Microsoft.Office.Core;
namespace ConsoleApplication_UpdateChart
{
class Program
{
static void Main()
{
Excel.Application xlapp = new Excel.Application { Visible = true };
Excel.Workbook workbook = xlapp.Workbooks.Add(1);
Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Sheets[1];
worksheet.Cells[3, 1] = "Series 1";
worksheet.Cells[3, 2] = 1;
worksheet.Cells[3, 3] = 2;
worksheet.Cells[3, 4] = 3;
PowerPoint.Application ppapp = new PowerPoint.Application();
PowerPoint.Presentation presentation = ppapp.Presentations.Open(
"C:\\Samples\\UpdateChart\\template.pptx",
Office.MsoTriState.msoFalse,
Office.MsoTriState.msoTrue
);
var tcXlAddIn = xlapp.COMAddIns.Item("thinkcell.addin").Object;
tcXlAddIn.UpdateChart(
presentation,
"Chart1",
worksheet.get_Range("A1", "D3"),
false
);
presentation.SaveAs("C:\\Samples\\UpdateChart\\template_updated.pptx");
presentation.Close();
ppapp.Quit();
workbook.Close(false);
xlapp.Quit();
}
}
}
25.2 PresentationFromTemplate
25.2.1 签名
VBA
tcXlAddIn.PresentationFromTemplate( _
wb As Excel.Workbook, _
strTemplate As String, _
ppapp As PowerPoint.Application _
) As PowerPoint.Presentation
C#
PowerPoint.Presentation tcXlAddIn.PresentationFromTemplate(
Excel.Workbook wb,
string strTemplate,
PowerPoint.Application ppapp
);
25.2.2 描述
此函数使用 Excel 工作簿 wb
和文件名为 strTemplate
的模板之间的数据链接,通过使用与它们相链接的范围内的数据来更新链接的元素,从而将该模板实例化。结果是,在 PowerPoint 实例 ppapp
中产生新的演示文稿。
strTemplate
可以是完整路径,也可以是相对路径(随后会将其作为相对于 Excel 工作簿文件 所在位置的相对路径)wb
。
将更新 strTemplate
中已链接到 Excel 工作簿 wb
的所有元素(不论是否已将其设为自动更新,都是如此)。在生成的演示文稿中,它们的数据链接会被中断,以防止对这些元素进行进一步更改。
strTemplate
中已链接到除 wb
外其他 Excel 工作簿的元素将保持不变,并且仍保持链接状态,因此可以将此函数的结果另存为新模板,然后使用下一个工作簿再次调用此函数,以便更新多个 Excel 工作簿中的链接。
若您希望使用 Excel 链接控制图表段的颜色或表格单元格的格式,可以分别将配色方案设为在顶部使用数据表填充(请参阅 配色方案)或使用数据表…选项(请参阅 设置表格的格式)。同样,若要使用 Excel 链接控制数字格式,请将其设为使用 Excel 格式(请参阅数字格式)。
请确保先设置 Excel 中各单元格的相关格式选项和数字格式,然后再调用 PresentationFromTemplate
。
25.2.3 示例
若要使用这些示例,先按照 从 Excel 创建图表 中的说明,创建一个演示文稿,里面包含一个堆积图,该图链接到 Excel 工作簿的第一个表格的范围 G1:K4
。在同一个目录中将生成的演示文稿保存为 C:\Samples\PresentationFromTemplate\template.pptx
,将工作簿保存为 data.xlsx
。
VBA
若要使用此示例,将其添加到按照上面说明准备的 Excel 工作簿 data.xlsx
中的模块。
它需要引用 Microsoft PowerPoint 16.0 对象库(请参阅 Visual Basic for Applications 了解详细信息)。
运行 PresentationFromTemplate_Sample
将会更改单元格 Sheet1!H3
中的值(该值链接到 template.pptx
中包含的图表的第一系列的第一个值,从 i=1
到 10
),使用更新后包含该值的模板中的图表创建新的演示文稿(该值不再链接到工作簿),并在与模板相同的目录中将其保存为 output_i.pptx
。
Option Explicit
Sub PresentationFromTemplate_Sample()
' Get the range to modify. It is more efficient
' to do this once rather than within the loop.
Dim rng As Excel.Range
Set rng = ActiveWorkbook.Sheets(1).Cells(3, 8)
' Get the think-cell add-in object
Dim tcXlAddIn As Object
Set tcXlAddIn = Application.COMAddIns("thinkcell.addin").Object
' Get a PowerPoint instance. Hold on to this
' object as long as you want to access the
' generated presentations. There can only be a
' single PowerPoint instance. If there is no
' PowerPoint running, one will be started.
' Otherwise the existing one is used.
Dim ppapp As Object
Set ppapp = New PowerPoint.Application
Dim i As Integer
For i = 1 To 10
' Modify the range value.
' Note: Avoid selecting the cell prior to
' changing it. It is very slow and has
' undesirable side-effects.
' BAD:
' rng.Select
' ActiveWindow.Selection.Value = 0
' GOOD:
rng.Value = i
' Generate a new presentation based on the
' linked template.
Dim pres As PowerPoint.Presentation
Set pres = tcXlAddIn.PresentationFromTemplate( _
Excel.ActiveWorkbook, "template.pptx", ppapp _
)
' If you want to modify the new presentation
' before saving it this is the place to do it.
' Save the new presentation
pres.SaveAs "C:\Samples\PresentationFromTemplate\output_" & i & ".pptx"
' Explicitly close the presentation when we
' are done with it to free its memory.
' Letting the object go out of scope is not
' sufficient.
pres.Close
Next
End Sub
C#
若要使用此模板,请用它更换 C# 控制台应用程序项目模板的 Program.cs
中的代码。
它需要引用 Microsoft PowerPoint 16.0 对象库、Microsoft Excel 16.0 对象库和 Microsoft Office 16.0 对象库(请参阅 C# 了解详细信息)。
运行生成的应用程序显然将会打开 Excel,加载工作簿 data.xlsx
,更改单元格 H3
中的值(该值链接到 template.pptx
中包含的图表的第一系列的第一个值,从 i=1
到 10
),使用更新后包含该值的模板中的图表创建新的演示文稿(该值不再链接到工作簿),并在与模板相同的目录中将其保存为 output_i.pptx
。
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Excel = Microsoft.Office.Interop.Excel;
namespace ConsoleApplication_PresentationFromTemplate
{
class Program
{
static void Main()
{
var xlapp = new Excel.Application { Visible = true };
var tcXlAddIn = xlapp.COMAddIns.Item("thinkcell.addin").Object;
var workbook = xlapp.Workbooks.Open("C:\\Samples\\PresentationFromTemplate\\data.xlsx");
var ppapp = new PowerPoint.Application();
for (var i = 1; i <= 10; ++i)
{
workbook.Sheets[1].Cells[3, 8] = i;
PowerPoint.Presentation presentation = tcXlAddIn.PresentationFromTemplate(
workbook,
"C:\\Samples\\PresentationFromTemplate\\template.pptx",
ppapp
);
presentation.SaveAs("C:\\Samples\\PresentationFromTemplate\\output" + i + ".pptx");
presentation.Close();
}
ppapp.Quit();
workbook.Close(false);
xlapp.Quit();
}
}
}