Wie man Daten und Zeiten verwaltet
Wie man Daten und Zeiten in Excel speichert
Daten und Zeiten werden in Zellen als Zahlen gespeichert. Daher sind die Werte von Zellen, die Daten und Zeiten enthalten, vom numerischen Typ. Eine Zahl, die ein Datum und eine Uhrzeit angibt, besteht aus den Datum (Ganzzahlteil) und Uhrzeit (Bruchteilteil) Komponenten. Die Eigenschaft Cell.DoubleValue gibt diese Zahl zurück.
Wie man Daten und Zeiten in Aspose.Cells anzeigt
Um eine Zahl als Datum und Uhrzeit anzuzeigen, wenden Sie das erforderliche Datums- und Zeitformat auf eine Zelle über die Eigenschaften Style.number oder Style.Custom an. Die Eigenschaft CellValue.DateTimeValue gibt das DateTime-Objekt zurück, das das Datum und die Uhrzeit angibt, die durch die Zahl in einer Zelle dargestellt werden.
Wie man zwischen zwei Datumsystemen in Aspose.Cells wechselt
MS-Excel speichert Daten als Zahlen, die als Serienwerte bezeichnet werden. Ein Serienwert ist eine Ganzzahl, die die Anzahl der vergangenen Tage seit dem ersten Tag im Datensystem angibt. Excel unterstützt die folgenden Datensysteme für Serienwerte:
- Das 1900-Datensystem. Das erste Datum ist der 1. Januar 1900 und sein Serienwert ist 1. Das letzte Datum ist der 31. Dezember 9999 und sein Serienwert beträgt 2.958.465. Dieses Datensystem wird standardmäßig in der Arbeitsmappe verwendet.
- Das 1904-Datumsystem. Das erste Datum ist der 1. Januar 1904, und sein serieller Wert ist 0. Das letzte Datum ist der 31. Dezember 9999, und sein serieller Wert ist 2.957.003. Um dieses Datumsystem im Arbeitsbuch zu verwenden, setzen Sie die Eigenschaft Workbook.settings.date1904 auf wahr.
Dieses Beispiel zeigt, dass die in verschiedenen Datensystemen gespeicherten Serienwerte für dasselbe Datum unterschiedlich sind.
from aspose.cells import CellValueType, Workbook | |
from datetime import datetime | |
# Instantiating an Workbook object | |
workbook = Workbook() | |
workbook.settings.date1904 = False | |
# Obtaining the reference of the newly added worksheet | |
ws = workbook.worksheets[0] | |
cells = ws.cells | |
dateData = datetime(2023, 11, 23) | |
# Setting the DateTime value to the cells | |
a1 = cells.get("A1") | |
a1.put_value(dateData) | |
# Check if the cell contains a numeric value | |
if a1.type == CellValueType.IS_NUMERIC: | |
print("A1 is Numeric Value: " + str(a1.double_value)) | |
workbook.settings.date1904 = True | |
print("use The 1904 date system====================") | |
# Setting the DateTime value to the cells | |
a2 = cells.get("A2") | |
a2.value = dateData | |
# Check if the cell contains a numeric value | |
if a2.type == CellValueType.IS_NUMERIC: | |
print("A2 is Numeric Value: " + str(a2.double_value)) |
A1 is Numeric Value: 45253
use The 1904 date system====================
A2 is Numeric Value: 43791
So legen Sie den DateTime-Wert in Aspose.Cells fest
Dieses Beispiel legt einen DateTime-Wert in Zelle A1 und A2 fest, legt ein benutzerdefiniertes Format für A1 und ein Zahlenformat für A2 fest und gibt dann die Werttypen aus.
from aspose.cells import CellValueType, Workbook | |
from datetime import datetime | |
# Instantiating an Workbook object | |
workbook = Workbook() | |
# Obtaining the reference of the newly added worksheet | |
ws = workbook.worksheets[0] | |
cells = ws.cells | |
# Setting the DateTime value to the cells | |
a1 = cells.get("A1") | |
a1.put_value(datetime.now()) | |
# Check if the cell contains a numeric value | |
if a1.type == CellValueType.IS_NUMERIC: | |
print("A1 is Numeric Value: " + str(a1.is_numeric_value)) | |
a1Style = a1.get_style() | |
# Set custom Datetime style | |
a1Style.custom = "mm-dd-yy hh:mm:ss" | |
a1.set_style(a1Style) | |
# Check if the cell contains a DateTime value | |
if a1.type == CellValueType.IS_DATE_TIME: | |
print("Cell A1 contains a DateTime value.") | |
else: | |
print("Cell A1 does not contain a DateTime value.") | |
# Setting the DateTime value to the cells | |
a2 = cells.get("A2") | |
a2.value = datetime.now() | |
# Check if the cell contains a numeric value | |
if a2.type == CellValueType.IS_NUMERIC: | |
print("A2 is Numeric Value: " + str(a2.is_numeric_value)) | |
a2Style = a2.get_style() | |
# Set the display format of numbers and dates. | |
a2Style.number = 22 | |
a2.set_style(a2Style) | |
# Check if the cell contains a DateTime value | |
if a2.type == CellValueType.IS_DATE_TIME: | |
print("Cell A2 contains a DateTime value.") | |
else: | |
print("Cell A2 does not contain a DateTime value.") |
Ausgabenergebnis:
A1 is Numeric Value: True
Cell A1 contains a DateTime value.
A2 is Numeric Value: True
Cell A2 contains a DateTime value.
So erhalten Sie den DateTime-Wert in Aspose.Cells
Dieses Beispiel legt einen DateTime-Wert in Zelle A1 und A2 fest, legt ein benutzerdefiniertes Format für A1 und ein Zahlenformat für A2 fest, überprüft die Werttypen von zwei Zellen und gibt dann den DateTime-Wert und den formatierten String aus.
from aspose.cells import CellValueType, Workbook | |
from datetime import datetime | |
# Instantiating an Workbook object | |
workbook = Workbook() | |
# Obtaining the reference of the newly added worksheet | |
ws = workbook.worksheets[0] | |
cells = ws.cells | |
# Setting the DateTime value to the cells | |
a1 = cells.get("A1") | |
a1.put_value(datetime.now()) | |
# Check if the cell contains a numeric value | |
if a1.type == CellValueType.IS_NUMERIC: | |
print("A1 is Numeric Value: " + str(a1.is_numeric_value)) | |
a1Style = a1.get_style() | |
# Set custom Datetime style | |
a1Style.custom = "mm-dd-yy hh:mm:ss" | |
a1.set_style(a1Style) | |
# Check if the cell contains a DateTime value | |
if a1.type == CellValueType.IS_DATE_TIME: | |
print("Cell A1 contains a DateTime value.") | |
# Get the DateTime value | |
dateTimeValue = a1.date_time_value | |
# Now, you can use dateTimeValue as needed | |
print("A1 DateTime Value: " + str(dateTimeValue)) | |
# Output date formatted string | |
print("A1 DateTime String Value: " + a1.string_value) | |
else: | |
print("Cell A1 does not contain a DateTime value.") | |
# Setting the DateTime value to the cells | |
a2 = cells.get("A2") | |
a2.value = datetime.now() | |
# Check if the cell contains a numeric value | |
if a2.type == CellValueType.IS_NUMERIC: | |
print("A2 is Numeric Value: " + str(a2.is_numeric_value)) | |
a2Style = a2.get_style() | |
# Set the display format of numbers and dates. | |
a2Style.number = 22 | |
a2.set_style(a2Style) | |
# Check if the cell contains a DateTime value | |
if a2.type == CellValueType.IS_DATE_TIME: | |
print("Cell A2 contains a DateTime value.") | |
# Get the DateTime value | |
dateTimeValue = a2.date_time_value | |
# Now, you can use dateTimeValue as needed | |
print("A2 DateTime Value: " + str(dateTimeValue)) | |
# Output date formatted string | |
print("A2 DateTime String Value: " + a2.string_value) | |
else: | |
print("Cell A2 does not contain a DateTime value.") |
Ausgabenergebnis:
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