c# - 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();
}
}
任何帮助表示赞赏。谢谢!
解决方案
要打印文件(文档),您需要指示操作系统打印它。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 是您想要自己布局和打印文档的时候。
推荐阅读
- python-3.x - Morningstar 用 python 请求获取 10 年的财务数据
- java - Spring security - oauth2 资源服务器测试
- python - PyMongo:更新很多,熊猫数据框
- android - 滚动到列表视图条目后如何使其闪烁?
- regex - 为什么我在 influxdb 中为单个测量获得多个表
- macos - 当两个 macOS .plugin 依赖于同一个 .dylib 并且每个都将它单独捆绑在插件文件夹中时会发生什么?
- python - 正则表达式:匹配括号括起来的字符串中的某个单词
- c# - KeyBinding 用作 UserControl 但在 XAML 中使用属性元素语法时不起作用
- visual-c++ - 非成员函数和抽象类
- shell - 基于轮换在目录中查找和合并日志文件