Wednesday, May 30, 2012

Getting Date from Excel using openXml

When iterated through excel and read cell contains date it returned int value. you can read cell contains date and convert to simple date format using code below:
  Cell cellDate;            
    string newDate;            
    using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open("2.xlsx", false))            
   {
                var sheets = spreadsheetDocument.WorkbookPart.Workbook.Descendants();

                foreach (Sheet sheet in sheets)
                {
                    var sheets = spreadsheetDocument.WorkbookPart.Workbook.Descendants();
                    foreach (Sheet sheet in sheets)
                   {
                       List news = new List();
                       uint rowIndex = 1;
                       uint colIndex = 1;
                       WorksheetPart worksheetPart = (WorksheetPart)spreadsheetDocument.WorkbookPart.GetPartById(sheet.Id);

                       Worksheet worksheet = worksheetPart.Worksheet;
                       SheetData sheetData = worksheet.GetFirstChild();
                       IEnumerator rows = sheetData
                                            .Elements().GetEnumerator();
                      while (rows.MoveNext())
                      {
                        Row row = rows.Current;
                        cellDate = GetCell(sheetData, "C" + rowIndex.ToString());
                        if (cellDate.DataType != null && cellDate.DataType == CellValues.SharedString)
                        {
                             newDate = GetSharedStringItemById(spreadsheetDocument.WorkbookPart, int.Parse(cellDate.CellValue.InnerText));
                        }
                        else
                        {
                           newDate = DateTime.FromOADate(double.Parse(((CellValue)cellDate.FirstChild).Text)).ToString();
                        }
              }
           }
        }
     }

   public static Cell GetCell(SheetData sheetData, string fullAddress)
   {
            return sheetData.Descendants()
                .Where(c => c.CellReference == fullAddress)
                .FirstOrDefault();
   }

    private static Cell GetCell(Worksheet worksheet, string columnName, uint rowIndex)
    {
          Row row = GetRow(worksheet, rowIndex);

          if (row == null)
                return null;

          return row.Elements().Where(c => string.Compare
                   (c.CellReference.Value, columnName +
                   rowIndex, true) == 0).FirstOrDefault();
    }


    // Given a worksheet and a row index, return the row.
    private static Row GetRow(Worksheet worksheet, uint rowIndex)
    {
          return worksheet.GetFirstChild().
              Elements().Where(r => r.RowIndex == rowIndex).FirstOrDefault();
    }

    public static string GetSharedStringItemById(WorkbookPart workbookPart, int id)
    {
          return workbookPart.SharedStringTablePart.SharedStringTable.Elements()
                .ElementAt(id).InnerText;
    }


No comments: