C# 如果未安装 Excel,如何创建 Excel 实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12375943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create an instance of Excel if Excel is not installed
提问by Mahender
In my C# app, with the help of Excel Interop dll (as reference) i am reading/writing excel files. If I move this program to system where office/excel is not installed (think of clean machine), i am hitting with below error.
在我的 C# 应用程序中,在 Excel Interop dll(作为参考)的帮助下,我正在读取/写入 excel 文件。如果我将此程序移动到未安装 office/excel 的系统(想想干净的机器),我会遇到以下错误。
System.Runtime.InteropServices.COMException (0x80040154): Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).
System.Runtime.InteropServices.COMException (0x80040154):检索 CLSID 为 {00024500-0000-0000-C000-000000000046} 的组件的 COM 类工厂由于以下错误而失败:80040154 类 H0040154 未注册 REG040154 REG0404004 REG0400000004 ))。
Above error is expected since there is no excel on target machine.
My question is, is there any other way to use my program apart from registering Interop dll on target machine?
由于目标机器上没有excel,因此预计会出现上述错误。
我的问题是,除了在目标机器上注册 Interop dll 之外,还有其他方法可以使用我的程序吗?
采纳答案by Seth Flowers
My question is, is there any other way to use my program apart from registering Interop dll on target machine?
我的问题是,除了在目标机器上注册 Interop dll 之外,还有其他方法可以使用我的程序吗?
You are asking if there is any way to use your program, which uses Excel interop, when Excel is not installed on the computer running your program. The short answer is no. The longer answer is yes, if you are willing to refactor your program to not use interop.
您在询问当运行您的程序的计算机上未安装 Excel 时,是否有任何方法可以使用您的程序,该程序使用 Excel 互操作。最简洁的答案是不。更长的答案是肯定的,如果您愿意重构您的程序以不使用互操作。
You can use the OOXml SDKprovided by Microsoft if the version of Excel you are targeting is 2007 and up. You can also use a third party library such as Asposeif you are willing to spend a little bit of money.
如果您的目标 Excel 版本是 2007 及更高版本,您可以使用Microsoft 提供的OOXml SDK。如果您愿意花一点钱,也可以使用第三方库,例如Aspose。
An example of using the OOXml SDK for inserting a spreadsheet into an excel file can be found in the microsoft docs.
可以在microsoft docs 中找到使用 OOXml SDK 将电子表格插入 Excel 文件的示例。
// Given a document name, inserts a new worksheet.
public static void InsertWorksheet(string docName)
{
// Open the document for editing.
using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(docName, true))
{
// Add a blank WorksheetPart.
WorksheetPart newWorksheetPart = spreadSheet.WorkbookPart.AddNewPart<WorksheetPart>();
newWorksheetPart.Worksheet = new Worksheet(new SheetData());
Sheets sheets = spreadSheet.WorkbookPart.Workbook.GetFirstChild<Sheets>();
string relationshipId = spreadSheet.WorkbookPart.GetIdOfPart(newWorksheetPart);
// Get a unique ID for the new worksheet.
uint sheetId = 1;
if (sheets.Elements<Sheet>().Count() > 0)
{
sheetId = sheets.Elements<Sheet>().Select(s => s.SheetId.Value).Max() + 1;
}
// Give the new worksheet a name.
string sheetName = "Sheet" + sheetId;
// Append the new worksheet and associate it with the workbook.
Sheet sheet = new Sheet() { Id = relationshipId, SheetId = sheetId, Name = sheetName };
sheets.Append(sheet);
}
}
回答by Aghilas Yakoub
You can register your COM with RegSrv32
您可以使用 RegSrv32 注册您的 COM
Link : http://msdn.microsoft.com/en-us/library/ms859484.aspx
链接:http: //msdn.microsoft.com/en-us/library/ms859484.aspx
You are obliged to register your COM before using
您有义务在使用前注册您的 COM
回答by Gromer
From all the research I did a while ago when I needed to interact with an Excel file on a server, you have to install Office.
根据我不久前所做的所有研究,当我需要与服务器上的 Excel 文件进行交互时,您必须安装 Office。
回答by trailmax
Even Microsoft does not recommends usein Interop libraries on a server. So best to find some alternative framework to do Excel for you. I have successfully used Npoiin the past for that purpose.
甚至Microsoft 也不建议在服务器上使用 Interop 库。所以最好找到一些替代框架来为你做 Excel。为此,我过去曾成功地使用过Npoi。
I know this is not an answer to your exception. But honestly, Interop is a path to an endless trouble and cryptic exception messages.
我知道这不是对您的例外的回答。但老实说,Interop 是一条通往无尽麻烦和神秘异常消息的道路。
Update 2017: I have not used NPOI for a while now and moved all my projects to EPPlusinsted - library based on OpenXML that creates you modern xlsx files.
2017 年更新:我有一段时间没有使用 NPOI,并将我的所有项目移至EPPlus insted- 基于 OpenXML 的库,该库可为您创建现代 xlsx 文件。

