我在今天之前从未听说过这个,但听起来很有趣。我做了一些谷歌搜索,发现了这个。
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 都有非常、非常、非常强大的报告工具。