C#前期绑定和后期绑定操作Excel-------实现简单打印功能

  1,前期绑定

前期绑定需要引用Microsoft.Office.Interop.Excel.dll,代码有提示功能,编写方便,且代码量小。但是和具体的office版本密切相关,不同版本的Excel可能会出现不兼容。

using System;
using System.Collections.Generic;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;

namespace ExcelCsharpTest
{
    class ExcelPrintNomal
    {
        public void PrintOut(string excelFileName)
        {
            object Missing = System.Reflection.Missing.Value;
            Excel.Application objExcel = null;
            Excel.Workbooks objWorkbooks = null;
            Excel.Workbook objWorkbook = null;
            Excel.Worksheet objWorkSheet = null;

            try
            {
                objExcel = new Microsoft.Office.Interop.Excel.Application();
            }
            catch
            {
                throw new Exception("没有安装Microsoft Office Excel。");
            }

            if (!System.IO.File.Exists(excelFileName))
            {
                throw new Exception("Excle文件不存在。");
            }

            try
            {
                objExcel.DisplayAlerts = false;
                objExcel.WindowState = Microsoft.Office.Interop.Excel.XlWindowState.xlMinimized;
                objExcel.Visible = false;

                objWorkbooks = objExcel.Workbooks;

                objWorkbook = objExcel.Workbooks.Open(excelFileName, Missing, Missing, Missing, Missing, Missing, Missing, Missing,
                                                      Missing, Missing, Missing, Missing, Missing, Missing, Missing);

                objWorkSheet = (Excel.Worksheet)objWorkbook.Worksheets[1];

                SetValue(objWorkSheet, 1, 1, "Cell_11");
                SetValue(objWorkSheet, 1, 2, "Cell_12");
                SetValue(objWorkSheet, 1, 3, "Cell_13");

                objWorkSheet.PrintOut(Missing, Missing, 1, Missing, Missing, Missing, true, Missing);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                ReleaseComObj(objWorkSheet);
                if (objWorkbook != null)
                {
                    objWorkbook.Close(false, Missing, Missing);
                    ReleaseComObj(objWorkbook);
                }
                if (objWorkbooks != null)
                {
                    objWorkbooks.Close();
                    ReleaseComObj(objWorkbooks);
                }
                if (objExcel != null)
                {
                    objExcel.Quit();
                    ReleaseComObj(objExcel);
                }
                System.GC.Collect();
            }

        }

        public void SetValue(Excel.Worksheet sheet, int row, int col, object value)
        {
            object cell;
            Excel.Range range;

            cell = sheet.Cells[row, col];
            range = sheet.get_Range(cell, cell);
            range.Value2 = value;

            ReleaseComObj(cell);
            ReleaseComObj(range);
        }

        public void ReleaseComObj(object objCom)
        {
            if (objCom != null)
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(objCom);
                objCom = null;
            }
        }

    }
}


2,后期绑定

后期绑定,利用反射实现。不需引用dll,也和机器上安装的office版本无关,excel2003,excel2007都能正常运行。灵活性很强,但是可以看出代码不够方便,编写量也比前期绑定稍大。

using System;
using System.Collections.Generic;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;
namespace ExcelCsharpTest
{
    class ExcelPrint
    {
        public  void PrintOut(string excelFileName)
        {
            object Missing = System.Reflection.Missing.Value;

            object objExcel = null;
            object objWorkbooks = null;
            object objWorkbook = null;
            object objWorkSheet = null;
            object[] parameters = null;

            try
            {
                objExcel = Activator.CreateInstance(Type.GetTypeFromProgID("Excel.Application"));
            }
            catch
            {
                throw new Exception("没有安装Microsoft Office Excel。");
            }

            if (!System.IO.File.Exists(excelFileName))
            {
                throw new Exception("Excle文件不存在。");
            }

            try
            {
                const int xlMinimized = -4140;

                parameters = new object[1];
                parameters[0] = false;
                objExcel.GetType().InvokeMember("DisplayAlerts", System.Reflection.BindingFlags.SetProperty, null, objExcel, parameters);

                parameters = new object[1];
                parameters[0] = xlMinimized;
                objExcel.GetType().InvokeMember("WindowState", System.Reflection.BindingFlags.SetProperty, null, objExcel, parameters);

                parameters = new object[1];
                parameters[0] = false;
                objExcel.GetType().InvokeMember("Visible", System.Reflection.BindingFlags.SetProperty, null, objExcel, parameters);

                objWorkbooks = objExcel.GetType().InvokeMember("WorkBooks", System.Reflection.BindingFlags.GetProperty, null, objExcel, null);

                parameters = new object[1];
                parameters[0] = excelFileName;
                objWorkbook = objWorkbooks.GetType().InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, objWorkbooks, parameters);

                parameters = new object[1];
                parameters[0] = 1;
                objWorkSheet = objWorkbook.GetType().InvokeMember("Worksheets", System.Reflection.BindingFlags.GetProperty, null, objWorkbook, parameters);

                SetValue(objWorkSheet, 1, 1, "Cell_11");
                SetValue(objWorkSheet, 1, 2, "Cell_12");
                SetValue(objWorkSheet, 1, 3, "Cell_13");

                parameters = new object[8];
                parameters[0] = Missing;
                parameters[1] = Missing;
                parameters[2] = 1;
                parameters[3] = Missing;
                parameters[4] = Missing;
                parameters[5] = Missing;
                parameters[6] = true;
                parameters[7] = Missing;
                objWorkSheet.GetType().InvokeMember("PrintOut", System.Reflection.BindingFlags.InvokeMethod, null, objWorkSheet, parameters);
            }
            catch(Exception ex)
            {
                throw ex;
            }
            finally
            {
                ReleaseComObj(objWorkSheet);
                if (objWorkbook != null)
                {
                    objWorkbook.GetType().InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, objWorkbook, null);
                    ReleaseComObj(objWorkbook);
                }
                if (objWorkbooks != null)
                {
                    objWorkbooks.GetType().InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, objWorkbooks, null);
                    ReleaseComObj(objWorkbooks);
                }
                if (objExcel != null)
                {
                    objExcel.GetType().InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, objExcel, null);
                    ReleaseComObj(objExcel);
                }
                System.GC.Collect();
            }

        }

        public void SetValue(object sheet, int row, int col, object value)
        {
            object[] parms = null;

            parms = new object[2];
            parms[0] = row;
            parms[1] = col;
            object cell = sheet.GetType().InvokeMember("Cells", System.Reflection.BindingFlags.GetProperty, null,sheet, parms);

            parms = new object[1];
            parms[0] = value;
            cell.GetType().InvokeMember("Value", System.Reflection.BindingFlags.SetProperty, null, cell, parms);

            ReleaseComObj(cell);
        }

        public void ReleaseComObj(object objCom)
        {
            if (objCom != null)
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(objCom);
                objCom = null;
            }
        }

    }
}


调用代码:

            ExcelPrint excelPrint = new ExcelPrint();
            excelPrint.PrintOut("C:\\Book1.xls");

            ExcelPrintNomal excelPrintNomal = new ExcelPrintNomal();
            excelPrintNomal.PrintOut("C:\\Book1.xls");


 

你可能感兴趣的:(exception,object,C#,Excel,null,Parameters)