我在C#中使用连接到Access数据库的ASP.NET应用程序,现在我希望它将报告导出为PDF。这是我的代码只连接到Access,我正在考虑运行访问宏或使用Microsoft.Office.Interop.Access.Application DoCmd.OutputTo运行命令,但我不知道如何使用它。有什么帮助吗?
string user = "myUser";
string pass = "myPass";
string host = "myHost";
OleDbConnection connection = new OleDbConnection(@"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = myPath");
string _command = ...; //I don't know what put here
try
{
OleDbCommand command = new OleDbCommand(_command, connection);
command.CommandText = ...; //I don't know what put here
int temp = command.ExecuteNonQuery();
Console.WriteLine("PDF created!");
}
答案 0 :(得分:3)
我通过自动化解决了我的问题,我遵循了这个https://support.microsoft.com/en-us/help/317114/how-to-automate-microsoft-access-by-using-visual-c(由Erik von Asmuth建议我),现在我可以用PDF导出我的报告。我的代码现在:
using Microsoft.Office.Interop.Access;
Application access = new Microsoft.Office.Interop.Access.Application();
access.OpenCurrentDatabase(@"C:\Desktop\MyDB.accdb", false);
access.DoCmd.OutputTo(AcOutputObjectType.acOutputReport,
"Dati", //ReportName
".pdf", //OutputType
@"C:\Desktop\Test.pdf", //outupuFile
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
AcExportQuality.acExportQualityPrint
);