Excel Date列使用EPPlus返回INT

时间:2014-07-24 12:39:27

标签: c# excel epplus

所以我使用EPPlus来读写excel文档。

工作流

  • 用户生成已填充的Excel文档
  • 打开文档并添加一行
  • 已上传并阅读

当我使用EPPlus创建文档时生成的日期在我读回值时正确显示但是用户更改日期一行或添加的行显示为INT值而不是我可以用作的值一个真实的约会。

当我输入日期 1/01/2014 并写下来时,我打开文件时的输出显示 41640

我按照以下方式阅读

sheet.Cells[i, "AE".ConvertExcelColumnIndex()].Value != null
     ? sheet.Cells[i, "AE".ConvertExcelColumnIndex()].Value.ToString().Trim()
         : string.Empty

更新

导出文件时,我添加了以下内容

DateTime testDate;

if (DateTime.TryParse(split[i], out testDate))
{
    sheet.Cells[row, i + 1].Style.Numberformat.Format = "MM/dd/yyyy";
    sheet.Cells[row, i + 1].Value = testDate.ToString("MM/dd/yyyy");
}

同样在阅读价值时,我试过了

sheet.Cells[i, "AE".ConvertExcelColumnIndex()].Style.Numberformat.Format = "MM/dd/yyy";

我还是回来了

4 个答案:

答案 0 :(得分:36)

  

...当我需要阅读那个excel文件时,唯一的日期   不正确的是用户已更改的

因此,当您阅读修改后的excel表时,修改日期是数字,而未更改的值是日期格式的字符串?

您可以通过DateTime获取DateTime.FromOADate

long dateNum = long.Parse(worksheet.Cells[row, column].Value.ToString());
DateTime result = DateTime.FromOADate(dateNum);

使用您的样本编号:

Console.Write(DateTime.FromOADate(41640)); // ->  01/01/2014 

答案 1 :(得分:0)

今天,当我尝试从某些ASP.NET DataTables生成一些Excel文档时,我偶然发现了这个问题:我对字符串没有问题,但是遇到了一些数字类型(int,double,decimals)和DataTables的问题。格式为字符串或数字表示形式(OADate)。

这是我最终设法实现的解决方案:

$ sudo update-alternatives --config python
There are 3 choices for the alternative python (providing /usr/bin/python).

  Selection    Path                Priority   Status
------------------------------------------------------------
  0            /usr/bin/python3.7   2         auto mode
  1            /usr/bin/python2.7   0         manual mode
  2            /usr/bin/python3.4   1         manual mode
* 3            /usr/bin/python3.7   2         manual mode

Press enter to keep the current choice[*], or type selection number: 3
$ make clean;make
0.00ns INFO     Running on Icarus Verilog version 11.0 (devel)
0.00ns INFO     Python interpreter initialised and cocotb loaded!
0.00ns INFO     Running tests with Cocotb v1.0.1 from /opt/cocotb
0.00ns INFO     Seeding Python random module with 1554105931
0.00ns INFO     Found test test_ttl.ttl_test
0.00ns INFO     Running test 1/1: ttl_test
0.00ns INFO     Starting test: "ttl_test"
                Description:  simple ttl test function 
[...]
3.4.2 (default, Feb  7 2019, 06:11:23) 
[...]

显然,技巧是将值设置为if (dc.DataType == typeof(DateTime)) { if (!r.IsNull(dc)) { ws.SetValue(row, col, (DateTime)r[dc]); // Change the following line if you need a different DateTime format var dtFormat = "dd/MM/yyyy"; ws.Cells[row, col].Style.Numberformat.Format = dtFormat; } else ws.SetValue(row, col, null); } ,然后根据需要配置适当的DateTime

我发布了完整的代码示例(使用EPPlus将数据表转换为Excel文件)in this post on my blog

答案 2 :(得分:0)

您可以检查单元格格式是否为日期格式, 然后将其解析为最新的

var cell = worksheet.Cells[row, col];
  value = cell.Value.ToString();
  if (cell.Style.Numberformat.Format == "[$-409]d\\-mmm\\-yy;@")
  {
 string inputString = DateTime.FromOADate(long.Parse(value.ToString())).ToString("dd-MMM-yyyy");
 
 }

答案 3 :(得分:-1)

您还可以更改“NumberFormatLocal”属性。这对我有用。如果在使用EPPLUS对其进行压缩之前格式化Excel文件。

以下基本代码示例格式化典型excel文件中的A列。

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

End Sub