【问题标题】:Importing an MS Access report into C#将 MS Access 报告导入 C#
【发布时间】:2018-07-31 08:52:25
【问题描述】:

你能帮我解决一个小问题吗?将不胜感激:)

所以我有一个要转换为 C# 的访问应用程序。它实际上非常庞大,并且由于访问权限非常有限,因此使用了多个访问数据库。现在我遇到的一个问题是用 C# 做报告。我知道可以从 Access 将报告导出为 XML 格式,但问题是客户想要一个不支持 SQL Server 报告服务的 SQL Server Express 版本(据我所知)。所以无论如何都可以将这些报告导入C#?或者至少只有布局?

我正在使用适用于 Visual Studio 2017 的 Microsoft RDLC 报表设计器

感谢您的宝贵时间!

编辑: 或者有什么替代方法可以用来用 C# 构建报告?

【问题讨论】:

    标签: c# sql-server ms-access reporting sql-server-express


    【解决方案1】:

    简短的回答是

    Access 报告在每件事上都非常具体地针对 Access,与 RDLC 非常不同。

    这是我和我的一个同事为在 .Net 中创建的报告制作的一个示例,它完全模仿了旧样本 Northwind 数据库的报告:

    Northwind.NET

    【讨论】:

    • 感谢您的努力,非常感谢!
    【解决方案2】:

    我在今天之前从未听说过这个,但听起来很有趣。我做了一些谷歌搜索,发现了这个。

    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;
    

    单击打印...按钮时,将打开选定的报告并将其发送到默认打印机。 隐藏复制代码

    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;
    

    最后,当单击“保存”按钮时,所选报告以 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;
    

    最后,

    1)From the main menu select Project > Add Reference.
    2)Go to the COM tab. Scroll down and select Microsoft Access 11.0 Object Library.
    3)Click Select and then click OK.
    
    using MsAccess = Microsoft.Office.Interop.Access;
    

    顺便说一句,考虑将访问表和查询导入 Python 或 R。如果您使用的是 SQL Server Express,我猜想钱是个问题。 Python 和 R 都是 100% 免费的,并且都可以与 Access 完美配合。最后,Python 和 R 都有非常、非常、非常强大的报告工具。

    【讨论】:

    • 哇,非常感谢您的努力 :) 虽然这不是这个项目的解决方案(客户希望访问应用程序完全转换为 c#/sql),但我肯定会保存它。这将在未来为我节省大量时间。再次感谢
    【解决方案3】:

    你能直接导入它们吗?不。我知道没有任何工具可以做到。您应该可以在 ReportViewer 中重新创建它们,尽管这很乏味。

    【讨论】:

    • 同意,这很乏味。
    【解决方案4】:

    就我而言,标记为答案的帖子不正确。 这个问题没有直接的答案。 在这里没有复制和粘贴解决方案的地方。 但是,我已经解决了将 MS Access 应用程序转换为 C# 程序的类似问题。 大多数耗时的报告任务是格式化报告的外观。 我使用 Crystal Reports 和 DevExpress 报表解决方案将报表导入到他们的报表环境中。 他们很好地为您制作所有格式。并节省大量从头开始格式化所有报告的无聊时间。 但是您需要修复报表的查询和数据库连接。因为 MS Access 使用了自己的 SQL 语法。但对于程序员来说,这些是更有趣的任务。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-23
      • 2014-08-18
      • 2022-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多