Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My requirement is to read,write spreadsheets in excel file using OpenXml.
My spreadsheet cells are in many formats like numbers,strings,dates etc..
My code is reading everything except dateformat cell. one cell is a Custom dateformat(d-mmm), which is returning numbers (like 7-Aug is returning 41128). I am not getting resolved this...
I searched alot. pls help

my code is as below. Thanks advance..

C#
using (SpreadsheetDocument xl = SpreadsheetDocument.Open(filepath, true))
            {
                WorkbookPart wbp = xl.WorkbookPart;
                foreach (Sheet isheet in wbp.Workbook.Sheets)
                {
                    
                    string relationshipId = isheet.Id.Value;
                    WorksheetPart worksheetPart = (WorksheetPart)xl.WorkbookPart.GetPartById(relationshipId);
                    SheetData sheetData = worksheetPart.Worksheet.Elements<SheetData>().First();
                    IEnumerable<Row> rows = sheetData.Elements<Row>();
                    for (int irow = 1; irow <= rows.Count(); irow++)
                    {
                       
                        try
                        {
                            IEnumerable<Cell> cells = rows.ElementAt(irow).Elements<Cell>();
                            for (int Ccount = 0; Ccount < cells.Count(); Ccount++)
                            {
                                
                                string cellIndex = GetCellIndex(cells.ElementAt(Ccount));
    
                                WorksheetPart wsPart = (WorksheetPart)(wbp.GetPartById(isheet.Id));
    
                               
                                Cell theCell = wsPart.Worksheet.Descendants<Cell>().Where(c => c.CellReference == cellIndex).FirstOrDefault();
    
                                if (theCell != null)
                                {
                                    value = theCell.InnerText;
                                    if (theCell.DataType != null)
                                    {
                                        switch (theCell.DataType.Value)
                                        {
                                            case CellValues.SharedString:
                                                var stringTable = wbp.GetPartsOfType<SharedStringTablePart>().FirstOrDefault();
                                                if (stringTable != null)
                                                    value = stringTable.SharedStringTable.ElementAt(int.Parse(value)).InnerText;
                                                break;
    
                                            case CellValues.Date:
                                                {
                                                    int i = 0;
                                                    string s = value;
                                                    bool result = int.TryParse(s, out i);
                                                    if (result)
                                                        value = JulianToDateTime(Convert.ToInt32(value)).ToShortDateString();
                                                    else
                                                        value = "";
                                                }
                                                break;
                                            case CellValues.Boolean:
                                                switch (value)
                                                {
                                                    case "0":
                                                        value = "FALSE";
                                                        break;
                                                    default:
                                                        value = "TRUE";
                                                        break;
                                                }
                                                break;
                                        }
                                    }
                                }
                               
                            }
                        }
                        catch (ArgumentOutOfRangeException argex)
                        {
                            break;
                        }
                       
                    }
                    
                }
            }
Posted
Updated 25-Apr-13 21:17pm
v2

1 solution

 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900