我需要从xls文件的单元格中读取DateTime,但单元格内容无效,而不是将内容作为日期或字符串返回,而是返回一个数字。
读取文件的代码:
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类:
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];
}
}
}
结果是42443,而不是约会或任何事情。 你知道为什么会发生这种情况吗?我该如何解决? 感谢
答案 0 :(得分:1)
Excel将日期存储为数字,特别是自1900年1月1日以来的天数。 Microsoft article
Excel将所有日期存储为整数,将所有日期存储为小数部分。 使用此系统,Excel可以添加,减去或比较日期和时间 就像任何其他数字一样,所有日期都是通过使用来操纵的 这个系统。
在此系统中,序列号1代表上午12:00至凌晨1点1分。 时间存储为.0和.99999之间的十进制数,其中.0是 00:00:00和.99999是23:59:59。日期整数和时间小数 可以组合分数以创建具有小数和数字的数字 整数部分。例如,数字32331.06表示日期 和时间7/7/1988 1:26:24 a.m。