3

I'm looking for a way to print all files in a given directory. Each file will be an Excel doc created by my program.

I have tried using PrintDocument but couldn't figure out how I actually specify to PrintDocument which file it should be printing...

private void PrintSheets(string filepath)
{
            //get directory
            DirectoryInfo dirInfo = new DirectoryInfo(filepath);
            //get all files in directory
            FileInfo[] Files = dirInfo.GetFiles();

            //loop through files
            foreach (FileInfo file in Files)
            {
                //create new print doc
                PrintDocument printDocument = new PrintDocument();
                //select printer
                printDocument.PrinterSettings.PrinterName = cmbSelectPrinter.SelectedItem.ToString();
                //set filename document
                printDocument.DocumentName = file.Name;

                //print the doc
                printDocument.Print();               
            }
}

Any help appreciated. Thanks!

Paul Alexander
  • 2,686
  • 4
  • 33
  • 69

1 Answers1

2

To print a file (document) you instruct the operating system to print it. Using Process.Start with the print Verb, and the filename.

The OS (Windows) will find the correct application to open the document (Excel) and send it a command to print the file.

 var fileName = @"C:\Path To Your Excel file.xlsx";

 var startInfo = new ProcessStartInfo(fileName);
 startInfo.Verb = "print";

 Process.Start(startInfo);

PrintDocument is when you want to layout and and print a document yourself.

GvS
  • 52,015
  • 16
  • 101
  • 139