在C#中读取Excel数据和单元格格式 [英] Reading Excel data and cell formatting in C#

查看:933
本文介绍了在C#中读取Excel数据和单元格格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个可以让我在C#中阅读Excel文档的图书馆,但是我需要能够阅读单元格格式以及电子表格中的数据。我发现这个有用的问题列出了许多不同的替代方案,用于阅读Excel文档但是这里提供的很多解决方案只允许读取电子表格的内容,而不是单元格属性(例如字体,背景颜色等)。对于我的特殊问题,我需要阅读一个包含很多单元格格式的文档,我需要能够读取这个格式信息。

I am looking for a library that allows me to read an Excel document in C#, but I am needing to be able to read the cell formatting as well as the data in the spreadsheet. I found this useful question that lists a lot of different alternatives for reading Excel documents, but a lot of the solutions offered here only allow the content of the spreadsheet to be read, not the cell properties (eg font, background colour, etc). For my particular problem, though, I am needing to read a document that contains a lot of cell formatting and I need to be able to read this formatting information.

我需要阅读的Excel文档是一个 xls 文档,因此我不需要阅读 xlsx 在这个阶段我正在寻找一个可以在没有安装Excel的服务器上使用的库,因此它必须是独立的解决方案,最好是开源的。

The Excel document that I am needing to reading is an xls document, so I do not need to read xlsx at this stage. I am looking for a library that can be used on a server without Excel being installed, so it must be a standalone solution and preferably open-source.

有没有人有经验从 xls 文档读取单元格格式化信息,并可以建议一个可以帮助完成此任务的库?

Does anyone have experience with reading cell formatting information from an xls document and can suggest a library that will help accomplish this task?

更新:

我正在看使用ClosedXml,因为我已经阅读过,似乎提供了我需要的功能。我从这个示例代码中工作:

I am looking at using ClosedXml because from what I have read about it, it seems to provide the functionality that I am needing. I am working from this sample code:

http://closedxml.codeplex.com/wikipage?title=Finding%20and%20extracting%20the%20data&referringTitle=Documentation

,并且能够阅读Excel文档的内容而没有问题。我已经使用了这个代码中的Categories示例,但是格式化了两个类别名单元格以具有背景颜色。现在我正试图用ClosedXml来确定:

and have been able to read the content of a Excel document without issues. I have used just the Categories example from this code, but have formatted two of the categoryname cells to have background colours. Now what I am trying to do with ClosedXml is determine:


  • 如果一个填充背景颜色被定义在特定单元格

  • if a Fill background colour has been defined on a specific cell

如果已定义,则检索填充的颜色(十六进制值将执行)

if it has been defined, retrieve the colour of the Fill (hex value will do)

以下是我尝试使用的一些代码:

Here is some code that I have tried to use:

// Get all categories
while (!categoryRow.Cell(coCategoryId).IsEmpty())
{
    IXLCell categoryName = categoryRow.Cell(coCategoryName);
    try
    {
        categories.Add(categoryName.GetString() + " " + categoryName.Style.Fill.BackgroundColor.Color.ToHex());
    }
    catch
    {
        categories.Add(categoryName.GetString() + " None");
    }
    categoryRow = categoryRow.RowBelow();
}

但代码

categoryName.Style.Fill.BackgroundColor.Color.ToHex()

总是抛出ex给定的键不存在于字典中。,即使是定义了背景颜色的单元格。任何人都有任何想法如何使用ClosedXml?

always throws the expection "The given key was not present in the dictionary.", even for cells that do have background colours defined. Anyone have any ideas how to get this working with ClosedXml?

推荐答案

OpenXml 及其包装器 - ClosedXml 为您提供了很多有用的功能。

OpenXml and its wrapper - ClosedXml gives you lot useful functions.

这篇关于在C#中读取Excel数据和单元格格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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