相关疑难解决方法(0)

日期时间之间的差异在MSExcel和SQL Server中转换

今天我发现了一些非常奇怪的事情,同时将一个日期时间转换为excel中的文本,并使用它生成的数字转换为SQL Server中的datetime.

这有什么奇怪的?结果不同.准确地说两天的差异.

我在Excel中假设今天(20/05/2014 dd/MM/yyyy)的日期,并在文本中得到41779.

日期时间输入

转换为文字

我得到了文本值,我使用SQL转换来datetime检索值作为日期,我没有得到我想要的结果.

SQL转换为datetime

我甚至测试过datetime2但是我知道我无法转换intdatetime2

在此输入图像描述

我不是MS Excel专家,也不是SQL Server专家,但是发生了什么?我可以通过执行MS Excel生成的数字并删除2来使其工作,但对我来说仍然没有意义.

sql sql-server excel datetime

11
推荐指数
2
解决办法
7387
查看次数

.NET DateTime,与OADate进行转换时的分辨率是否不同?

我正在将DateTime转换为OADate.我希望在将OADa​​te转换回时获得完全相同的DateTime,但现在它只有毫秒级的分辨率,因此不同.

var a = DateTime.UtcNow;
double oadate = a.ToOADate();
var b = DateTime.FromOADate(oadate);
int compare = DateTime.Compare(a, b); 

//Compare is not 0; the date times are not the same
Run Code Online (Sandbox Code Playgroud)

来自a:634202170964319073

来自b的筹码:634202170964310000

OADate双倍:40437.290467951389

这是什么原因?DateTime的分辨率显然足够好.

.net c# datetime resolution ole-automation

7
推荐指数
2
解决办法
7749
查看次数

从 Excel 读取的随机日期格式

我正在使用 OleDb 从 Excel 文件读取数据。读取数据的代码如下:

OleDbCommand oleDbCommand = new OleDbCommand(selectCommandText, oleDbConnection);

using (OleDbDataReader dr = oleDbCommand.ExecuteReader())
{
    DataTable dt = new DataTable();
    dt.Load(dr);
    return dt;
}
Run Code Online (Sandbox Code Playgroud)

问题是读取的数据有时随机出现为字符串(例如“16.02.1995”)或类似数字时间戳(41187),类似这样将 Excel Date Serial Number 转换为 Regular Date

有什么办法可以解决这个问题吗?我想始终以某种格式读取数据,无论它是数字还是字符串。

编辑:我发现当我打开Excel文件时,读取的日期是数字格式(日期序列号),当我没有打开文件时,日期是字符串格式。有人知道为什么吗?

Edit2:日期单元格中使用的个性化格式

个性化格式

c# excel date

5
推荐指数
1
解决办法
444
查看次数

Npoi ICell.DateCellValue 返回 NullReferenceException

我有这个 Excel 列:

在此处输入图片说明

格式化为日期数据格式:

在此处输入图片说明

NullReferenceException如果我尝试读取 DateTime 值,我就会明白。

在此处输入图片说明

你知道这里有什么问题以及如何解决吗?是否可以以某种方式将数字转换为 DateTime?例如,当我更改为数字格式时,31/12/9999 是 2958465。

ICell 成字符串扩展

public static class NpoiExtension
{
    public static string GetStringValue(this ICell cell)
    {
        switch (cell.CellType)
        {
            case CellType.Numeric:
                if (DateUtil.IsCellDateFormatted(cell)) 
                {
                    try
                    {
                        return cell.DateCellValue.ToString();   
                    }
                    catch (NullReferenceException)
                    {
                        // /sf/ask/1052839721/
                        //var prevCulture = Thread.CurrentThread.CurrentCulture;
                        //CultureInfo customCulture = new CultureInfo("en-GB", false);
                        //Thread.CurrentThread.CurrentCulture = customCulture;

                        string dateOutput = cell.DateCellValue.ToString();

                        //Thread.CurrentThread.CurrentCulture = prevCulture;
                        return dateOutput;
                    }
                }
                else
                {
                    return cell.NumericCellValue.ToString();
                }
            case CellType.String:
                return cell.StringCellValue;

            case CellType.Boolean:
                return …
Run Code Online (Sandbox Code Playgroud)

c# datetime-format npoi

3
推荐指数
1
解决办法
2292
查看次数