日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

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

發布時間:2025/3/20 C# 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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");


?

轉載于:https://www.cnblogs.com/xiashengwang/archive/2011/09/06/2578802.html

總結

以上是生活随笔為你收集整理的C#前期绑定和后期绑定操作Excel-------实现简单打印功能的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。