Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# excel OpenXml中单元格的日期格式_C#_Excel_Openxml_Openxml Sdk - Fatal编程技术网

C# excel OpenXml中单元格的日期格式

C# excel OpenXml中单元格的日期格式,c#,excel,openxml,openxml-sdk,C#,Excel,Openxml,Openxml Sdk,我通过OpenXMLSDK2.5创建excel文件。我有一个IDIsposable类,它包含一个变量 private SpreadsheetDocument ProcessDocument { get; set; } 我可以插入具有日期值的单元格: var cell = GetCell(sheet, columnName, rowIndex); //method return a cell cell.DataType = new EnumValue<CellValues>(Cel

我通过OpenXMLSDK2.5创建excel文件。我有一个IDIsposable类,它包含一个变量

private SpreadsheetDocument ProcessDocument { get; set; }
我可以插入具有日期值的单元格:

var cell = GetCell(sheet, columnName, rowIndex); //method return a cell

cell.DataType = new EnumValue<CellValues>(CellValues.Number);
cell.CellValue = new CellValue(dateResult.ToOADate().ToString(CultureInfo.InvariantCulture));
cell.StyleIndex = 0;
此时,当我打开生成的excel时,我收到以下消息:“excel在“test.xlsx”中发现不可读的内容。是否恢复此工作簿的内容?如果您信任此工作簿的源,请单击“是”

修复我的excel后,在文件“/xl/styles.xml”中报告一个错误

有人知道我在创建单元格格式时哪里出错了吗?
非常感谢

不熟悉OpenXML,但是单元格上是否没有
.NumberFormat
属性?例如,
cell1..NumberFormat=“m/d/yyyy”
?不,此选项不存在。但我在这里找到了一个解决办法:答案如下:
public virtual UInt32 CreateCellFormat(String format)
{
    NumberingFormat nf = new NumberingFormat();
    nf.FormatCode = format;

    if (this.WorkbookStylesPart.Stylesheet.NumberingFormats == null)
    {
        this.WorkbookStylesPart.Stylesheet.NumberingFormats = new NumberingFormats();
        this.WorkbookStylesPart.Stylesheet.NumberingFormats.AppendChild(new NumberingFormats(new NumberingFormat() { NumberFormatId = 0 }));
        this.WorkbookStylesPart.Stylesheet.NumberingFormats.Count = 1;
    }

    var numberFormatId = this.WorkbookStylesPart.Stylesheet.NumberingFormats.Elements<NumberingFormat>().Where(a => a.NumberFormatId.HasValue).Select(a => a.NumberFormatId).Max();
    nf.NumberFormatId =  numberFormatId == null || numberFormatId < 199 ? 200 : numberFormatId + 1;

    this.WorkbookStylesPart.Stylesheet.NumberingFormats.InsertAt(nf, (int)this.WorkbookStylesPart.Stylesheet.NumberingFormats.Count.Value);
    this.WorkbookStylesPart.Stylesheet.NumberingFormats.Count++;

    return nf.NumberFormatId;
}
public virtual void SetCellStyle(Sheet sheet, UInt32 rowIndex, String columnName, UInt32? fontIndex, UInt32? fillIndex, UInt32? formatIndex)
{
    var cell1 = this.GetCell(sheet, columnName, rowIndex);
    cell1.StyleIndex = this.CreateCellFormat(sheet, fontIndex, fillIndex, formatIndex);
}

internal virtual UInt32 CreateCellFormat(Sheet sheet, UInt32? fontIndex, UInt32? fillIndex, UInt32? formatIndex)
{
    CellFormat cellFormat = new CellFormat();
    WorksheetPart workSheetPart = this.GetWorkSheetPart(sheet.Name);

    if (fontIndex.HasValue)
    {
        cellFormat.FontId = fontIndex;
        cellFormat.ApplyFont = true;
    }

    if (fillIndex.HasValue)
    {
        cellFormat.FillId = fillIndex;
        cellFormat.ApplyFill = true;
    }

    if (formatIndex.HasValue)
    {
        cellFormat.NumberFormatId = formatIndex;

        cellFormat.ApplyNumberFormat = BooleanValue.FromBoolean(true);
    }

    if (this.ProcessDocument.WorkbookPart.WorkbookStylesPart.Stylesheet.CellFormats == null)
    {
        this.ProcessDocument.WorkbookPart.WorkbookStylesPart.Stylesheet.CellFormats = new CellFormats();
        this.ProcessDocument.WorkbookPart.WorkbookStylesPart.Stylesheet.CellFormats.Append(new CellFormat());
        this.ProcessDocument.WorkbookPart.WorkbookStylesPart.Stylesheet.CellFormats.Count = 1;
    }

    this.ProcessDocument.WorkbookPart.WorkbookStylesPart.Stylesheet.CellFormats.InsertAt(cellFormat, 
        (int)this.ProcessDocument.WorkbookPart.WorkbookStylesPart.Stylesheet.CellFormats.Count.Value);

    UInt32 result = this.ProcessDocument.WorkbookPart.WorkbookStylesPart.Stylesheet.CellFormats.Count;
    this.ProcessDocument.WorkbookPart.WorkbookStylesPart.Stylesheet.CellFormats.Count++;

    return result;

}
var formatIndex = excel.CreateCellFormat("dd.mm.yyyy");
excel.AddCellValue(workSheet, row, 6, DateTime.Now);
excel.SetCellStyle(workSheet, row, 6, null, null, formatIndex);