将 MS Access 报告导入 C#

Importing an MS Access report into C#

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

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

我正在为 Visual Studio 2017

使用 Microsoft RDLC Report Designer

感谢您的宝贵时间!

编辑: 或者有没有其他方法可以让我使用 C# 构建报告?

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

简短的回答是

Access 报告在每一件事上都非常针对 Access,与 RDLC 有很大不同。

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

Northwind.NET

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

MainForm class 是应用程序,是开始自上而下评估代码的好地方。此应用程序只执行三个任务。当按下 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;

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

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