Vb.net 如何使用openxml 2.0创建日期单元格
正在尝试使用OpenXML2.0库创建日期单元格。在某些情况下,会显示日期,但excel在打开文件时会显示错误。如果我删除日期单元格,它将打开,没有错误。有人知道怎么了吗Vb.net 如何使用openxml 2.0创建日期单元格,vb.net,openxml,Vb.net,Openxml,正在尝试使用OpenXML2.0库创建日期单元格。在某些情况下,会显示日期,但excel在打开文件时会显示错误。如果我删除日期单元格,它将打开,没有错误。有人知道怎么了吗 Protected Function CreateCell(columnIndex As Integer, rowIndex As Integer, value As DateTime) As Cell Dim cell As New Cell() cell.DataType = CellValues.Dat
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
请注意,
CellValues.Date
枚举成员仅为
在Microsoft Office 2010及更高版本中提供(请参见库
更多信息)
因此,如果使用Microsoft Office 2007打开Excel电子表格,其中包含类型为CellValues.Date
的单元格
你会得到一个错误
此外,类型为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
分隔
有关更多信息,请参阅以下文章()
在ISO8601格式上
下面的代码示例显示创建类型为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如何显示日期值。如果您从ExcelInterface.vb下载代码并查看,您将看到一些基于(ECMA-376第1部分,18.8.30)检测日期隐式单元格格式的代码。您也应该能够调整此设置来设置它们。另请参阅将Excel日期和时间转换为.NET和从.NET转换的例程,因为您需要将Excel(电子表格)日期值放入日期格式的单元格中。单元格引用的全部内容是什么?我没有为其他单元格设置它,它工作得很好…@eschneider:cell引用描述了列索引和行索引(单元格的位置)。您还在问题中描述了设置单元格引用的方法(getColumnName)。如果真的有必要?我不知道。在Microsoft提供的示例中,也设置了单元格引用。我认为当您将单元格添加到集合的列/行中时,它会为您设置单元格引用。@eschneider:是的,您是对的。这是没有必要的。我已经更新了我的答案。下载了你的代码;查看了ConvertDateTimeToExcelDate(),但仍然不确定缺少什么