EPPlus:date列返回int而不是实际显示的文本值

我正在使用EPPlus来读取excel数据。 我需要显示在网格中的Excel单元格的用户。

而阅读date列EPPlus正在给出date的OADate int值。

有没有一种方法可以读取string的值,用户在打开excel时通常会看到这些值。 即应用excel格式后的值。

似乎没有提供valueAsString或valueAsDisplayed等等的CELL类的任何function…

如果将date存储在excel中作为双精度(如果没有时间分量,则为整数),这将是“正确”的方式,您可以在代码中重新创builddate,并重新应用Excel中应用的date格式。 唯一的缺点是excel的格式与当时的略有不同.net特别是在区分大小写的情况下,所以你应该在那里确认MMM不是mmm(这会让你在.net分钟)。 所以这样的工作:

[TestMethod] public void OADate_Test() { //http://stackoverflow.com/questions/28046069/epplus-date-column-returning-int-rather-than-the-actual-displayed-text-value var existingFile = new FileInfo(@"c:\temp\temp.xlsx"); if (existingFile.Exists) existingFile.Delete(); using (var package = new ExcelPackage(existingFile)) { //.NET is case sensive so MMM must be capital but Excel might not be const string FORMATDATE = "dd-MMM-yyyy"; var mydate = new DateTime(2015, 1, 1); var datedouble = mydate.ToOADate(); var worksheet = package.Workbook.Worksheets.Add("Newsheet"); worksheet.Cells["A1"].Value = "As Date"; worksheet.Cells["A2"].Value = datedouble; worksheet.Cells["A2"].Style.Numberformat.Format = FORMATDATE; package.Save(); } using (var package = new ExcelPackage(existingFile)) { var worksheet = package.Workbook.Worksheets["Newsheet"]; worksheet.Cells["B1"].Value = "As String"; var datedouble = (double) worksheet.Cells["A2"].Value; worksheet.Cells["B2"].Value = DateTime.FromOADate(datedouble).ToString(worksheet.Cells["A2"].Style.Numberformat.Format); package.Save(); } } 

我还发现一个不太复杂的方法。 如果您将“NumberFormatLocal”而不是“NumberFormat”属性更改为“dd / mm / yyyy”,则会导入date而不是数字。

所以

Sub ChangeExcelColumnFormat()

  Dim ExcelApp As Excel.Application Dim ExcelWB As Excel.Workbook Dim ExcelWS As Excel.Worksheet Dim formatRange As Excel.Range Dim strFile As String = "C:\Test.xlsx" Dim strSheetname As String = "Sheet1" ExcelApp = New Excel.Application ExcelWB = ExcelApp.Workbooks.Open(strFile) strColSelect = "A:A" strFormat = "dd/mm/yyyy" formatRange = ExcelWS.Range(strColSelect) formatRange.NumberFormatLocal = strFormat ExcelWB.Save() ExcelWB.Close() ExcelApp.Quit() ExcelWS = Nothing ExcelWB = Nothing ExcelApp = Nothing 

结束小组