将excel单元格格式保存为“date like”数据。

时间:2022-09-17 20:25:28

This seems silly, but I haven't been able to get my values in the format of #/#### to write as the literal string rather than becoming formatted as a date within excel.

这看起来很傻,但是我还没能让#### ##格式的值作为文字字符串写入,而不是在excel中格式化为日期。

I'm using ClosedXML to write to excel, and using the following:

我使用ClosedXML来编写excel,并使用以下方法:

// snip
IXLRangeRow tableRow = tableRowRange.Row(1);
tableRow.Cell(1).DataType = XLCellValues.Text;
tableRow.Cell(1).Value = "2/1997";
// snip

Looking at the output excel sheet I get in the cell 2/1/1997 - even though I'm setting the format as text in code, I'm getting it as a "Date" in the excel sheet - I checked this by right clicking the cell, format cell, seeing "date" as the format.

查看单元格中得到的输出excel表(2/1/1997)——尽管我将格式设置为代码中的文本,但我将它设置为excel表中的“日期”——我通过右键单击单元格(format cell)检查它,将“日期”作为格式。

If I change things up to:

如果我把东西改成:

// snip
IXLRangeRow tableRow = tableRowRange.Row(1);
tableRow.Cell(1).Value = "2/1997";
tableRow.Cell(1).DataType = XLCellValues.Text;
// snip

I instead get 35462 as my output.

结果是35462。

I just want my literal value of 2/1997 to be displayed on the worksheet. Please advise on how to correct.

我只是想让我的2/1997的文字值显示在工作表上。请建议如何改正。

4 个解决方案

#1


20  

try this

试试这个

ws.Cell(rowCounter, colCounter).SetValue<string>(Convert.ToString(fieldValue));

#2


2  

Consider:

考虑:

tableRow.Cell(1).Value = "'2/1997";

Note the single quote.

注意单引号。

#3


1  

Not sure about from ClosedXML, but maybe try Range.NumberFormat (MSDN Link)

不确定从ClosedXML,但可以尝试Range。NumberFormat(MSDN链接)

For example...

例如……

Range("A1").NumberFormat = "@"

Or

Selection.NumberFormat = "#/####"

#4


0  

ws.Cell(rowCounter, colCounter).Value="'"+Convert.ToString(fieldValue));

#1


20  

try this

试试这个

ws.Cell(rowCounter, colCounter).SetValue<string>(Convert.ToString(fieldValue));

#2


2  

Consider:

考虑:

tableRow.Cell(1).Value = "'2/1997";

Note the single quote.

注意单引号。

#3


1  

Not sure about from ClosedXML, but maybe try Range.NumberFormat (MSDN Link)

不确定从ClosedXML,但可以尝试Range。NumberFormat(MSDN链接)

For example...

例如……

Range("A1").NumberFormat = "@"

Or

Selection.NumberFormat = "#/####"

#4


0  

ws.Cell(rowCounter, colCounter).Value="'"+Convert.ToString(fieldValue));