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

发布于 2024-12-27 16:27:54 字数 862 浏览 0 评论 0原文

我一直在网站上搜索和搜索答案,但我找不到解决方案 - 人们大多讨论如何向文档添加新的数字格式并应用它。

我需要的是以应用格式的字符串形式获取单元格值 - 即与 Excel 显示的字符串相同。

我已经认为没有简单的方法或内置函数可以返回单元格的现成格式化值。

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

但这两个步骤我都遇到问题。

我们可以轻松获取包含 NumberFormatId 的 CellFormat 实例:

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

但是,如果 id 对应于标准预定义格式之一,如何使用此 NumberFormatId 获取格式字符串呢? (即低于 160)它们不在电子表格文档中,我无法相信它们应该在应用程序中硬编码。

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

我发现 此页面 提到使用 Microsoft.Office.Excel.Interop,但是我宁愿只使用 OpenXML SDK。

总的来说,我感到非常惊讶的是,在网络上找到这个问题的明确答案是如此困难,因为我认为这将是许多开发人员在日常工作中需要的东西。

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.

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.

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.

One can easily get CellFormat instance which would contain NumberFormatId:

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

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.

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

铁轨上的流浪者 2025-01-03 16:27:54

伙计们,这是一个困难的...我将在这里添加我发现可能值得的东西..

首先是获取单元格的编号格式(一旦您有了 CellFormat:

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

有关此的更多信息,您可以去至: NumberingFormats

我试图找出如何应用此格式到 cell.CellValue 属性...我认为这就是你必须走的路!

好吧,阅读 ClosedXml 代码(其开源),似乎很容易获得格式

。将文本值转换为其类型(int、double 等)并调用传递格式的 ToString 方法。我尝试使用 String.Format 执行此操作,但没有成功。我测试了 ToString 并且它有效,但

我建议 仍然缺少一些内容。让你看看这个类并从中获取代码@El G 在他的评论中告诉的方法 GetFormattedString()

基本上你必须添加这样的内容:

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

希望它能帮助你......

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

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;

For more information about this you can go to: NumberingFormats

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

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

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.

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 you will have to add something like this:

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

Hope it helps you...

摘星┃星的人 2025-01-03 16:27:54

如果要采用应用格式的单元格值(与 Excel 中显示的相同),请使用 Cell 对象的 .Text 属性。像这样:

String formattedValue = cell.Text

If you want to take cell value with applied formatting, same as displayed in Excel, use .Text property of Cell object. Like this:

String formattedValue = cell.Text
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文