簡體   English   中英

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

[英]Adding a date in an Excel cell using OpenXML

這就是我在做的事情:

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 Tool驗證生成的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>

為了獲得最廣泛的兼容性,請使用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中不使用“排序” CellValues.Date默認數據類型本身。

推測CellValues.Date類型的原因是克服數字日期的限制,例如最早的Excel數字日期是01/01/1900。

digitalpreservation.gov 解釋了日期單元格類型背后的一些歷史意圖 ,本頁面解釋了XLSX Transitional是主流現實世界應用程序使用的版本 (2014年測試)。

XLSX Strict具有日期單元格的值類型,使用ISO 8601中的完整,擴展格式日歷表示。出於向后兼容性的原因,XLSX Transitional中不允許使用ISO 8601日期類型。

在OOXML的ISO標准化過程的后期,提議采用ISO 8601格式的電子表格中的日期和時間。

出席ISO 29500投票決議會議的專家對OOXML格式的未完成提案進行了投票,主要是XML和文本文檔方面的專家,而不是電子表格專家

由於ISO 29500的Transitional變體的目的是與.xlsx文檔的現有語料庫和旨在處理它們的應用程序兼容,因此引入了對過渡變體中禁止ISO 8601日期的第4部分的修訂。 其次,ISO 8601是一種非常靈活的格式,在針對互操作性的上下文中的任何使用都需要具體說明日期和時間預期的特定文本字符串模式。

... 2014年11月的測試表明,Google表格和自由辦公室都在Transitional變體中創建了新文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM