WinForm中 如何直接调用Excel的打印预览功能,但不显示Excel文件

时间:2021-07-02 17:53:50

 

using Microsoft.Office.Interop.Excel; //引用

public void PrintPriviewExcelFile(string filePath)
        {
            Microsoft.Office.Interop.Excel.ApplicationClass xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
            xlApp.Visible = true;
            object oMissing = System.Reflection.Missing.Value;
            Microsoft.Office.Interop.Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(filePath, 0, true, 5, oMissing, oMissing, true, 1, oMissing, false, false, oMissing, false, oMissing, oMissing);
            Microsoft.Office.Interop.Excel.Worksheet xlWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkbook.Worksheets[1];
            xlWorksheet.PrintPreview(null);
            xlApp.Visible = false;
            xlWorksheet = null;
        }