使用Office Open Xml SDK以编程方式格式化Excel工作表中的日期

时间:2022-07-30 20:24:53

I'm building an Excel xlsx spreadsheet using the office open XML SDK. I can add dates to the sheet by converting them to their "AO" date representation and setting the Cell Value to number. I can't, however, figure out how to add the dd-mm-yy formatting to the cell.

我正在使用office open XML SDK构建Excel xlsx电子表格。我可以通过将日期转换为“AO”日期表示并将单元格值设置为数字来向表单添加日期。但是,我无法弄清楚如何将dd-mm-yy格式添加到单元格中。

I've seen a lot of complicated examples where you have to create a stylesheet from scratch, add the format to it and then reference that format, but I figure there must be an easier way. I'm creating my sheet from a template, and Excel already has builtin formats/styles. IMO I should be able to just load the stylesheet from the excel file I'm using as a template and then apply the format. Can't figure out how to do it though.

我已经看到很多复杂的例子,你必须从头开始创建一个样式表,添加格式然后引用那个格式,但我认为必须有一个更简单的方法。我正在从模板创建工作表,Excel已经内置了格式/样式。 IMO我应该能够从我用作模板的excel文件中加载样式表,然后应用格式。虽然无法弄清楚如何做到这一点。

2 个解决方案

#1


1  

If you're creating an xlsx from scratch you can't avoid creating the style sheet. You minimally need a style sheet and cell format element. The cell format element can reference a built-in date format in which case you can avoid the custom number format, but if you want a date format that isn't built-in you need a number format element too.

如果您从头开始创建xlsx,则无法避免创建样式表。您至少需要样式表和单元格格式元素。单元格格式元素可以引用内置日期格式,在这种情况下,您可以避免使用自定义数字格式,但如果您需要非内置日期格式,则还需要数字格式元素。

If you're creating an xlsx from a template xlsx, then you can reference a cell format that's already defined, but it can be very sensitive since the references are based only on index, so you are safer looking through the defined cell formats to find the one that matches the date format you want as opposed to hard-coding the cell format index.

如果您正在从模板xlsx创建xlsx,那么您可以引用已定义的单元格格式,但它可能非常敏感,因为引用仅基于索引,因此您可以更安全地查看定义的单元格格式以查找与您想要的日期格式匹配的那个,而不是硬编码单元格格式索引。

#2


0  

Try this - I used it when I was trying to make my own number formats. It's a good example: Advanced Styling in Excel OpenXML

试试这个 - 我在尝试制作自己的数字格式时使用过它。这是一个很好的例子:Excel OpenXML中的高级样式

You will still have to create a style sheet but this is a very good place to start. He has quite a few styles in his as he demonstrates what can be done. Your style sheet could be lots simpler if you only want the one format.

您仍然需要创建样式表,但这是一个非常好的起点。他在演示可以做的事情时,有很多风格。如果您只想要一种格式,那么您的样式表可以简单得多。

Also have a quick look at the list in this post. Your format may be built into Excel already. Built in Styles for Excel

还快速浏览一下这篇文章中的列表。您的格式可能已内置到Excel中。内置样式的Excel

#1


1  

If you're creating an xlsx from scratch you can't avoid creating the style sheet. You minimally need a style sheet and cell format element. The cell format element can reference a built-in date format in which case you can avoid the custom number format, but if you want a date format that isn't built-in you need a number format element too.

如果您从头开始创建xlsx,则无法避免创建样式表。您至少需要样式表和单元格格式元素。单元格格式元素可以引用内置日期格式,在这种情况下,您可以避免使用自定义数字格式,但如果您需要非内置日期格式,则还需要数字格式元素。

If you're creating an xlsx from a template xlsx, then you can reference a cell format that's already defined, but it can be very sensitive since the references are based only on index, so you are safer looking through the defined cell formats to find the one that matches the date format you want as opposed to hard-coding the cell format index.

如果您正在从模板xlsx创建xlsx,那么您可以引用已定义的单元格格式,但它可能非常敏感,因为引用仅基于索引,因此您可以更安全地查看定义的单元格格式以查找与您想要的日期格式匹配的那个,而不是硬编码单元格格式索引。

#2


0  

Try this - I used it when I was trying to make my own number formats. It's a good example: Advanced Styling in Excel OpenXML

试试这个 - 我在尝试制作自己的数字格式时使用过它。这是一个很好的例子:Excel OpenXML中的高级样式

You will still have to create a style sheet but this is a very good place to start. He has quite a few styles in his as he demonstrates what can be done. Your style sheet could be lots simpler if you only want the one format.

您仍然需要创建样式表,但这是一个非常好的起点。他在演示可以做的事情时,有很多风格。如果您只想要一种格式,那么您的样式表可以简单得多。

Also have a quick look at the list in this post. Your format may be built into Excel already. Built in Styles for Excel

还快速浏览一下这篇文章中的列表。您的格式可能已内置到Excel中。内置样式的Excel