简体   繁体   English

C# DateTime 从 Excel 变成浮点数

[英]C# DateTime from Excel turns into float number

I have an excel file in the first column which contains dates in the format dd.MM.yyyy hh:mm:ss .我在第一列中有一个 excel 文件,其中包含格式dd.MM.yyyy hh:mm:ss的日期。 I'm trying to display data from an excel table in datagridview, but the date is displayed as a float number.我试图在 datagridview 中显示 excel 表中的数据,但日期显示为浮点数。

I tried to convert the date to the desired format in this way, but it does not work: worksheet.Cells[2, 1, endCell.Row, 1].Style.Numberformat.Format = "dd.MM.yyyy hh:mm:ss";我试图以这种方式将日期转换为所需的格式,但它不起作用: worksheet.Cells[2, 1, endCell.Row, 1].Style.Numberformat.Format = "dd.MM.yyyy hh:mm:ss";

The full code of my method:我的方法的完整代码:

public static DataTable readTableFromExcel(FileInfo file)
        {
            DataTable table = new DataTable();

            Console.WriteLine(file.Exists);

            using (ExcelPackage package= new ExcelPackage(file))
            {
                ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
                ExcelWorksheet worksheet = package.Workbook.Worksheets[0];

                ExcelCellAddress startCell = worksheet.Dimension.Start;
                ExcelCellAddress endCell = worksheet.Dimension.End;

                ExcelRange range = worksheet.Cells[startCell.Row, startCell.Column, endCell.Row, endCell.Column];
                ExcelTable excelTable = worksheet.Tables.Add(range, "table");

                Console.WriteLine(worksheet.Cells[2, 1].Value.ToString());

                table = excelTable.ToDataTable();
            }
            
            return table;
        }

String with Console.Writeline outputs 44912,0912268519 instead of 17.12.2022 2:11:22.带有 Console.Writeline 的字符串输出 44912,0912268519 而不是 17.12.2022 2:11:22。

Then I output the data to datagridview: tableView.DataSource = table;然后我把output的数据传给datagridview: tableView.DataSource = table;

And it looks like: https://i.stack.imgur.com/aXx6V.png它看起来像: https://i.stack.imgur.com/aXx6V.png

File used: https://drive.google.com/drive/folders/1hXKmKs_F7EyO5GdVU3HVATxDLlFWO4jk?usp=share_link使用的文件: https://drive.google.com/drive/folders/1hXKmKs_F7EyO5GdVU3HVATxDLlFWO4jk?usp=share_link

How can I display the datetime correctly?如何正确显示日期时间?

In Excel, dates and times are stored as a floating point number representing the number of days since the epoch date of January 1, 1970. This means that when you read a date or time value from an Excel file into a C# DateTime object, you will need to convert the floating point value to a DateTime object.在 Excel 中,日期和时间存储为浮点数,表示自纪元日期 1970 年 1 月 1 日以来的天数。这意味着当您将日期或时间值从 Excel 文件读取到 C# DateTime object 时,您将需要将浮点值转换为 DateTime object。

// Assume that the Excel date value is stored in a variable called "excelDate"

// Convert the Excel date value to a DateTime object
DateTime dateTime = DateTime.FromOADate(excelDate);

system.datetime.fromoadate 系统.datetime.fromoadate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM