c# EPPLUS -打印excel文件
本文关键字:excel 文件 打印 EPPLUS | 更新日期: 2023-09-27 18:06:54
您好,我使用EPPLUS-ExcelPackageClass,我想打印工作表?我发现只有一个功能的设置:公共密封类ExcelPrinterSettings: XmlHelper(例如:BlackAndWhite,BottomMargin,Draft,FitToHeight,PaperSize,PrintArea等),打印文件到打印机的最后命令是什么?
根据这个答案,没有打印方法:Print command in EPPlus?
您可以尝试将其转换为PDF或使用Office Excel应用程序打印。
看起来你不能用EPPlus打印成PDF。你可以用Excel来做。如果这对你有帮助,这里有一个语法可以实现(我使用Excel 2013和pdfcreator)
这样做的是将pdf保存在位置C:'ExcelPDF中,然后添加下划线,然后将今天的日期以mmddyyyy格式附加到文件名的末尾。
ActiveWorkbook.ExportAsFixedFormat (Type: Excel.XlFixedFormatType.xlTypePDF,
Filename: "C:'ExcelPDF_" +
DateTime.Now.ToString("MMddyyyy") + ".pdf",
Quality: XlFixedFormatQuality.xlQualityStandard, OpenAfterPublish: false);
我建议您将其转换为pdf然后打印。要将Excel文件中的工作表转换为pdf文件,您可以使用EPPlus库,该库允许您将页面导出为pdf文件,使用WorksheetToPdfConverter函数将页面转换为pdf文件并保存到硬盘。可以使用以下代码:
public void ExcelWorksheetToPdf(string excelFilePath, string sheetName, string pdfFilePath)
{
// Load Excel workbook
using (ExcelPackage excelPackage = new ExcelPackage(new FileInfo(excelFilePath)))
{
// Get worksheet by name
ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets[sheetName];
// PDF converter
ExcelToPdfConverter converter = new ExcelToPdfConverter(worksheet);
// Convert worksheet to PDF and save to file
File.WriteAllBytes(pdfFilePath, converter.Convert());
}
}
之后,可以使用打印对话框使用以下代码打印PDF文件:
public void PrintPdfFileWithDialog(string pdfFilePath)
{
// Create a process to open the PDF file
Process process = new Process();
process.StartInfo.FileName = pdfFilePath;
// Start the process
process.Start();
// Wait for the process to finish loading the PDF file
process.WaitForInputIdle();
// Open the print dialog
process.StartInfo.Verb = "Print";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.UseShellExecute = true;
process.StartInfo.Arguments = "/p '"" + pdfFilePath + "'"";
process.Start();
// Wait for the process to finish printing
process.WaitForExit();
// Close the process
process.Close();
}