LightSwitch 没有内置报告功能,但是,您可以集成 Word 创建和打印从 LightSwitch 应用程序的报表。可以实现 Word 自动化的报告使用 Visual Studio 和 API,但是,Office 集成包 LightSwitch 扩展简化任务。
使用 Office 集成能包,您可以更轻松地自动 Word 2010、Excel 2010和 Outlook 2010 以各种方式。例如,可以导入,并且导出数据,创建文档和报表和与电子邮件和约会一起使用。可以下载 Office 集成包扩展免费从 CodePlex。
系统必备
本演练需要以下组件:
创建报表模板
首先,创建将能够影响该报表的一个 Word 文档。
创建报表模板
打开 Word 2010。
新的空白文件将出现在中。
在文档的开头,键入 PrescriptionContoso Product Catalog。
显示文本,然后,在 主页 选项的 风格 组中的功能区上的,选择 标题 命令。
在标题下将光标,然后,在 插入 选项卡中,选择 表 命令,然后选择 插入表 命令。
出现**“插入表”**对话框。
在 列数 文本框中,输入 5,然后,在 行数 文本框中,输入 2。
选择 根据窗口调整表格 选项按钮,然后选择 确定 按钮。
在表的第一行,输入以下列标题:产品 ID、产品、说明、价格和 打包。
显示表,然后,在 插入 选项的 链接 组中,选择 书签 命令。
在 书签 对话框中,命名书签 目录,然后选择 添加 按钮。
在 页面布局 选项的 页面设置 组中,选择 方向 命令,然后选择 横向 命令。
在 文件 选项卡中,选择 另存为 命令。
在 另存为 对话框中,打开 我的文档 文件夹,将文件命名为 产品目录,然后选择 保存 按钮。
在 文件 选项卡中,选择 退出 命令。
添加一个报表到应用程序
在创建报表模板后,可以实现 Office 集成包扩展,将按钮添加到应用程序工具栏,并添加代码以创建报表。您还可将文件类型。
![]() |
---|
如果您以前未生成 visual 诊所示例应用程序,您首先需要安装和配置 PrescriptionContoso 数据库,作为示例过程包的一部分,下载。打开 Install.htm 文件,然后按照说明安装数据库。 |
启用扩展
在 Visual Studio 菜单栏上,依次选择 文件,打开,Project/Solution。
查找 Vision Clinic.sln 文件,然后选择 打开 按钮。
在 解决方案资源管理器,打开 属性 节点的快捷菜单,然后选择 打开。
在应用程序设计器中,选择 扩展 选项卡,然后选择 Office 集成包 复选框。
创建报表
在 解决方案资源管理器,打开 ProductList 屏幕节点的快捷菜单,然后选择 打开。
在内容节点构树窗格中,展开 屏幕命令栏 节点,然后选择 添加,新建按钮。
在 添加按钮 对话框中,命名按钮您创建 目录,然后选择 确定 按钮。
打开 目录 按钮的快捷菜单,然后选择 编辑 Execute 代码。
在 代码编辑器,输入以下导入或使用语句在文件的顶部:
Imports OfficeIntegration
Using OfficeIntegration;
将以下代码添加到 Catalog_Execute 方法中:
' Function to format a field as Currency. Dim formatPrice = Function(x As Decimal) As String Return Format(x, "c2") End Function ' Map the Word column names to the entity column names. Dim mapContent As New List(Of ColumnMapping) mapContent.Add(New ColumnMapping("ProductID", "ProductID")) mapContent.Add(New ColumnMapping("ProductName", "ProductName")) mapContent.Add(New ColumnMapping("Description", "Description")) ' Format the price as Currency using the Function created above. mapContent.Add(New ColumnMapping("CurrentPrice", "CurrentPrice", FormatDelegate:=formatPrice)) mapContent.Add(New ColumnMapping("ProductImage", "ProductImage")) ' Define the document object. Dim doc As Object = Word.GenerateDocument(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "\Product Catalog.docx", Me.Products.SelectedItem, mapContent) ' Export the document object to Word. Word.Export(doc, "Catalog", 2, False, Me.Products, mapContent)
{ // Function to format a field as Currency. dynamic formatPrice = (decimal x) => { return Strings.Format(x, "c2"); }; // Map the Word column names to the entity column names. List<ColumnMapping> mapContent = new List<ColumnMapping>(); mapContent.Add(new ColumnMapping("ProductID", "ProductID")); mapContent.Add(new ColumnMapping("ProductName", "ProductName")); mapContent.Add(new ColumnMapping("Description", "Description")); // Format the price as Currency using the Function created above. mapContent.Add(new ColumnMapping("CurrentPrice", "CurrentPrice", FormatDelegate: formatPrice)); mapContent.Add(new ColumnMapping("ProductImage", "ProductImage")); // Define the document object. object doc = Word.GenerateDocument(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\Product Catalog.docx", this.Products.SelectedItem, mapContent); // Export the document object to Word. Word.Export(doc, "Catalog", 2, false, this.Products, mapContent); }
在菜单栏上,依次选择 调试,启动调试 运行应用程序。
在 任务 菜单中,选择 产品列表,然后选择 目录 按钮查看该报表。
(可选) 将以下代码行添加到 Catalog_Execute 方法的末尾保存和查看报表以 .pdf 格式:
Word.SaveAsPDF(doc, Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "\Product Catalog.pdf", True)
Word.SaveAsPDF(doc, Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\Product Catalog.pdf", true);
后续步骤
测试在 OfficeIntegration 命名空间中的 API 查看使用 Office 集成能包,您可以做出的许多操作。