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();
            }
        }
  • 相关阅读:
    (八)断路器-Hystrix
    WINDOWS SERVER 2012 虚拟机 忘记密码后
    IIS FTP :在组合的密钥属性“users,roles,permissions”分别设置为“*,Read,Write”时,无法添加类型为“add”的重复集合项
    log4j 日志组件
    IDEA缓存
    com.alibaba.druid.pool.DruidDataSource
    EHCache CacheManager
    webservice调试(XML参数) Wizdler PostMan
    jar类库加载顺序
    JAXB工具
  • 原文地址:https://www.cnblogs.com/TNTZWC/p/1943106.html
Copyright © 2011-2022 走看看