使用OpenXMLWriter时Excel中的DateFormat问题

时间:2017-05-18 10:27:31

标签: c# openxml

我正在尝试使用数据格式编写excelsheet。我使用了这里提到的逻辑https://www.codeproject.com/Articles/877791/How-to-Create-Large-Excel-File-using-Openxml

我面临的问题是我需要的日期是英国格式DD-MM-YYYY。所以我改变了

NumberingFormat nf;
nf = new NumberingFormat();
nf.NumberFormatId = iExcelIndex++;
// nf.FormatCode = @"[$-409]m/d/yy\ h:mm\ AM/PM;@";Changed this to below
nf.FormatCode = @"[$-409]dd/mm/yyyy;@";

但是现在日期是XX-01-1900,月份和年份默认为01和1900。

如果有人能指出我正确的方向,会很棒。

1 个答案:

答案 0 :(得分:1)

以下适用于我: 我在cellRef A1上添加了一个日期

static void Main(string[] args)
{
    string excelFilePath = "Test1.xlsx";
    string text = "02-25-1999";
    string sheetName = "Sheet1";

    using (SpreadsheetDocument spreadsheetDoc = SpreadsheetDocument.Open(excelFilePath, true))
    {              
        var stylesheet = spreadsheetDoc.WorkbookPart.WorkbookStylesPart.Stylesheet;
        var numberingFormats = stylesheet.NumberingFormats;

        const string dateFormatCode = "dd/mm/yyyy";

        var dateFormat =
            numberingFormats.OfType<NumberingFormat>()
                .FirstOrDefault(format => format.FormatCode == dateFormatCode);

        if (dateFormat == null)
        {
            dateFormat = new NumberingFormat
            {
                NumberFormatId = UInt32Value.FromUInt32(164),
                // Built-in number formats are numbered 0 - 163. Custom formats must start at 164.
                FormatCode = StringValue.FromString(dateFormatCode)
            };
            numberingFormats.AppendChild(dateFormat);
            numberingFormats.Count = Convert.ToUInt32(numberingFormats.Count());
            stylesheet.Save();
        }
        // get the (1-based) index 
        var dateStyleIndex = numberingFormats.ToList().IndexOf(dateFormat) + 1;
        var worksheetPart = GetWorksheetPartByName(spreadsheetDoc, "Sheet1");

        Row row1 = worksheetPart.Worksheet.GetFirstChild<SheetData>().Elements<Row>().FirstOrDefault();
        Cell cell = row1.Elements<Cell>().FirstOrDefault();

        DateTime dateTime = DateTime.Parse(text);
        double oaValue = dateTime.ToOADate();
        cell.CellValue = new CellValue(oaValue.ToString(CultureInfo.InvariantCulture));

        cell.StyleIndex = Convert.ToUInt32(dateStyleIndex);

        worksheetPart.Worksheet.Save();
        spreadsheetDoc.WorkbookPart.WorkbookStylesPart.Stylesheet.Save();
    }
    Console.ReadKey();
}

GetWorksheetPartByName的位置:

private static WorksheetPart GetWorksheetPartByName(SpreadsheetDocument document, string sheetName)
{
    IEnumerable<Sheet> sheets =
        document.WorkbookPart.Workbook.GetFirstChild<Sheets>().Elements<Sheet>().Where(s => s.Name == sheetName);

    if (!sheets.Any())
    {
        // The specified worksheet does not exist.
        return null;
    }

    string relationshipId = sheets.First().Id.Value;
    WorksheetPart worksheetPart = (WorksheetPart)document.WorkbookPart.GetPartById(relationshipId);
    return worksheetPart;
}

excel中的日期只是默认日期的天数。计算要插入的数字,然后在单元格上应用所需的样式。