且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用C#在嵌入的Winforms控件中打开Excel文档(.xlsx)

更新时间:2023-10-04 09:40:40

如果您安装了32位办公室在64位操作系统中,您必须将winforms应用程序编译为x86而不是AnyCPU。如果它被编译为AnyCPU,它将启动64位CLR,它不能托管32位办公室互操作dll。



旧的xls格式由Jet引擎支持对于64位和32位并排支持,新的xlsx格式由更新的引擎支持,该引擎具有并排支持。这是一个主要的痛苦,但有关64位支持的信息直接来自Microsoft支持 - 他们更长时间打算提供并排支持,因为将文件支持视为办公室的一部分,而不是操作系统的一部分。幸运的是,很少有人安装64位办公室,即使在64位操作系统上,因此编译x86将在任一操作系统上使用32位办公室。如果您必须同时支持32位办公室和64位办公室,则需要两个版本(一个AnyCPU或x64和一个x86),并且需要确定您在安装时与哪个办公室版本进行交互。
If you have 32 bit office installed in a 64 bit OS, you must compile your winforms application as "x86" not "AnyCPU". If it is compiled as "AnyCPU" it will start the 64 bit CLR which cannot host 32 bit office interop dlls.

The older xls format was supported by the Jet engine, which has side by side support for both 64 and 32 bit, the newer xlsx format is supported by a newer engine that does have side by side support. This is a major pain in the neck, but the information about 64 bit support was directly from Microsoft support - they nmo longer intend to provide side by side support, as the see the file support as part of office, not part of the OS. Fortunately, few people install 64 bit office, even on a 64 bit OS, so compiling for x86 will work with 32 bit office on either OS. If you have to support both 32 bit office, and 64 bit office, you will need two versions (one AnyCPU or x64 and one x86), and will need to determine which office version you are interacting with at install time.