Come gestire le date e gli orari
Come archiviare date e orari in Excel
Date e orari sono archiviati nelle celle come numeri. Quindi, i valori delle celle che contengono date e orari sono di tipo numerico. Un numero che specifica una data e un’ora è composto dalle componenti di data (parte intera) e ora (parte frazionaria). La proprietà Cell.DoubleValue restituisce questo numero.
Come visualizzare date e orari in Aspose.Cells
Per visualizzare un numero come data e ora, applica il formato di data e ora richiesto a una cella tramite la proprietà Style.Number o Style.Custom. La proprietà CellValue.DateTimeValue restituisce l’oggetto DateTime, che specifica la data e l’ora rappresentate dal numero contenuto in una cella.
Come passare da un sistema di date a un altro in Aspose.Cells
MS-Excel archivia le date come numeri chiamati valori seriali. Un valore seriale è un intero che rappresenta il numero di giorni trascorsi dal primo giorno nel sistema di date. Excel supporta i seguenti sistemi di data per i valori seriali:
- Il sistema di data 1900. La prima data è il 1 gennaio 1900 e il suo valore seriale è 1. L’ultima data è il 31 dicembre 9999 e il suo valore seriale è 2.958.465. Questo sistema di data è utilizzato nel foglio di lavoro per impostazione predefinita.
- Il sistema di data 1904. La prima data è il 1 gennaio 1904 e il suo valore seriale è 0. L’ultima data è il 31 dicembre 9999 e il suo valore seriale è 2.957.003. Per utilizzare questo sistema di data nel foglio di lavoro, impostare la proprietà Workbook.Settings.Date1904 su true.
Questo esempio mostra che i valori seriali archiviati nella stessa data in sistemi di date diversi sono diversi.
//Instantiating an Workbook object | |
Workbook workbook = new Workbook(); | |
workbook.Settings.Date1904 = false; | |
//Obtaining the reference of the newly added worksheet | |
Worksheet ws = workbook.Worksheets[0]; | |
Cells cells = ws.Cells; | |
DateTime dateData = new DateTime(2023, 11, 23); | |
//Setting the DateTime value to the cells | |
Cell a1 = cells["A1"]; | |
a1.PutValue(dateData); | |
// Check if the cell contains a numeric value | |
if (a1.Type == CellValueType.IsNumeric) | |
{ | |
Console.WriteLine("A1 is Numeric Value: " + a1.DoubleValue); | |
} | |
workbook.Settings.Date1904 = true; | |
Console.WriteLine("use The 1904 date system===================="); | |
//Setting the DateTime value to the cells | |
Cell a2 = cells["A2"]; | |
a2.Value = dateData; | |
// Check if the cell contains a numeric value | |
if (a2.Type == CellValueType.IsNumeric) | |
{ | |
Console.WriteLine("A2 is Numeric Value: " + a2.DoubleValue); | |
} |
A1 is Numeric Value: 45253
use The 1904 date system====================
A2 is Numeric Value: 43791
Come impostare il valore di data e ora in Aspose.Cells
Questo esempio imposta un valore di data e ora nella cella A1 e A2, imposta il formato personalizzato di A1 e il formato numerico di A2, e quindi restituisce i tipi di valore.
//Instantiating an Workbook object | |
Workbook workbook = new Workbook(); | |
//Obtaining the reference of the newly added worksheet | |
Worksheet ws = workbook.Worksheets[0]; | |
Cells cells = ws.Cells; | |
//Setting the DateTime value to the cells | |
Cell a1 = cells["A1"]; | |
a1.PutValue(DateTime.Now); | |
// Check if the cell contains a numeric value | |
if (a1.Type == CellValueType.IsNumeric) | |
{ | |
Console.WriteLine("A1 is Numeric Value: " + a1.IsNumericValue); | |
} | |
Style a1Style = a1.GetStyle(); | |
// Set custom Datetime style | |
a1Style.Custom = "mm-dd-yy hh:mm:ss"; | |
a1.SetStyle(a1Style); | |
// Check if the cell contains a DateTime value | |
if (a1.Type == CellValueType.IsDateTime) | |
{ | |
Console.WriteLine("Cell A1 contains a DateTime value."); | |
} | |
else | |
{ | |
Console.WriteLine("Cell A1 does not contain a DateTime value."); | |
} | |
//Setting the DateTime value to the cells | |
Cell a2 = cells["A2"]; | |
a2.Value = DateTime.Now; | |
// Check if the cell contains a numeric value | |
if (a2.Type == CellValueType.IsNumeric) | |
{ | |
Console.WriteLine("A2 is Numeric Value: " + a2.IsNumericValue); | |
} | |
Style a2Style = a2.GetStyle(); | |
// Set the display format of numbers and dates. | |
a2Style.Number = 22; | |
a2.SetStyle(a2Style); | |
// Check if the cell contains a DateTime value | |
if (a2.Type == CellValueType.IsDateTime) | |
{ | |
Console.WriteLine("Cell A2 contains a DateTime value."); | |
} | |
else | |
{ | |
Console.WriteLine("Cell A2 does not contain a DateTime value."); | |
} |
Risultato in output:
A1 is Numeric Value: True
Cell A1 contains a DateTime value.
A2 is Numeric Value: True
Cell A2 contains a DateTime value.
Come ottenere il valore di data e ora in Aspose.Cells
Questo esempio imposta un valore di data e ora nella cella A1 e A2, imposta il formato personalizzato di A1 e il formato numerico di A2, verifica i tipi di valore delle due celle, e quindi restituisce il valore di data e ora e la stringa formattata.
//Instantiating an Workbook object | |
Workbook workbook = new Workbook(); | |
//Obtaining the reference of the newly added worksheet | |
Worksheet ws = workbook.Worksheets[0]; | |
Cells cells = ws.Cells; | |
//Setting the DateTime value to the cells | |
Cell a1 = cells["A1"]; | |
a1.PutValue(DateTime.Now); | |
// Check if the cell contains a numeric value | |
if (a1.Type == CellValueType.IsNumeric) | |
{ | |
Console.WriteLine("A1 is Numeric Value: " + a1.IsNumericValue); | |
} | |
Style a1Style = a1.GetStyle(); | |
// Set custom Datetime style | |
a1Style.Custom = "mm-dd-yy hh:mm:ss"; | |
a1.SetStyle(a1Style); | |
// Check if the cell contains a DateTime value | |
if (a1.Type == CellValueType.IsDateTime) | |
{ | |
Console.WriteLine("Cell A1 contains a DateTime value."); | |
// Get the DateTime value | |
DateTime dateTimeValue = a1.DateTimeValue; | |
// Now, you can use dateTimeValue as needed | |
Console.WriteLine("A1 DateTime Value: " + dateTimeValue); | |
// Output date formatted string | |
Console.WriteLine("A1 DateTime String Value: " + a1.StringValue); | |
} | |
else | |
{ | |
Console.WriteLine("Cell A1 does not contain a DateTime value."); | |
} | |
//Setting the DateTime value to the cells | |
Cell a2 = cells["A2"]; | |
a2.Value = DateTime.Now; | |
// Check if the cell contains a numeric value | |
if (a2.Type == CellValueType.IsNumeric) | |
{ | |
Console.WriteLine("A2 is Numeric Value: " + a2.IsNumericValue); | |
} | |
Style a2Style = a2.GetStyle(); | |
// Set the display format of numbers and dates. | |
a2Style.Number = 22; | |
a2.SetStyle(a2Style); | |
// Check if the cell contains a DateTime value | |
if (a2.Type == CellValueType.IsDateTime) | |
{ | |
Console.WriteLine("Cell A2 contains a DateTime value."); | |
// Get the DateTime value | |
DateTime dateTimeValue = a2.DateTimeValue; | |
// Now, you can use dateTimeValue as needed | |
Console.WriteLine("A2 DateTime Value: " + dateTimeValue); | |
// Output date formatted string | |
Console.WriteLine("A2 DateTime String Value: " + a2.StringValue); | |
} | |
else | |
{ | |
Console.WriteLine("Cell A2 does not contain a DateTime value."); | |
} |
Risultato in output:
A1 is Numeric Value: True
Cell A1 contains a DateTime value.
A1 DateTime Value: 11/23/2023 5:59:09 PM
A1 DateTime String Value: 11-23-23 17:59:09
A2 is Numeric Value: True
Cell A2 contains a DateTime value.
A2 DateTime Value: 11/23/2023 5:59:09 PM
A2 DateTime String Value: 11/23/2023 17:59