zoukankan      html  css  js  c++  java
  • c# 调用Excel的打印预览页面

    调用Excel的打印预览页面 
    public void ExcelPrint(string strFilePath, string strSheetName)
        {
            Microsoft.Office.Interop.Excel.ApplicationClass xlApp = new ApplicationClass();
            Microsoft.Office.Interop.Excel.Workbooks xlWorkbooks;
            Microsoft.Office.Interop.Excel.Workbook xlWorkbook;
            Microsoft.Office.Interop.Excel.Worksheet xlWorksheet;
            System.Type tyWorkbooks;
            System.Reflection.MethodInfo[] methods;
            object objFilePath;
            
            object oMissing = System.Reflection.Missing.Value;
            strFilePath = Server.MapPath(strFilePath);
                if (!System.IO.File.Exists(strFilePath))
                {
                    throw new System.IO.FileNotFoundException();
                    return;
                }
                try
                {
                xlApp.Visible = true;
                xlWorkbooks = xlApp.Workbooks;
                tyWorkbooks = xlWorkbooks.GetType();
                methods = tyWorkbooks.GetMethods();
                objFilePath = strFilePath;
                object Nothing = System.Reflection.Missing.Value;
                xlWorkbook = xlApp.Workbooks.Open(strFilePath, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing);
                
                //xlWorkbook = (Microsoft.Office.Interop.Excel.Workbook)tyWorkbooks.InvokeMember("Open ",
                //System.Reflection.BindingFlags.InvokeMethod,
                //null,
                //xlWorkbooks,
                //new object[] { objFilePath, true, true });
                xlWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkbook.Worksheets[strSheetName];
                   
                xlWorksheet.PrintPreview(true);
                xlWorkbook.Close(oMissing, oMissing, oMissing);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (xlApp != null)
                {
                    xlApp.Quit();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
                    xlApp = null;
                }
                GC.Collect();
            }
        }
  • 相关阅读:
    第二周c语言PTA作业留
    2018第零次作业
    总结报告
    第14/15周作业
    第七周作业
    第六周作业
    第四次作业
    第三次作业
    大学第二次作业
    大学的第一次作业
  • 原文地址:https://www.cnblogs.com/TNTZWC/p/1943106.html
Copyright © 2011-2022 走看看