如何使用openxml 2.0创建日期单元格

时间:2012-06-28 17:07:51

标签: vb.net openxml

尝试使用openXML 2.0 lib创建日期单元格。在某些情况下会显示日期,但excel在打开文件时会出错。如果我删除它打开的日期单元格没有错误。谁知道什么是错的?

Protected Function CreateCell(columnIndex As Integer, rowIndex As Integer, value As DateTime) As Cell

    Dim cell As New Cell()
    cell.DataType = CellValues.Date
    Dim v As CellValue = New CellValue()
    v.Text = value.ToString()
    cell.CellValue = v
    Return cell

End Function

Protected Function CreateCell(columnIndex As Integer, rowIndex As Integer, value As Double) As Cell
    Dim cell As New Cell()

    cell.DataType = CellValues.Number
    'cell.CellReference = getColumnName(columnIndex) & rowIndex
    cell.CellValue = New CellValue()
    cell.CellValue.Text = value.ToString()
    Return cell
End Function

2 个答案:

答案 0 :(得分:2)

如果从this article下载代码并查看ExcelInterface.vb,您将看到一些基于(ECMA-376第1部分,18.8.30)检测日期隐式单元格格式的代码。您应该能够将其调整为设置它们。另请参阅将Excel日期和时间转换为.NET的例程,因为您希望将Excel(电子表格)日期值放入日期格式的单元格中。

答案 1 :(得分:0)

请注意,CellValues.Date枚举成员仅限 可在Microsoft Office 2010及更高版本中使用(请参阅MSDN库 了解更多信息)。

因此,如果您打开包含Microsoft Office 2007类型为CellValues.Date的单元格的Excel电子表格 你会收到一个错误。

此外,CellValues.Date类型的单元格的值必须为 ISO 8601格式(参见Office Open XML第1部分规范,第18.17.4.1节)。

摘要ISO 8601格式:

  • ISO 8601中的日期采用以下格式表示:YYYY-MM-DD
  • 时间按以下格式存储:hh:mm:ss
  • 日期和时间使用大写字母T分隔:YYYY-MM-DDThh:mm:ss

有关详细信息,请参阅以下文章(WIKIPEDIA) ISO 8601格式。

以下代码示例显示了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如何显示日期值。