none
求,关于以下代码,的一些资料,,感谢。。各位,先生。。 RRS feed

  • 问题

  • using Microsoft.Office.Interop.Excel;
    public void PrintPriviewExcelFile(string filePath)//filepath excel文件路径
            {
                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.
                Microsoft.Office.Interop.Excel.Worksheet xlWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkbook.Worksheets[1];
                xlWorksheet.PrintPreview(null);//打印预览
                xlApp.Visible = false;
                xlWorksheet = null;
            }
    关于Excel类,操作excel表格,实现打印的,调用的是excel的打印
    2010年5月5日 15:09

答案

  • using Excel=Microsoft.Office.Interop.Excel;
    
     /// <summary>
        /// 打印
        /// </summary>
        /// <param name="strFilePath">文件路径</param>
        /// <param name="strSheetName">SHEET表名称</param>
        public static void InvokeExcelPrint(string strFilePath, string strSheetName)
        {
          Excel.ApplicationClass xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
          Excel.Workbooks xlWorkbooks;
          Excel.Workbook xlWorkbook;
          Excel.Worksheet xlWorksheet;
          System.Type tyWorkbooks;
          System.Reflection.MethodInfo[] methods;
          object objFilePath;
    
          object oMissing = System.Reflection.Missing.Value;
    
          if (!System.IO.File.Exists(strFilePath))
            throw new System.IO.FileNotFoundException();
    
          try
          {
            xlApp.Visible = true;
            xlWorkbooks = xlApp.Workbooks;
            tyWorkbooks = xlWorkbooks.GetType();
            methods = tyWorkbooks.GetMethods();
            objFilePath = strFilePath;
            xlWorkbook = (Excel.Workbook)tyWorkbooks.InvokeMember("Open",
            System.Reflection.BindingFlags.InvokeMethod,
            null,
            xlWorkbooks,
            new object[] { objFilePath, true, true });
            xlWorksheet = (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();
          }
        }

    努力+方法=成功
    2010年5月6日 1:52

全部回复

  • using Excel=Microsoft.Office.Interop.Excel;
    
     /// <summary>
        /// 打印
        /// </summary>
        /// <param name="strFilePath">文件路径</param>
        /// <param name="strSheetName">SHEET表名称</param>
        public static void InvokeExcelPrint(string strFilePath, string strSheetName)
        {
          Excel.ApplicationClass xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
          Excel.Workbooks xlWorkbooks;
          Excel.Workbook xlWorkbook;
          Excel.Worksheet xlWorksheet;
          System.Type tyWorkbooks;
          System.Reflection.MethodInfo[] methods;
          object objFilePath;
    
          object oMissing = System.Reflection.Missing.Value;
    
          if (!System.IO.File.Exists(strFilePath))
            throw new System.IO.FileNotFoundException();
    
          try
          {
            xlApp.Visible = true;
            xlWorkbooks = xlApp.Workbooks;
            tyWorkbooks = xlWorkbooks.GetType();
            methods = tyWorkbooks.GetMethods();
            objFilePath = strFilePath;
            xlWorkbook = (Excel.Workbook)tyWorkbooks.InvokeMember("Open",
            System.Reflection.BindingFlags.InvokeMethod,
            null,
            xlWorkbooks,
            new object[] { objFilePath, true, true });
            xlWorksheet = (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();
          }
        }

    努力+方法=成功
    2010年5月6日 1:52
  • 能不能,加上注释啊,大哥。。。。

    2010年5月6日 3:07
  • 顶上去。

    2010年5月6日 4:51
  • 没有弄懂楼主的意思,楼主究竟想用Excel或者Word进行什么操作?这样我们才可以找代码啊?呵呵
    2010年5月6日 5:43
    版主
  • 就是,,,winform点击按钮调用excel打印预览。

    在预览之前我想设置excel的打印格式,如单色打印 缩放1:1

    2010年5月6日 6:01