如果没有安装Excel,如何创建Excel实例 [英] How to create an instance of Excel if Excel is not installed

查看:207
本文介绍了如果没有安装Excel,如何创建Excel实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的C#应用​​程序中,借助于Excel Interop dll(作为参考),我正在读/写excel文件。如果我把这个程序移动到没有安装office / excel的系统(想想干净的机器),我碰到了错误。


System.Runtime.InteropServices.COMException(0x80040154):由于以下错误,检索CLSID {00024500-0000-0000-C000-000000000046}的组件的COM类工厂失败:80040154类未注册(HRESULT异常:0x80040154(REGDB_E_CLASSNOTREG ))。


以上错误是预期的,因为目标机器没有excel。

我的问题是,除了在目标机上注册Interop dll,还有什么其他方法可以使用我的程序吗?

解决方案


我的问题是,是否有任何其他方式使用我的程序除了
在目标机器上注册Interop dll?


你问是否有任何方法使用您的程序,它使用Excel互操作运行程序的计算机上未安装Excel。简短的答案是否定的。更长的答案是肯定的,如果你愿意重构你的程序不使用互操作。



您可以使用 OOXml SDK 由Microsoft提供,如果您所定位的Excel版本是2007年以上。您还可以使用第三方库,例如如果您愿意花费一点钱。



使用OOXml SDK将电子表格插入excel文件的示例可以是发现于 msdn

  //给定一个文档名称,插入一个新的工作表。 
public static void InsertWorksheet(string docName)
{
//打开文档进行编辑。
使用(SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(docName,true))
{
//添加一个空白的WorksheetPart。
WorksheetPart newWorksheetPart = spreadSheet.WorkbookPart.AddNewPart< WorksheetPart>();
newWorksheetPart.Worksheet = new Worksheet(new SheetData());

表单= spreadSheet.WorkbookPart.Workbook.GetFirstChild< Sheets>();
string relationshipsId = spreadSheet.WorkbookPart.GetIdOfPart(newWorksheetPart);

//获取新工作表的唯一ID。
uint sheetId = 1;
if(sheets.Elements< Sheet>()。Count()> 0)
{
sheetId = sheets.Elements< Sheet>() SheetId.Value).Max()+ 1;
}

//给新的工作表一个名字。
string sheetName =Sheet+ sheetId;

//附加新工作表并将其与工作簿相关联。
Sheet sheet = new Sheet(){Id = relationshipId,SheetId = sheetId,Name = sheetName};
sheets.Append(sheet);
}
}


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.

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)).

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?

解决方案

My question is, is there any other way to use my program apart from registering Interop dll on target machine?

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.

You can use the OOXml SDK provided by Microsoft if the version of Excel you are targeting is 2007 and up. You can also use a third party library such as Aspose if you are willing to spend a little bit of money.

An example of using the OOXml SDK for inserting a spreadsheet into an excel file can be found at msdn.

// 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);
    }
}

这篇关于如果没有安装Excel,如何创建Excel实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆