65.9K
CodeProject 正在变化。 阅读更多。
Home

从 C# 打印 Microsoft Access 报表

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.74/5 (9投票s)

2007年12月17日

CPOL

3分钟阅读

viewsIcon

61084

downloadIcon

6465

如何在 C# 应用程序中打印 Microsoft Access 报表

Screenshot -

引言

我非常喜欢 Microsoft Access。对于非企业级应用来说,它是最好的数据库,因为它非常容易使用:它便携且易于备份。Access 最棒的地方在于报表功能极其简单。无需额外付费购买 Crystal Reports 等软件包,您就可以生成精美的报表。

直到我为了撰写本文进行一些研究之前,我一直无法利用 C# 应用程序中的报表功能。多年来,我一直使用 .NET 的 OleDb 将 Access 的存储结构作为后端数据库。它非常简单,极大地简化了应用程序的开发,但我一直觉得无法通过我的程序生成 Access 报表是一个很大的缺点。为部门级应用程序开发一个报表生成器工作量太大了,而涉及到 Crystal Reports 又太麻烦,所以我通常都是生成 HMTL 或文本报表。

本文介绍了如何从 .NET 应用程序中打印 Microsoft Access 报表。

背景

非常感谢 Tom Archer,他最初用 C++ 实现了这项功能。我没有重复 Tom 的类结构,而是通过一个应用程序用 .NET 演示了这项功能。代码非常简单。本文使用了 Microsoft Access COM 的 .NET Interop 功能。尽管使用了这些技术,但本文并未详细解释这些技术,因为有许多其他文章对此进行了阐述。本文仅专注于从 Microsoft Access 报表打印。

Using the Code

MainForm 类是该应用程序,是自顶向下评估代码的一个好起点。该应用程序仅执行三项任务。当按下 **Browse...** 按钮时,它会将报表加载到列表框中。

// we have a valid file name so we now need to
// populate the list box with available reports
listBoxReports.Items.Clear();

// create an application object.
MsAccess.Application app = new MsAccess.Application();
// open the access database file.
app.OpenCurrentDatabase(dlg.FileName, false, "");
string sql = "SELECT [Name] FROM MSysObjects WHERE Type = -32764";
dao.Database db = app.CurrentDb();
// query the database for all the reports.  all this data is
// contained in the MSysObejcts table which is invisible through
// the table listing in access.
dao.Recordset rs = db.OpenRecordset(sql, Type.Missing, Type.Missing, Type.Missing);
// go through and add all the reports to the list box.
while (!rs.EOF) {
    listBoxReports.Items.Add(rs.Fields[0].Value);
    rs.MoveNext();
}

// clean up
rs.Close();
rs = null;
db.Close();
db = null;
app.CloseCurrentDatabase();
app = null;

当单击 **Print...** 按钮时,选定的报表将被打开并发送到默认打印机。

string report = listBoxReports.SelectedItem.ToString();

// create an application object.
MsAccess.Application app = new MsAccess.Application();
// open the access database file.
app.OpenCurrentDatabase(textBoxAccess.Text.Trim(), false, "");
app.Visible = false;
// open the report
app.DoCmd.OpenReport(report, 
    Microsoft.Office.Interop.Access.AcView.acViewPreview, Type.Missing, 
    Type.Missing, MsAccess.AcWindowMode.acWindowNormal, Type.Missing);
// print the report to the default printer.
app.DoCmd.PrintOut(MsAccess.AcPrintRange.acPrintAll, Type.Missing, 
    Type.Missing, MsAccess.AcPrintQuality.acHigh, Type.Missing, Type.Missing);
// cleanup
app.CloseCurrentDatabase();
app = null;

最后,当单击 **Save** 按钮时,选定的报表将作为 HTML 保存到与 Access 数据库文件相同的目录中。

// create an application object.
MsAccess.Application app = new MsAccess.Application();
// open the access database file.
app.OpenCurrentDatabase(textBoxAccess.Text.Trim(), false, "");
app.Visible = false;
// open the report
app.DoCmd.OpenReport(report, Microsoft.Office.Interop.Access.AcView.acViewPreview, 
    Type.Missing, Type.Missing, MsAccess.AcWindowMode.acWindowNormal, Type.Missing);
// export the report to an HTML file
app.DoCmd.OutputTo(MsAccess.AcOutputObjectType.acOutputReport, 
    report, "HTML (*.html)", fileName, Type.Missing, Type.Missing, Type.Missing);
// cleanup
app.CloseCurrentDatabase();
app = null;

添加 Microsoft Access COM 组件

Microsoft Access 报表是通过 Microsoft Access COM 组件访问的。需要为这些 COM 组件创建包装器。Microsoft Visual Studio 2005 提供了一种简便的方法,可以将非托管 COM 组件包含到您的托管代码中。您需要在开发系统上安装 Microsoft Access 才能遵循这些说明。本文示例中使用了 Microsoft Access 2003 和 Visual Studio 2005。

  1. 从主菜单中选择 **Project** > **Add Reference**。
  2. 转到 **COM** 选项卡。向下滚动并选择 **Microsoft Access 11.0 Object Library**。
  3. 单击 **Select**,然后单击 **OK**。

当您转到 Solution Explorer 时,您应该会在 _References_ 文件夹下看到 AccessADODBDAOMicrosoft.Office.CoreSystem.XMLVBIDE。下一步是将 using 语句添加到您的代码中。对于 using 语句,我使用赋值格式,因为对于库特定的代码,我更喜欢使用类名称的命名空间部分,这样我就知道类的位置。通过使用 using 语句的赋值格式,我可以使代码更具可读性,同时还能指示类的位置。

using MsAccess = Microsoft.Office.Interop.Access;

这就是将 Interop 功能添加到您的项目所需的所有内容。

讨论

正如我在本文开头所说,我喜欢 Microsoft Access,但我讨厌在 Access 中编程,也讨厌 Access 应用程序。我就是不喜欢在另一个应用程序中编程。多年来,我一直使用 Access 作为 .NET 应用程序的存储位置。在那些年里,我一直缺少 Access 丰富的报表功能。现在,多亏了 Tom Archer,我可以在我的应用程序中拥有丰富的报表功能,而无需求助于 Crystal Reports。

历史

  • 2007 年 12 月 17 日 -- 发布原始版本
© . All rights reserved.