如何获取单元格的值与应用的格式(格式的单元格值)的OpenXML SDK单元格、格式、SDK、OpenXML

2023-09-03 00:27:05 作者:無奈伤痛

我一直在使用Google和搜索网站上的答案,但我无法找到一个解决方案 - 无处不在的人大多​​是讨论如何添加新的数字格式的文档,并将其应用于

I've been googling and searching on the site for the answer, but I couldn't find a solution - everywhere people mostly discuss how to add new number format to the document and apply it.

我需要的是让单元格的值作为一个字符串应用格式 - 即相同的字符串会用Excel显示

What I need is to get the cell value as a string with applied formatting - i.e. same string as would be displayed by Excel.

我已经想通,有没有简单的方法或内置函数将返回现成格式化值的单元格。

I already figured that there's no easy way or built-in function which would return the readymade formatted value for a cell.

所以,在我看来,要想让我需要做两件事情的价值:  1.获取格式字符串。  2.使用此字符串格式的单元格的值。

So it seems to me that to get the value I need to do two things: 1. Get the format string. 2. Format the cell value using this string.

不过,我有问题,这两个步骤。

But I have problems with both steps.

我们可以很容易地得到CellFormat例如其中将包括NumberFormatId:

One can easily get CellFormat instance which would contain NumberFormatId:

CellFormat cellFormat = (CellFormat) document.WorkbookPart.WorkbookStylesPart.Stylesheet.CellFormats.ElementAt(cell.StyleIndex);

可是如何才能让格式字符串与此NumberFormatId,如果ID对应于标准predefined格式之一? (即低于160),他们不是在S preadsheet文档中,我不能相信他们应该很难codeD中的应用程序。

But how to get the format string with this NumberFormatId, if the id corresponds to one of standard predefined formats? (i.e. is below 160) They are not in the spreadsheet document and I can't believe that they should be hardcoded in the application.

此外,一旦格式字符串以某种方式获得,如何将其应用到单元格的值?到目前为止,我明白了,code应该检查单元格的值的类型,如果是数 - 使用该格式字符串将其转换为字符串

Also, once the format string is somehow obtained, how to apply it to the cell value? So far I understand, the code should check the type of the cell value and if is Number - convert it to string using the format string.

我发现其中提到使用Microsoft.Office.Excel此页面。互操作,但我会preFER留OpenXML的SDK只。

I found this page which mentions using Microsoft.Office.Excel.Interop, but I would prefer to stay with OpenXML SDK only.

总的来说,我很惊讶,它是如此难以找到一个明确的答案,因为我认为这会是很多开发者需要在日常工作中在网络上这个问题。

Overall, I'm very surprised that it's so difficult to find a definitive answer to this question on the Web as I thought that this would be something which many developers need in their daily work.

推荐答案

男人,这是一个很难的......我会增加这里的事情,我发现这可能是值得..

Men, this is a hard one... I will be adding here things that i found that could be worth..

首先是让电池的编号格式(一旦你有CellFormat:

First is to get the numbering format of the cell (once you have the CellFormat:

string format = excel.WorkbookPart.WorkbookStylesPart.Stylesheet.NumberingFormats.Elements<NumberingFormat>()
            .Where(i => i.NumberFormatId.ToString() == cellFormat.NumberFormatId.ToString())
            .First().FormatCode;

有关它的更多信息,你可以去:NumberingFormats

For more information about this you can go to: NumberingFormats

林试图找出如何应用该格式的 cell.CellValue 属性...我想这就是你必须走的路!

Im trying to find out how to apply this format to the cell.CellValue property... I think thats the way you have to go!

好了,看完ClosedXml code(其开源),似乎是很容易获得的格式。

Ok, reading the ClosedXml code (its open source), seems to be easy to get the format.

简单的价值文本转换为它的类型(整型,双等),并调用ToString方法传递的格式。我试图做到这一点的的String.Format并没有工作。香港专业教育学院测试的ToString和它的作品,但一些人仍下落不明。

Simply convert the value text to its type (int, double, etc) and call the ToString method passing the format. I was trying do that with the String.Format and didnt work. Ive tested the ToString and it works, but something still missing.

我建议你看一下这个类,并从方法GetFormattedString(在code)根据@El摹告诉他的评论。

I recommend to you to look at this class and get the code from the method GetFormattedString() as @El G tell in his comment.

Bassicaly你将需要添加这样的:

Bassicaly you will have to add something like this:

double d = double.Parse(cell.CellValue.InnerText);
string val = d.ToString(format);

希望它可以帮助你......

Hope it helps you...