Das ist, was ich tue:ein Datum in einer Excel-Zelle Hinzufügen mit 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;
und dann irgendwo später in meinem Code
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);
}
Allerdings, wenn ich die erzeugte Excel-Validierung-Datei mit Open XML SDK Tool, bekomme ich den folgenden Validierungsfehler: Das Attribut 't' hat ungültigen Wert 'd'. Die Enumeration-Einschränkung ist fehlgeschlagen.
Was fehlt mir hier? Danke im Voraus für Ihre Hilfe.
PS: Hinzufügen, so sieht das x: sheetData aus. Es gibt mir die Validierungsfehler:
<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>
Setzen Sie nicht einfach cell.DataType auf CellValues.Date? Ich glaube nicht, dass Sie dafür einen EnumValue instanziieren. DataType hat ein Attribut von SchemaAttrAttribute, in dem ich annahm, dass es leer ist, was den Fehler verursacht. – Dispersia
Und Sie haben nicht gesagt, welche Zeile es anwarf, aber wenn CellValue = ist, versuchen Sie, das DataType-Set davor zu verschieben. Vielleicht muss es seinen Typ kennen, bevor er seinen Inhalt einstellt. – Dispersia
Ja, es war dumm, einen EnumValue zu instantiieren. Alle Eigenschaften von DataType sind korrekt eingestellt, trotzdem erhalte ich diesen Fehler. –