保持excel单元格格式为“date like”数据的文本

这看起来很愚蠢,但是我还没有能够以#/####的格式获取我的值作为文字string,而不是格式化为Excel中的date。

我正在使用ClosedXML写入Excel,并使用以下内容:

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

看看输出excel表格我在单元格获得2/1/1997年2 2/1/1997 – 即使我设置格式为代码中的文本,我把它作为Excel表单中的“date” – 我检查了这个权利单击单元格,格式化单元格,将“date”作为格式。

如果我改变了:

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

我反而得到35462作为我的输出。

我只想要我的文字值2/1997被显示在工作表上。 请告知如何更正。

尝试这个

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

考虑:

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

请注意单引号。

不知道从ClosedXML,但也许尝试Range.NumberFormat( MSDN链接 )

例如…

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

要么

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