I need to read the DateTime from a cell from a xls file, but the cell contents is invalid, instead of returning the contents as a date or string it returns a number.
The code to read the file:
using System.IO;
using ExcelLibrary.SpreadSheet;
namespace XLSX_reader
{
class Program
{
private static ExcelDocument XlsDocument { get; set; }
static void Main(string[] args)
{
var inputStream = new MemoryStream(File.ReadAllBytes("d:\\Work\\Flexportal_Other\\Importers\\11-2016_Uitzendrapportage_SRS_Personeel_85873112442611460665848.xls"));
XlsDocument = new ExcelDocument(inputStream, null);
Cell cell1 = XlsDocument.GetCell(6, 4);
XlsDocument.GetCell(5, 7);
var date = ExcelDocument.ParseDateTime(cell1);
}
}
}
ExcellDocument class:
namespace XLSX_reader
{
public class ExcelDocument
{
public ExcelDocument(MemoryStream stream, string sheetName = null)
{
Workbook = LoadWorkbook(stream);
if (Workbook == null)
{
throw new InvalidOperationException("err");
}
Worksheet = LoadWorksheet(sheetName);
}
public Worksheet LoadWorksheet(string sheetName = null)
{
if (Workbook == null)
{
throw new InvalidOperationException(
"##(Load workbook before loading worksheets)(Open eerst een werkboek alvorens een werkblad te openen)##");
}
Worksheet = sheetName != null
? Workbook.Worksheets.FirstOrDefault(ws => ws.SheetType == SheetType.Worksheet && ws.Name == sheetName)
: Workbook.Worksheets.FirstOrDefault(ws => ws.SheetType == SheetType.Worksheet);
if (Worksheet == null)
{
throw new InvalidOperationException("##noWorksheetLoaded##");
}
RowIndexMin = Worksheet.Cells.FirstRowIndex;
RowIndexMax = Worksheet.Cells.LastRowIndex;
_rowIndex = RowIndexMin - 1;
return Worksheet;
}
public Cell GetCell(int row, int column)
{
return Worksheet.Cells[row, column];
}
}
}
And the reading of the cell looks like this:
So the outcome is 42443, not a date or anything. Do you have any idea why this happens and how can I fix it? Thanks