gpt4 book ai didi

C# - 打印目录中的所有文件

转载 作者:太空宇宙 更新时间:2023-11-03 11:57:15 25 4
gpt4 key购买 nike

我正在寻找一种方法来打印给定目录中的所有文件。每个文件都是由我的程序创建的 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();
}
}

感谢任何帮助。谢谢!

最佳答案

要打印文件(文档),您可以指示操作系统打印它。使用 Process.Start打印 Verb , 和文件名。

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

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

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

Process.Start(startInfo);

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

关于C# - 打印目录中的所有文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59073738/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com