1 调用Excel的COM组件。
在项目中打开Add Reference对话框,选择COM栏,之后在COM列表中找到"Microsoft Excel 11.0 Object Library"(Office 2003),然后将其加入到项目的References中即可。
但是打包之后会出一些问题,最好将excel.exe 转变成excel.dll引用。
方法是将Office目录下的Excel.exe文件拷出来,把Excel.exe文件考到C:/Program Files/Microsoft Visual Studio 8/SDK/v2.0/Bin里,然后运行VS2005命令提示输入TlbImp EXCEL.EXE Excel.dll
2 调用excel.Application.Workbooks.Add(Missing.Value)会遇到如下错误: |
3 最后记得要清理内存和恢复环境 System.Runtime.InteropServices.Marshal.ReleaseComObject(range); System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet); System.Runtime.InteropServices.Marshal.ReleaseComObject(book); while (System.Runtime.InteropServices.Marshal.ReleaseComObject(excel) > 0) ; range = null; sheet = null; book = null; excel = null; GC.Collect(); System.Threading.Thread.CurrentThread.CurrentCulture = CurrentCI;//恢复系统环境 |