首页 > 解决方案 > C# - 打印目录中的所有文件

问题描述

我正在寻找一种方法来打印给定目录中的所有文件。每个文件都是我的程序创建的 Excel 文档。

我曾尝试使用 PrintDocument 但无法弄清楚我实际上如何指定 PrintDocument 它应该打印哪个文件...

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

任何帮助表示赞赏。谢谢!

标签: c#.netexcelwinformsprinting

解决方案


要打印文件(文档),您需要指示操作系统打印它。Process.Start与 printVerb和文件名一起使用。

操作系统 (Windows) 将找到正确的应用程序来打开文档 (Excel) 并向其发送命令以打印文件。

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

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

 Process.Start(startInfo);

PrintDocument 是您想要自己布局和打印文档的时候。


推荐阅读