CellValues.Date
列挙メンバーは、Microsoft Office 2010 以降でのみ使用できることに注意してください(詳細については、 MSDNライブラリを参照してください)。
CellValues.Date
そのため、 Microsoft Office 2007でタイプのセルを含む Excel スプレッドシートを開くと、エラーが発生します。
さらに、型のセルの値はCellValues.Date
ISO 8601 形式である必要があります (Office Open XML Part 1 仕様のセクション 18.17.4.1 を参照)。
要約 ISO 8601 形式:
- ISO 8601 の日付は、次の形式で表されます
YYYY-MM-DD
。
- 時刻は次の形式で格納されます
hh:mm:ss
。
- 日付と時刻は、大文字の T: を使用して区切ります
YYYY-MM-DDThh:mm:ss
。
ISO 8601 形式の詳細については、次の記事 ( WIKIPEDIA )を参照してください。
次のコード サンプルは、タイプ のセルの作成を示していますCellValues.Date
。
Protected Shared Function CreateCell(columnIndex As Integer, rowIndex As Integer, value As DateTime, styleIndex As Integer) As Cell
Dim cell As Cell = New Cell()
cell.DataType = CellValues.Date
Dim v As CellValue = New CellValue()
v.Text = value.ToString("yyyy-MM-ddThh:mm:ss") ' Use ISO 8601 format for date value
cell.CellReference = "" ' Set cell reference here! E.g. A1
cell.CellValue = v
cell.StyleIndex = styleIndex
Return cell
End Function
Protected Function CreateNumberingFormatForDateCells() As NumberingFormat
Dim numberingFormat As NumberingFormat = New NumberingFormat()
numberingFormat.NumberFormatId = CType(165U, UInt32Value)
numberingFormat.FormatCode = "dd-mm-yyyy"
return numberingFormat
End Function
Protected Function CreateCellFormatForDateCells() As CellFormat
Dim cellFormat As CellFormat = New CellFormat()
cellFormat.NumberFormatId = CType(165U, UInt32Value)
cellFormat.ApplyNumberFormat = true
return cellFormat
End Function
Sub Main()
' ... Code to get/create your workbook
Dim workbookPart As WorkbookPart ...
' Add number format and cell style for date cells.
Dim nf As NumberingFormats = New NumberingFormats()
workbookPart.WorkbookStylesPart.Stylesheet.NumberingFormats = nf
workbookPart.WorkbookStylesPart.Stylesheet.NumberingFormats.Append(CreateNumberingFormatForDateCells());
workbookPart.WorkbookStylesPart.Stylesheet.CellFormats.Append(CreateCellFormatForDateCells());
' Call CreateCell() to create date cells
Dim c As Cell = CreateCell(0,0,DateTime.Now, workbookPart.WorkbookStylesPart.Stylesheet.CellFormats.Count())
' Code to append cell to spreadsheet
End Sub
CellValues.Date
セル タイプを に設定することで、日付値が ISO 8601 形式で保存されることのみが指定されることに注意してください。日付の表示セル スタイルを指定する必要があります。上記のサンプルでは、セル スタイルを作成 (およびセル スタイル インデックスを設定) して、日付値の表示方法を Excel に指示しています。