简体   繁体   中英

Export Access report as PDF

I'm doing an ASP.NET application in C# that connects to an Access database and now I want that it exports a report as PDF. Here is my code that only connects to Access, I was thinking of running an access macro or running a command with Microsoft.Office.Interop.Access.Application DoCmd.OutputTo but I do not know how to use it exactly. Any help?

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!");
 }

I resolved my problem with the automation, I followed this https://support.microsoft.com/en-us/help/317114/how-to-automate-microsoft-access-by-using-visual-c (suggested me by Erik von Asmuth) and now I can export my report in PDF. My code now:

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
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM