2016-09-21 41 views
1

を使用してExcelのセルに日付を追加:これは私がやっているものです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);    
} 

をしかし、私は、生成されたExcelが付いたファイルを検証する場合XML SDKツールを開くと、次の検証エラーが発生します。属性 't'に無効な値 'd'があります。 Enumeration制約が失敗しました。

私はここで何が欠けていますか?事前にあなたの助けをありがとう。

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> 
+0

Cell.DataTypeをCellValues.Dateに設定していませんか?私はそれについてEnumValueをインスタンス化するとは思わない。 DataTypeにはSchemaAttrAttributeという属性があります。私はそれが空であると仮定しているため、エラーを引き起こしています。 – Dispersia

+0

そして、あなたはそれがどんな行を投げているのかは言わなかったが、CellValue =であれば、その前にあなたのDataTypeセットを移動しようとする。おそらく、コンテンツを設定する前にそのタイプを知る必要があるかもしれません。 – Dispersia

+0

はい、それは愚かなEnumValueのインスタンス化でした。 DataTypeのすべてのプロパティが正しく設定されていますが、このエラーが発生します。 –

答えて

6

CellValues.Numberをセルのデータタイプとして使用します。

docsによると、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); 

このearlier more complete answerは、Excel 2010がデフォルトで「並べ替え可能な」CellValues.Dateデータ型自体を使用していないことを示唆しています。

おそらくCellValues.Dateタイプの理由は、最も早いExcel数字の日付が01/01/1900であるなどの数値データの制限を克服することです。

digitalpreservation.gov explains some of the historical intention behind the date cell typeこのページでは、XLSX Transitional is the version used by mainstream real world applications(2014年にテスト済み)について説明しています。

XLSX Strict has a value type for cells of date, using the Complete, Extended Format Calendar representations in ISO 8601. For reasons of backwards compatibility, this typed use of ISO 8601 dates is not permitted in XLSX Transitional.

Late in the ISO standardization process for OOXML, a proposal was made to adopt the ISO 8601 format for dates and times in spreadsheets.

The experts present at the ISO 29500 Ballot Resolution Meeting where votes were held on the outstanding proposals for the OOXML format were primarily experts in XML and in textual documents rather than with spreadsheets

Since the intent of the Transitional variant of ISO 29500 was to be compatible with the existing corpus of .xlsx documents and the applications designed to handle them, an amendment to Part 4 to disallow ISO 8601 dates in the Transitional variant was introduced. Secondly, ISO 8601 is a very flexible format, and any use in a context that aims at interoperability needs to be specific about which particular textual string patterns are expected for dates and times.

... Tests in November 2014 indicated that Google Sheets and Libre Office both created new documents in the Transitional variant

+0

ありがとうございます。しかし、CellValuesがNumberでなくDateであるべき理由を理解できません。その後、Dateの使用は何ですか? –

+1

'CellValues.Date'はあまり広くサポートされていません(回答が更新されました)。 –

+2

私はISO8601の日付サポートを共同執筆し、議会図書館の論文に取り組んでいたので、この質問には面白いです。 d属性は、日付がスプレッドシートに「1900年1月1日以降の日数」として保存されていて、仕様を変更して変更することを希望していなかったという、標準化プロセスに関わっている複数の人を和らげるために追加されました。いいアイデアかもしれませんが、それはLotus 1-2-3以降のようです。 Chrisが言うように、CellValuesを使用し、1900年1月1日以降の日数として日付を格納する必要があります。 –

関連する問題