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'。枚舉約束失敗。
我在這裏錯過了什麼?提前謝謝你的幫助。
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>
難道你不只是將cell.DataType設置爲CellValues.Date?我不相信你爲此實例化一個EnumValue。 DataType有一個屬性SchemaAttrAttribute,我假設它是空的是什麼導致錯誤。 – Dispersia
而你沒有說它投擲的是什麼線,但是如果它在CellValue =上,試着將你的DataType設置在它之前。在設置其內容之前,可能需要知道它的類型。 – Dispersia
是的,這是愚蠢的實例化一個EnumValue。 DataType的所有屬性都設置正確,但仍然出現此錯誤。 –