日付と時間の管理方法
Excelでの日付と時間の格納方法
日付と時間はセルに数値として格納されます。したがって、日付と時間を含むセルの値は数値型です。日付と時間を指定する数値は、日付(整数部)と時間(小数部)のコンポーネントで構成されます。Cell.DoubleValueプロパティは、この数値を返します。
Aspose.Cellsでの日付と時間の表示方法
数値を日付と時間として表示するために、セルにStyle.NumberプロパティまたはStyle.Customプロパティを使用して必要な日付と時間形式を適用します。CellValue.DateTimeValueプロパティは、セルに含まれる数値で表される日付と時間を示すDateTimeオブジェクトを返します。
Aspose.Cellsでの2つの日付システムの切り替え方法
MS-Excelは、日付をシリアル値と呼ばれる数値として格納します。シリアル値は、日付システムの最初の日から経過した日数を示す整数です。Excelは、シリアル値のために以下の日付システムをサポートしています。
- 1900年の日付システム。最初の日付は1900年1月1日で、そのシリアル値は1です。最後の日付は9999年12月31日で、そのシリアル値は2,958,465です。この日付システムはワークブックのデフォルトで使用されます。
- 1904年の日付システム。最初の日付は1904年1月1日で、そのシリアル値は0です。最後の日付は9999年12月31日で、そのシリアル値は2,957,003です。ワークブックでこの日付システムを使用するには、Workbook.Settings.Date1904プロパティをtrueに設定します。
この例は、異なる日付システムで同じ日付に格納されたシリアル値が異なることを示しています。
//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
Aspose.Cellsでの日時値の設定方法
この例では、セルA1とA2に日時値を設定し、A1のカスタム形式とA2の数値形式を設定し、値のタイプを出力します。
//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."); | |
} |
出力結果:
A1 is Numeric Value: True
Cell A1 contains a DateTime value.
A2 is Numeric Value: True
Cell A2 contains a DateTime value.
Aspose.Cellsでの日時値の取得方法
この例では、セルA1とA2に日時値を設定し、A1のカスタム形式とA2の数値形式を設定し、2つのセルの値のタイプをチェックし、日時値とフォーマットされた文字列を出力します。
//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."); | |
} |
出力結果:
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