【问题标题】:how to create a date cell using openxml 2.0如何使用 openxml 2.0 创建日期单元格
【发布时间】:2013-09-07 13:51:36
【问题描述】:

尝试使用 openXML 2.0 库创建日期单元格。在某些情况下会显示日期,但 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

【问题讨论】:

    标签: vb.net openxml


    【解决方案1】:

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

    【讨论】:

    • 下载了你的代码;看了 ConvertDateTimeToExcelDate() 但仍然不确定缺少什么
    【解决方案2】:

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

    因此,如果您使用 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如何显示日期值。

    【讨论】:

    • CellReference 到底是什么?我没有为其他单元格设置它,它工作正常......
    • @eschneider:单元格引用描述了列索引和行索引(单元格的位置)。您还在问题中描述了一种方法(getColumnName)来设置单元格引用。如果真的有必要?我不知道。在 Microsoft 提供的示例中,还设置了单元格引用。
    • 我认为当您将单元格添加到集合的列/行时,它会为您设置。
    • @eschneider:是的,你是对的。没有必要。我已经更新了我的答案。
    猜你喜欢
    • 1970-01-01
    • 2016-03-08
    • 1970-01-01
    • 1970-01-01
    • 2010-11-03
    • 1970-01-01
    • 1970-01-01
    • 2010-10-10
    • 1970-01-01
    相关资源
    最近更新 更多