使用 Excel 数据实现自动化
- Home
- 资源
- 用户手册
- think-cell Charts:数据可视化
- 高级报告自动化
- 使用 Excel 数据实现自动化
在 Windows 上,您可以使用 think-cell API 函数以编程方式创建包含 Excel 数据的 think-cell 演示文稿。
简介:使用 Excel 数据实现自动化
当您需要定期生成大型报告,但又不想每次都手动更新数据,Excel 自动化极其有用。对于较小或临时的制图任务,我们建议使用 从 Excel 创建元素。
若要创建 PowerPoint 模板的副本并更新复制的演示文稿中链接到 Excel 工作簿的所有 think-cell 元素(参阅从 Excel 创建元素),请使用 PresentationFromTemplate 函数。
若要更新 PowerPoint 模板中的特定 think-cell 元素,无论它们是否链接到 Excel,请使用 UpdateBatch API。UpdateBatch取代了 UpdateChart(已弃用)。但是,使用 UpdateChart 的现有代码仍可正常工作。
think-cell API 和 Office 自动化
think-cell API 已集成到 Microsoft 的组件对象模型 (COM) 中,因此您可以通过能对 Office 编程的任何语言(例如 Visual Basic for Applications (VBA) 或 C#)访问 API。有关如何使用 VBA 和 C# 的说明,请参阅开始使用 think-cell API。
think-cell 的入口点是 think-cell 加载项对象。您可以通过 Application.COMAddIns 集合访问对象。调用 think-cell 始终采用后期绑定(请参阅 Microsoft Learn)。因此,think-cell 加载项对象的类型只是 Object,您无需添加任何类型库或引用。获取对象后,即可进行调用。例如,在 Excel 内的 VBA 中,使用以下代码:
Dim tcXlAddIn As Object
Set tcXlAddIn = Application.COMAddIns("thinkcell.addin").Object
在 C# 中,您可以通过宣布 think-cell 加载项对象的引用为 dynamic,从而实现后期绑定。当您宣布引用为 var 时,编译器会推断对象的类型为 dynamic,因此您只需使用以下代码。在此示例中,xlapp 是加载了 think-cell 的 Excel.Application 对象的引用。
var tcXlAddIn = xlapp.COMAddIns.Item("thinkcell.addin").Object;
匹配 Excel 和模板数据布局
要在相应的图表中正确显示数据,Excel 中的数据布局必须与模板中的图表数据表布局相匹配(请参阅转置和编辑链接范围的数据布局)。例如,如果您在模板图表中添加行 100%=,think-cell 会将第二行 Excel 数据解释为总计,再根据总计计算百分比。
下表总结了常见的数据布局选项、这些选项在数据布局中的位置以及这些选项的默认设置。若要检查特定图表的数据布局,请双击图表以打开其数据表。
|
Data layout option |
Contents |
Position |
Default setting |
|---|---|---|---|
|
Series |
系列标签 |
第 1 列 |
已选择 |
|
Category |
类别标签 |
第 1 行 |
已选择 |
|
100%= |
用于计算百分比的总计 |
第 2 行(如果选择了“Category”) |
未选择 |
使元素格式与 Excel 相匹配
您可以使元素的格式(例如数字格式、填充和字体颜色)与 Excel 工作簿中的格式相匹配。在调用 PresentationFromTemplate 或 UpdateBatch 之前,请在 Excel 工作簿中应用所需的格式。然后,在 PowerPoint 模板中,使元素的数字格式、字体格式、填充或边框与 Excel 工作簿相匹配(请参阅使元素格式与链接的数据范围相匹配)。
PresentationFromTemplate
PresentationFromTemplate 函数可创建 PowerPoint 模板的副本,并更新复制的演示文稿中链接到 Excel 工作簿的所有 think-cell 元素。如需详细了解链接的 think-cell 元素,请参阅从 Excel 创建元素。
PresentationFromTemplate 签名
VBA
tcXlAddIn.PresentationFromTemplate( _
Workbook As Excel.Workbook, _
Template As String, _
PpApplication As PowerPoint.Application _
) As PowerPoint.Presentation
C#
PowerPoint.Presentation tcXlAddIn.PresentationFromTemplate(
Excel.Workbook Workbook,
string Template,
PowerPoint.Application PpApplication
);
PresentationFromTemplate 描述
PresentationFromTemplate 采用以下参数:
Workbook是指包含数据的 Excel 工作簿。Template是指包含要用数据填充的元素的 PowerPoint 模板。若要用数据填充元素,必须将元素链接到Workbook中的某个范围(请参阅从 Excel 创建元素)。Template可以指定来自Workbook所在位置的相对路径,也可以指定绝对路径。PpApplication是指生成新演示文稿的 PowerPoint 实例。
PresentationAsTemplate 可创建Template的副本,并更新复制的演示文稿中链接到Workbook的所有元素,无论您是否已将元素设置为自动更新(请参阅管理链接的元素中的数据)。
在新演示文稿中,PresentationFromTemplate 会有意地中断更新后元素的 Excel 链接,以防止任何无意的进一步更新。但是,函数未更新的元素会保留其 Excel 链接。这意味着您可以更新多个 Excel 工作簿中的链接。若要这样做,在创建新演示文稿后,使用下一个工作簿再次调用 PresentationFromTemplate,并使用您刚刚创建的演示文稿作为模板。
PresentationFromTemplate 示例
若要使用这些示例,先创建一个演示文稿,里面包含一个堆积柱形图,该图链接到 Excel 工作簿的第一个表格的范围 G1:K4(请参阅从 Excel 创建图表)。在同一个目录中将演示文稿保存为 C:\Samples\PresentationFromTemplate\template.pptx,将工作簿保存为 data.xlsx。
VBA
在此示例中,在 Sheet1 中,程序 PresentationFromTemplate_Sample() 将 H3 的值从 i=1 更改为 10。H3 链接到堆积柱形图的第一个系列中的第一个值。对于 H3 的每个新值,PresentationFromTemplate_Sample() 执行以下操作:
- 制作
template.pptx的副本。 - 在复制的演示文稿中,使用新值更新图表。
- 断开图表的 Excel 链接。
- 在与模板相同的目录中,将新演示文稿保存为
output_i.pptx。
若要使用此示例,请按照以下步骤在您先前创建的 Excel 工作簿data.xlsx中创建模块。模块需要引用 Microsoft PowerPoint 16.0 Object Library(请参阅Visual Basic for Applications)。
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 start.
' 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#
在此示例中,程序打开 Excel 并加载 data.xlsx。然后,在 Sheet1 中,程序将 H3 的值从 i=1 更改为 10。H3 链接到堆积柱形图的第一个系列中的第一个值。对于 H3 的每个新值,程序执行以下操作:
- 制作
template.pptx的副本。 - 在复制的演示文稿中,使用新值更新图表。
- 断开图表的 Excel 链接。
- 在与模板相同的目录中,将新演示文稿保存为
output_i.pptx。
若要使用此示例,请在 C# Console App项目模板中将Program.cs中的代码替换为以下代码。程序需要引用 Microsoft PowerPoint 16.0 Object Library、Microsoft Excel 16.0 Object Library和 Microsoft Office 16.0 Object Library(请参阅C#)。
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();
}
}
}
UpdateBatch
使用 UpdateBatch API 指定要使用新的 Excel 数据来更新 PowerPoint 模板中的哪些 think-cell 元素。无论元素是否链接到 Excel,您都可以更新元素。
UpdateBatch 取代了 UpdateChart(已弃用)。虽然使用 UpdateChart 的现有代码仍可正常工作,但我们建议您改为使用 UpdateBatch:
- 新功能(例如,用 Excel 数据填充表格图像;请参阅插入表格图像)仅在
UpdateBatch中可用。 - 对于较新的 think-cell 版本,
UpdateChart的运行速度可能非常慢,尤其是对于大型演示文稿。
UpdateBatch 签名
VBA
tcXlAddIn.CreateUpdate() As Object
tcUpdate.AddRangeData(
Target As Object, _
Name As String, _
Range As Excel.Range, _
Transposed As Boolean _
)
tcUpdate.AddRangeImage(
Target As Object, _
Name As String, _
Range As Excel.Range _
)
tcUpdate.Send()
C#
object tcXlAddIn.CreateUpdate();
void tcUpdate.AddRangeData(
object Target,
string Name,
Excel.Range Range,
bool Transposed
);
void tcUpdate.AddRangeImage(
object Target,
string Name,
Excel.Range Range
);
void tcUpdate.Send();
UpdateBatch 描述
UpdateBatch 包含以下函数:
CreateUpdate可创建对象来存储要更新的所有元素及其相应的 Excel 数据范围。AddRangeData可安排更新要用 Excel 数据填充的图表、表格、自动化文本字段、哈维球、复选框或图像。AddRangeImage可安排更新要用 Excel 数据填充的表格图像(请参阅插入表格图像)。Send可收集 Excel 数据并将其发送到 PowerPoint 以创建演示文稿。如果您的代码以 PowerPoint 模板中的 Excel 链接元素为目标,这些链接将会中断(请参阅从 Excel 创建元素)。
AddRangeData 和 AddRangeImage 采用以下参数:
Target是指包含要用数据填充的元素的 PowerPoint 模板。Target可以是Presentation、SlideRange、Slide、Master或CustomLayout。Name是指具有AddRangeData或AddRangeImage名称的元素。在运行UpdateBatch之前,您必须使用 AddRangeData Name或 AddRangeImage Name 在 PowerPoint 模板中指定名称(请参阅创建 PowerPoint 模板,以实现报告自动化)。名称不区分大小写。如果Target中的两个元素具有相同的名称,think-cell 会用相同的数据来填充它们。Range是指包含元素数据的 Excel 范围。Transposed(仅限AddRangeData):若要调换更新后元素数据表中的行和列的方向(参阅转置和编辑链接范围的数据布局),请将“Transposed”设置为True。如果元素数据的方向与 Excel 数据的方向相匹配,则将“Transposed”设置为False。
UpdateBatch 示例
以下 VBA 和 C# 代码示例使用 Excel 工作簿的第一个工作表中的数据更新 PowerPoint 模板中的三个元素。这些示例将生成的演示文稿保存为 template_updated.pptx。
若要使用这些示例,请创建包含 think-cell 元素的 PowerPoint 模板,并创建包含要用于填充元素的数据的 Excel 工作簿。
若要创建 PowerPoint 模板,请按照以下步骤操作:
- 按照PowerPoint 模板示例中所述创建带有幻灯片标题和图表的 PowerPoint 演示文稿。
- 在演示文稿中,添加表格图像(请参阅插入表格图像)。
- 打开表格的迷你工具栏。在“AddRangeImage Name”中,输入
TableAsImage1。选择 Enter。 - 将演示文稿保存到
C:\Samples\UpdateBatch\template.pptx。
若要创建 Excel 工作簿,请按照以下步骤操作:
- 打开 Excel 工作簿。
- 在 A1 中,输入幻灯片标题。
- 在 A2:D5 区域,输入图表数据。确保 Excel 中的数据布局与 PowerPoint 模板中的图表数据表布局相匹配(请参阅匹配 Excel 和模板数据布局)。
- 在 A7:D10 区域,输入表格数据。您应用的任何表格格式(例如填充和边框)都会显示在新演示文稿中。
- 将工作簿保存到
C:\Samples\UpdateBatch\data.xlsx。
在 UpdateBatch 程序中指定路径时,请使用双反斜杠 (\\) 作为目录分隔符。
VBA
若要使用此代码示例,请按照以下步骤在您先前创建的 Excel 工作簿中创建模块。模块需要引用 Microsoft PowerPoint 16.0 Object Library。有关更多信息,请参阅 Visual Basic for Applications。
Option Explicit
Sub UpdateBatch_Sample()
' Get the ranges that contain the new data
Dim rngTitle As Excel.Range
Set rngTitle = ActiveWorkbook.Sheets(1).Range("A1")
Dim rngChart As Excel.Range
Set rngChart = ActiveWorkbook.Sheets(1).Range("A2:D5")
Dim rngTableAsImage As Excel.Range
Set rngTableAsImage = ActiveWorkbook.Sheets(1).Range("A7:D10")
' Get the think-cell add-in object
Dim tcXlAddIn As Object
Set tcXlAddIn = Application.COMAddIns("thinkcell.addin").Object
' Get a PowerPoint instance. Hold onto this object as long as
' you want to access the new presentation. There can only be one
' PowerPoint instance. If there is no instance, the script will
' start one. Otherwise, the script uses the existing instance.
Dim ppapp As Object
Set ppapp = New PowerPoint.Application
Dim pres As PowerPoint.Presentation
' When the script ends, this code opens the new presentation
' so that you can edit it further. To keep the presentation open,
' also remove pres.Close and ppapp.Quit.
' Set pres = ppapp.Presentations.Open( _
' Filename:="C:\\Samples\\UpdateBatch\\template.pptx", _
' Untitled:=msoTrue _
' )
' When the script ends, this code does not open the new presentation.
Set pres = ppapp.Presentations.Open( _
Filename:="C:\\Samples\\UpdateBatch\\template.pptx", _
Untitled:=msoTrue, _
WithWindow:=msoFalse _
)
Dim tcUpdate As Object
Set tcUpdate = tcXlAddIn.CreateUpdate
Call tcUpdate.AddRangeData(pres, "Title", rngTitle, False)
Call tcUpdate.AddRangeData(pres, "Chart1", rngChart, False)
Call tcUpdate.AddRangeImage(pres, "TableAsImage1", rngTableAsImage)
Call tcUpdate.Send
pres.SaveAs("C:\\Samples\\UpdateBatch\\template_updated.pptx")
pres.Close
ppapp.Quit
End Sub
C#
若要使用此代码示例,请在 C# Console App项目模板中将Program.cs中的代码替换为以下代码。
程序需要引用 Microsoft PowerPoint 16.0 Object Library、Microsoft Excel 16.0 Object Library和 Microsoft Office 16.0 Object Library。有关更多信息,请参阅 C#。
using Excel = Microsoft.Office.Interop.Excel;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Office = Microsoft.Office.Core;
namespace ConsoleApplication_UpdateBatch
{
class Program
{
static void Main()
{
PowerPoint.Application ppapp = new PowerPoint.Application();
PowerPoint.Presentation presentation = ppapp.Presentations.Open(
"C:\\Samples\\UpdateBatch\\template.pptx",
Office.MsoTriState.msoFalse,
Office.MsoTriState.msoTrue
);
Excel.Application xlapp = new Excel.Application { Visible = true };
Excel.Workbook workbook = xlapp.Workbooks.Open(
"C:\\Samples\\UpdateBatch\\data.xlsx",
Office.MsoTriState.msoFalse,
Office.MsoTriState.msoTrue
);
Excel.Worksheet worksheet = workbook.Sheets[1];
var tcXlAddIn = xlapp.COMAddIns.Item("thinkcell.addin").Object;
var tcUpdate = tcXlAddIn.CreateUpdate();
tcUpdate.AddRangeData(
presentation,
"SlideTitle",
worksheet.get_Range("A1"),
true
);
tcUpdate.AddRangeData(
presentation,
"Chart1",
worksheet.get_Range("A2", "D5"),
true
);
tcUpdate.AddRangeImage(
presentation,
"TableAsImage1",
worksheet.get_Range("A7", "D10")
);
tcUpdate.Send();
presentation.SaveAs("C:\\Samples\\UpdateBatch\\template_updated.pptx");
presentation.Close();
ppapp.Quit();
workbook.Close(false);
xlapp.Quit();
}
}
}
UpdateChart(已弃用)
UpdateChart 可使用 Excel 数据来更新特定元素的数据表。
UpdateBatch 取代了 UpdateChart(请参阅UpdateBatch)。虽然使用 UpdateChart 的现有代码仍可正常工作,但对于新代码,我们建议您改为使用 UpdateBatch:
- 新功能(例如,用 Excel 数据填充表格图像;请参阅插入表格图像)仅在
UpdateBatch中可用。 - 对于较新的 think-cell 版本,
UpdateChart的运行速度可能非常慢,尤其是对于大型演示文稿。
UpdateChart 签名
VBA
tcXlAddIn.UpdateChart( _
Target As Object, _
ChartName As String, _
Range As Excel.Range, _
Transposed As Boolean _
)
C#
void tcXlAddIn.UpdateChart(
object Target,
string ChartName,
Excel.Range Range,
bool Transposed
);