简体   繁体   English

如何将Value2字符串转换为DateTime(Excel Interop)

[英]How do I convert a Value2 string to a DateTime (Excel Interop)

I am importing data from an Excel file and converting it into a DataSet, which I have working (kind of). 我正在从Excel文件中导入数据,并将其转换为我正在使用的DataSet。 The problem that I am having is that two fields are Dates in Excel but when I do the import they get turned into numbers and I am not sure how to get them back to dates. 我遇到的问题是两个字段是Excel中的“日期”,但是当我执行导入时,它们变成了数字,而且我不确定如何将它们恢复为日期。 The last two columns are the columns that are dates (HDate & CDate). 最后两列是日期(HDate和CDate)。

This is the code 这是代码

    private void FillDatagrid()
    {
        Excel.Application xlApp = new Excel.Application();
        Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"\\Server01\Ins\Eligible.xls");
        Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
        Excel.Range xlRange = xlWorksheet.UsedRange;

        DataTable table = new DataTable("Employees");
        table.Columns.Add("StoreID");
        table.Columns.Add("EmpID");
        table.Columns.Add("EmpName");
        table.Columns.Add("Position");
        table.Columns.Add("HDate");
        table.Columns.Add("CDate");

        int rowCount = xlRange.Rows.Count;

        for (int i = 2; i <= rowCount; i++)
        {
            table.Rows.Add(
                xlRange.Cells[i, 1].Value2.ToString(),
                xlRange.Cells[i, 2].Value2.ToString(),
                xlRange.Cells[i, 3].Value2.ToString(),
                xlRange.Cells[i, 4].Value2.ToString(),
                xlRange.Cells[i, 5].Value2.ToString(),
                xlRange.Cells[i, 5].Value2.ToString());
        }

        DataSet ds = new DataSet();
        ds.Tables.Add(table);
        dataGrid.ItemsSource = ds.Tables["Employees"].DefaultView;
    }

As far as I can tell a Value2 will only convert to a string. 据我所知,Value2只会转换为字符串。

Note #1 Although I am putting this data into a DataGrid in this sample I am not doing that in the actual code block. 注意#1尽管在本示例中将这些数据放入DataGrid中,但在实际的代码块中并未这样做。 I just wanted to mention that so that it didn't seem like this could be fixed with formatting in XAML etc. 我只是想提一提,这样看来似乎无法通过XAML等格式进行修复。

Note #2 I realize I have xlRange.Cells[i, 5].Value2.ToString() twice. 注意#2我意识到我有两次xlRange.Cells [i,5] .Value2.ToString()。 I am doing so to get around the problem of column 6 have null values, which my import didn't like. 我这样做是为了解决第6列具有空值的问题,我的导入不喜欢该值。 I plan to come back to that after I get this problem fixed. 解决此问题后,我打算再回到这一点。

Note #3 When I say I am getting the date as a string I mean it is coming from Excel as a string but formatted as a number so, for instance, the cell data has a date like 6/30/2015 but it comes over to my dataset like 42185. 注意#3当我说我以字符串形式获取日期时,是指它以字符串形式来自Excel,但格式为数字,因此,例如,单元格数据的日期类似于6/30/2015,但它会出现到我的数据集,例如42185。

What about: 关于什么:

table.Columns.Add("HDate", typeof(DateTime));
table.Columns.Add("CDate", typeof(DateTime));

I was able to solve this by altering my loop 我可以通过更改循环来解决此问题

        for (int i = 2; i <= rowCount; i++)
        {
            string storeId = xlRange.Cells[i, 1].Value2.ToString();
            string employeeId = xlRange.Cells[i, 2].Value2.ToString();
            string employeeName = xlRange.Cells[i, 3].Value2.ToString();
            string position = xlRange.Cells[i, 4].Value2.ToString();
            string hDate = Convert.ToString(xlRange.Cells[i, 5].Value);
            string cDate = Convert.ToString(xlRange.Cells[i, 6].Value);

            table.Rows.Add(storeId, employeeId, employeeName, position, hDate, cDate);
        }

This also took care of another problem. 这也解决了另一个问题。 Which is to say the Convert.ToString(xxx.Value) handles nulls where the Value2.ToString() would not. 也就是说,Convert.ToString(xxx.Value)处理的是null,而Value2.ToString()则不会。

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

相关问题 C# 互操作 Excel:可以像 Value2 一样设置 NumberFormat 吗? - C# interop Excel: is possible to set NumberFormat like Value2? 如何将DateTime转换为字符串? - How do I convert DateTime into string? C# 使用 Microsoft.Office.Interop.Excel 错误设置范围 .Formula 到范围 .Value2 - C# Using Microsoft.Office.Interop.Excel Error Setting Range .Formula to Range .Value2 如何将日期的字符串值(如Mon,5/5/2014转换为datetime dd / MM / yyyy)? - How do I convert a string value of date like Mon, 5/5/2014 to datetime dd/MM/yyyy? 如何为二维对象 [,] 数组分配 Excel Interop Range.Value 的值? - How do I assign a 2 dimensional object[,] array the value of an Excel Interop Range.Value? 如何使用互操作将Excel单元格值设置为string.empty? - How can I set an excel cell value to string.empty using interop? 如何在C#中将以毫秒为单位的日期时间转换为字符串? - How do I convert a datetime with milliseconds to a string in C#? 如何将短日期字符串转换回DateTime对象? - How do I convert a short date string back to a DateTime object? 如何在c#中将此特定字符串转换为Datetime? - How Do I Convert this specific string to a Datetime in c#? Excel DateTime 是一个字符串如何转换为实际日期时间 C#? - Excel DateTime is a string how can I convert to real datetime C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM