使用OpenXML在Excel单元格中添加date

这就是我所做的:

CellFormat cellFormat = new CellFormat() { NumberFormatId = (UInt32Value)14U, FontId = (UInt32Value)0U, FillId = (UInt32Value)0U, BorderId = (UInt32Value)0U, FormatId = (UInt32Value)0U, ApplyNumberFormat = true }; sd.WorkbookPart.WorkbookStylesPart.Stylesheet.CellFormats.AppendChild<CellFormat>(cellFormat); _dateStyleIndex = sd.WorkbookPart.WorkbookStylesPart.Stylesheet.CellFormats.Count() - 1; 

然后在我的代码稍后的某个地方

 else if (type == DataTypes.DateTime) { DateTime dateTime = DateTime.Parse(text); double oaValue = dateTime.ToOADate(); cell.CellValue = new CellValue(oaValue.ToString(CultureInfo.InvariantCulture)); cell.DataType = new EnumValue<CellValues>(CellValues.Date); cell.StyleIndex = Convert.ToUInt32(_dateStyleIndex); } 

但是,当我使用Open XML SDK Toolvalidation生成的excel文件时,出现以下validation错误: 属性“t”的值为无效值“d”。 枚举约束失败。

我在这里错过了什么? 提前谢谢你的帮助。

PS:添加,这是x:sheetData的样子。 它给我的validation错误:

 <x:sheetData xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main"> <x:row r="2"> <x:cr="B2" t="s"> <x:v>0</x:v> </x:c> <x:cr="C2" t="s"> <x:v>1</x:v> </x:c> <x:cr="D2" t="s"> <x:v>2</x:v> </x:c> </x:row> <x:row r="3"> <x:cr="B3" t="s"> <x:v>3</x:v> </x:c> <x:cr="C3" t="s"> <x:v>6</x:v> </x:c> <x:cr="D3" s="1" t="d"> <x:v>42634.906087963</x:v> </x:c> </x:row> <x:row r="4"> <x:cr="B4" t="s"> <x:v>4</x:v> </x:c> <x:cr="C4" t="s"> <x:v>7</x:v> </x:c> <x:cr="D4" s="1" t="d"> <x:v>42634.9062037037</x:v> </x:c> </x:row> <x:row r="5"> <x:cr="B5" t="s"> <x:v>5</x:v> </x:c> <x:cr="C5" t="s"> <x:v>8</x:v> </x:c> <x:cr="D5" s="1" t="d"> <x:v>42634.9062847222</x:v> </x:c> </x:row> </x:sheetData> 

为了最广泛的兼容性,使用CellValues.Number作为单元格数据types。

根据文档 ,CellValues.Date适用于Excel 2010,因此您可能希望避免与Excel 2007(以及其他应用程序)完全向后兼容。

 //broadly supported - earliest Excel numeric date 01/01/1900 DateTime dateTime = DateTime.Parse(text); double oaValue = dateTime.ToOADate(); cell.CellValue = new CellValue(oaValue.ToString(CultureInfo.InvariantCulture)); cell.DataType = new EnumValue<CellValues>(CellValues.Number); cell.StyleIndex = Convert.ToUInt32(_numericDateCellFormatIndex); //supported in excel 2010 - not XLSX Transitional compliant DateTime dateTime = DateTime.Parse(text); cell.CellValue = new CellValue(dateTime.ToString("s")); cell.DataType = new EnumValue<CellValues>(CellValues.Date); cell.StyleIndex = Convert.ToUInt32(_sortableDateCellFormatIndex); 

这个更早的更完整的答案表明,Excel 2010默认情况下不会使用“可sorting的” CellValues.Date数据types。

据推测, CellValues.Datetypes的原因是为了克服数字date的限制,例如最早的Excel数字date是01/01/1900。

digitalpreservation.gov 解释了date单元格types背后的一些历史意图 ,本页解释了XLSX Transitional是主stream真实世界应用程序 (2014年testing) 使用的版本 。

对于date单元,XLSX Strict使用ISO 8601中的完整,扩展格式日历表示。为了向后兼容,在XLSX过渡中不允许使用ISO 8601date。

在OOXML的ISO标准化stream程的后期,提出了一个在电子表格中采用ISO 8601格式的date和时间的scheme。

出席ISO 29500投票决议会议的专家们就OOXML格式的优秀提案进行了投票,他们主要是XML和文本文档的专家,而不是电子表格

由于ISO 29500过渡变体的意图是与现有的.xlsx文档语料库和处理这些文档的应用程序兼容,所以引入了对第4部分禁止在过渡变体中使用ISO 8601date的修改。 其次,ISO 8601是一种非常灵活的格式,任何针对互操作性的上下文的使用都需要具体说明哪些特定的文本string模式在date和时间上有所期望。

… 2014年11月的testing表明,Google表格和自由办公室都在Transitional变体中创build了新文档