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

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

问题描述

我要寻找一个图书馆,让我看在C#中的Excel文档,但我需要是能够读取单元格格,以及数据在S preadsheet。我发现这个有用的问题,列出了很多不同的选择读取Excel文档,但很多这里提供只允许所述s preadsheet内容的解决方案,以被读取,而不是电池特性(例如,字体,背景颜色等)。对于我的具体问题,不过,我需要读取,它包含了大量的单元格格式的文件,我需要能够读取该格式信息。

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中的一个服务器上运行的库,所以它必须是一个独立的解决方案和preferably开源。

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因为从我读到它,它似乎提供了我需要的功能。我从这个样本code工作:

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:

<一个href="http://closedxml.$c$cplex.com/wikipage?title=Finding%20and%20extracting%20the%20data&referringTitle=Documentation" rel="nofollow">http://closedxml.$c$cplex.com/wikipage?title=Finding%20and%20extracting%20the%20data&referringTitle=Documentation

和已经能够读取Excel文档的内容,而不问题。我已经使用只是分类的例子,从这个code,但格式化两个类别名称细胞有背景颜色。现在,我试图做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)

下面是我试图用一些code:

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

不过,code

But the code

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

总是抛出厚望的给出的关键字不是present在字典中。的,甚至对于那些具有定义背景色细胞。任何人有任何想法如何得到这个工作与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天全站免登陆