使用OpenXML在Excel单元格中添加日期

本文关键字:添加 日期 单元格 Excel OpenXML 使用 | 更新日期: 2023-09-27 18:08:06

我就是这样做的:

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工具验证生成的excel文件时,我得到以下验证错误:属性't'具有无效值'd'。

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

PS:添加,这是如何x:sheetData看起来像。它给了我验证错误:

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

使用OpenXML在Excel单元格中添加日期

为了获得最大的兼容性,请使用CellValues.Number作为单元格数据类型。

根据文档,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默认不使用'sortable' CellValues.Date数据类型本身。

可能CellValues.Date类型的原因是为了克服数字日期的限制,例如最早的Excel数字日期是01/01/1900。

digitalpreservation.gov解释了日期单元格类型背后的一些历史意图,本页解释了XLSX Transitional是主流实际应用程序使用的版本(2014年测试)。

XLSX Strict有一个日期单元格的值类型,使用Complete,ISO 8601中的扩展格式日历表示。出于…的原因向后兼容,这种类型的使用ISO 8601日期是不行的

ISO标准化后期在处理OOXML的过程中,提出了采用ISO 8601格式的建议用于电子表格中的日期和时间。

出席会议的专家ISO 29500投票决议会议,在此举行投票杰出的OOXML格式提案主要是专家XML和文本文档,而不是电子表格

自ISO 29500的过渡版本的意图是兼容现有的.xlsx文档语料库和设计用于处理它们的应用程序,对第4部分进行了修改在过渡版本中禁用ISO 8601日期。其次,ISO 8601是一种非常灵活的格式,可以在任何上下文中使用以互操作性为目标的需要是具体的日期和时间需要特定的文本字符串模式。

…2014年11月的测试表明,谷歌表格和Libre Office都可以都在Transitional变体

中创建了新文档