在未安装Microsoft Office的情况下使用C#为Excel工作表单元格着色

本文关键字:工作 Excel 表单 单元格 Microsoft 安装 Office 情况下 | 更新日期: 2023-09-27 18:00:15

我写了一个程序来做这件事,但得到了一个异常,说

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

如何解决此异常,以及如何使用C#为Excel工作表单元格着色?

以下是代码:

using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.Windows;
using System.Drawing;
using Microsoft.Office.Interop.Excel;
using System;
namespace Project32
{
    public class Class1
    {
        static void Main()
        {
            Application excel = new Application();
            Workbook wb = excel.Workbooks.Open(@"C:'Users'mvmurthy'Downloads'IDBDeviceReport.rdlc");
            Worksheet ws = wb.Worksheets[1];
            ws.Cells[1, 1].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red); 
        }
    }
}

在未安装Microsoft Office的情况下使用C#为Excel工作表单元格着色

当您使用Microsoft.Office.Interop命名空间通过COM"自动化"Excel时,必须在计算机上安装相应的Microsoft Office产品。

否则,您将不得不切换到某种第三方库,使您能够直接读取/写入文件,而不依赖于Microsoft Office软件。

可能考虑的库:

  • EPPlus
  • OpenXML
  • 关闭的XML