企业应用中后台经常会跑一些生成、发送Excel报表的程序。
.NET应用程序生成Excel文件,基本上有2种方法:
1.Microsoft Excel XX Object Library (Interop.Excel.DLL文件)
2.NPOI
虽然说NPOI很快,但是Excel XX Object Library是非常方便的,代码可以参考Excel中录制的宏代码。
前段时间就遇到一个问题:.NET开发的基于Microsoft Excel XX Object Library 报表程序在Windows Server 2008 R2上运行。报错:
Microsoft Office Excel 不能访问文件“XX.xls”。 可能的原因有:
? 文件名称或路径不存在。
? 文件正被其他程序使用。
? 您正要保存的工作簿与当前打开的工作簿同名
XX.xls就是模板文件。真是百思不得其解。
在网上查找了一圈找到了解办法:
?Windows 2008 Server x64
Please make this folder.
C:\Windows\SysWOW64\config\systemprofile\Desktop
?Windows 2008 Server x86
Please make this folder.
C:\Windows\System32\config\systemprofile\Desktop
...instead of dcomcnfg.exe.
This operation took away office automation problems in my system.
A Desktop folder seems to be necessary in the systemprofile folder to open file by Excel.
It disappears from Windows2008, Windows2003 had the folder,
and I think it cause this error.
也就是:
C:\Windows\System32\config\systemprofile和C:\Windows\SysWOW64\config\systemprofile目录下创建名为Desktop目录即可解决问题