excel OpenXml中单元格的日期格式

时间:2014-05-08 11:38:58

标签: c# excel openxml openxml-sdk

我通过OpenXML SDK 2.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文件时,我会看到一个数字。这是好的,我的意思是,但现在我需要设置一个单元格格式...我有一个方法:

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);

此时,当我打开生成的Excel时,收到此消息:“Excel在'test.xlsx'中找到了不可读的内容。你想恢复这个工作簿的内容吗?如果您信任此工作簿的来源,请单击“是”。“

修复我的Excel报告后,“/ xl / styles.xml”文件中出现错误。

有谁知道在创建单元格格式时我犯了什么错误? 非常感谢

0 个答案:

没有答案