2017-07-31 105 views
0

我正在C#中执行一个连接到Access数据库的ASP.NET应用程序,现在我希望它将报告导出为PDF。这里是我的代码,只连接到Access,我想运行一个访问宏或与Microsoft.Office.Interop.Access.Application DoCmd.OutputTo运行命令,但我不知道如何使用它。任何帮助?以PDF格式导出访问报告

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

你在这里错了路。您可以将报表打印为PDF,但由于连接到Access数据库(处理表,查询和数据宏的报表,而不是报表,表单,VBA和普通宏)而不是应用程序,因此无法使用OleDb访问报表。这:https://support.microsoft.com/en-us/help/317114/how-to-automate-microsoft-access-by-using-visual-c是使用COM和使用Access应用程序的一个很好的入门者,而不是数据库。但是试着先打印到在Access中工作的PDF,然后尝试在C#中实现它。 –

+0

你为什么不使用RDLC报告?它提供内置功能,可以将报告导出为PDF或其他格式,无需预览给用户。 –

+0

谢谢,我会搜索自动访问,但我不知道什么是RDLC报告,我不知道如何使用Access。 –

回答

0

我解决我的问题与自动化,我跟着这个https://support.microsoft.com/en-us/help/317114/how-to-automate-microsoft-access-by-using-visual-c(埃里克·冯·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 
);