2

I need to retrieve an excel column of date. My first column 'A' values are formatted like this6/20/2016 10:44. I have no problem of retrieving column 'A' with this format using

using DocumentFormat.OpenXml;

double d = double.Parse(theCell.InnerText);
DateTime conv = DateTime.FromOADate(d).Date;

My second column 'B' is formatted as 6/20/2016. With no time, just date. but my problem is when i tried this code below:

using DocumentFormat.OpenXml;

double d = double.Parse(theCell.InnerText);
DateTime conv = DateTime.FromOADate(d).Date;

theCell.InnerText value is 1455

I am having a different value. the value changes into 12/25/1903 12:00:00 AM

How can I retrieve excel values with this kind of date format 6/30/2016 ?

8
  • Is the value in column B formatted as a date or is it actually a string? Commented Aug 2, 2016 at 2:05
  • Hi @dev1998 , thanks for checking my question. Column B are formatted as date. Commented Aug 2, 2016 at 18:49
  • It is all working for me, and I'm using the same 2 lines you are using. For the value that is wrong, what is the debugger showing for theCell.InnerText ? Commented Aug 2, 2016 at 23:58
  • if you put a breakpoint on the second set of code, what are the values of theCell.InnerText and d? 12/3/1909 is 3,265. Somehow the text in that cell is rendering that value. This sounds oversimplified, but it looks like you're not reading the cell you think you are reading. I don't know OpenXml, but if you were using COM I'd have some suggestions to debug. Commented Aug 3, 2016 at 3:21
  • Hi @Hambone, i edited my question. Thanks for looking at it. I have done some research and couldnt find a solution to this problem using OpenXml. Commented Aug 3, 2016 at 17:31

2 Answers 2

4

I located some code from here and modified it: open xml reading from excel file

I am thinking the same thing that Hambone is thinking, namely the Excel cell has something else in it, or you are not reading the cell you think you are.

Here is the code I am using, and it works for me:

using System;
using System.Linq;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;



namespace ConsoleApplication1
{
    class Program
    {

        private static void Main(string[] args)
        {
            var filePath = @"c:\xyz\stack_c_Sharp.xlsx";
            using (var document = SpreadsheetDocument.Open(filePath, false))
            {
                var workbookPart = document.WorkbookPart;
                var workbook = workbookPart.Workbook;

                var sheets = workbook.Descendants<Sheet>();
                foreach (var sheet in sheets)
                {
                    var worksheetPart = (WorksheetPart)workbookPart.GetPartById(sheet.Id);
                    var sharedStringPart = workbookPart.SharedStringTablePart;

                    string text;
                    var rows = worksheetPart.Worksheet.Descendants<Row>();
                    foreach (var row in rows)
                    {
                        Console.WriteLine();
                        int count = row.Elements<Cell>().Count();

                        foreach (Cell theCell in row.Elements<Cell>())
                        {

                            text = theCell.CellValue.InnerText;

                            double d = double.Parse(theCell.InnerText);
                            DateTime conv = DateTime.FromOADate(d).Date;

                            Console.Write(text + " ");
                            Console.Write(conv + " ");

                        }
                    }
                }
                Console.ReadLine();
            }


        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

I had opened the file while I was debugging on VS2022 and I had your same problem. Try closing the file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.