Excel格式在c#中等效。

时间:2022-12-03 17:23:48

I have an application that uses excel. The user needs to write an excel format in my application and I need to place it excel to some cells. However I need to provide an example to the user to know if the format will be correct or not.

我有一个使用excel的应用程序。用户需要在我的应用程序中编写excel格式,我需要将它放在一些单元格中。但是,我需要向用户提供一个示例,以了解格式是否正确。

I know in VBA there is the function FORMAT. Is there an equivalent in C# for this function?

我知道在VBA中有函数格式。c#中是否有与这个函数等价的东西?

It needs to work for example with the format "[$-409]d.m.yy h:mm AM/PM;@" but it has to support anything that the Excel supports.

例如,它需要使用“[$-409]d.m”格式。但是它必须支持Excel支持的任何东西。

1 个解决方案

#1


1  

This was bothering me too. I didn't want to have to apply NumberFormat to the cells, paste the values and then retrieve the text.

这也困扰着我。我不希望必须对单元格应用NumberFormat,粘贴值,然后检索文本。

After some investigation, I found the function that does this.

经过调查,我找到了这个功能。

WorksheetFunction.Text: https://msdn.microsoft.com/en-us/library/office/ff841121.aspx

WorksheetFunction。文本:https://msdn.microsoft.com/en-us/library/office/ff841121.aspx

The first argument is the value that you want to format, this could be a String, DateTime, Double, Integer etc, the second argument is the Excel NumberFormat string.

第一个参数是要格式化的值,可以是字符串、DateTime、Double、Integer等等,第二个参数是Excel数字格式字符串。

MessageBox.Show(ExcelApp.WorksheetFunction.Text(DateTime.Now, "[$-409]d.m.yy h:mm AM/PM;@"));

#1


1  

This was bothering me too. I didn't want to have to apply NumberFormat to the cells, paste the values and then retrieve the text.

这也困扰着我。我不希望必须对单元格应用NumberFormat,粘贴值,然后检索文本。

After some investigation, I found the function that does this.

经过调查,我找到了这个功能。

WorksheetFunction.Text: https://msdn.microsoft.com/en-us/library/office/ff841121.aspx

WorksheetFunction。文本:https://msdn.microsoft.com/en-us/library/office/ff841121.aspx

The first argument is the value that you want to format, this could be a String, DateTime, Double, Integer etc, the second argument is the Excel NumberFormat string.

第一个参数是要格式化的值,可以是字符串、DateTime、Double、Integer等等,第二个参数是Excel数字格式字符串。

MessageBox.Show(ExcelApp.WorksheetFunction.Text(DateTime.Now, "[$-409]d.m.yy h:mm AM/PM;@"));