简体   繁体   中英

Office Open XML Date not working

I'm currently developing an component for our application that allows us to generate an Excel sheet without the clients having to own Excel. So Open XML came to mind.

What is not working at the moment is parsing of dates.

This is my worksheet:

<?xml version="1.0" encoding="UTF-8"?>
 <x:worksheet xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
 <x:cols>
  <x:col min="1" max="1" width="42.42" customWidth="1" />
  <x:col min="2" max="2" width="42.56" customWidth="1" />
  <x:col min="3" max="3" width="16.27" customWidth="1" />
</x:cols>
<x:sheetData>
  <x:row r="1">
     <x:c r="A1">
        <x:v>FF kijken hoe dit werkt snap er geen fu** van</x:v>
     </x:c>
     <x:c r="B1">
        <x:v>This is some really, really long text to display.</x:v>
     </x:c>
     <x:c r="C1" s="0">
        <x:v>40651.6777777778</x:v>
     </x:c>
  </x:row>
</x:sheetData>
</x:worksheet>

This is my stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<x:styleSheet xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
   <x:numFmts count="1">
  <x:numFmt numFmtId="164" formatCode="dd-mm-yy hh:mm" />
   </x:numFmts>
   <x:fonts count="1">
  <x:font>
 <x:sz val="11" />
 <x:name val="Arial" />
      </x:font>
   </x:fonts>

<x:cellXfs count="1">
<x:xf numFmtId="164" fontId="0" fillId="0" borderId="0" xfId="0"` applyNumberFormat="1" />   
</x:cellXfs>
</x:styleSheet>

This is the code I use to get my dates.

cell.StyleIndex = 0;  .
string columnValue = date.ToOADate().ToString().Replace(",", ".");
//string columnValue =  date.ToOADate().ToString().
Replace(CultureInfo.CurrentUICulture.NumberFormat.NumberDecimalSeparator, ".");
cell.CellValue = new CellValue(columnValue);

The result is not what I want I'll get the double value from the date not the date in the stylesheet I created.

I'll get this 40651,67778 instead of 18-4-2011 16:16

It looks like your styles.xml stylesheet is missing the cellStyles element. I wrote some code to create a file similar to yours and without that element the style was not applied. The whole element on mine looks like this:

<x:cellStyles count="1"><x:cellStyle xfId="0" builtinId="0" /></x:cellStyles>

The code I used to generate the file is below. Note I only output a single date cell but adding the other cells is trivial.

string fileName = @"D:\Test\formatting.xlsx";

SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(fileName, SpreadsheetDocumentType.Workbook);

// Add a WorkbookPart to the document.
WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
Stylesheet styleSheet = new Stylesheet();

uint iExcelIndex = 164;
CellFormats cfs = new CellFormats();
NumberingFormats nfs = new NumberingFormats();

NumberingFormat nf;
nf = new NumberingFormat();
nf.NumberFormatId = iExcelIndex++;
nf.FormatCode = "dd-mm-yyyy hh:mm:ss";
nfs.Append(nf);

CellFormat cf = new CellFormat();
cf.NumberFormatId = nf.NumberFormatId;
cf.FontId = 0;
cf.FillId = 0;
cf.BorderId = 0;
cf.FormatId = 0;
cf.ApplyNumberFormat = true;
cfs.Append(cf);

styleSheet.CellFormats = cfs;
styleSheet.NumberingFormats = nfs;
styleSheet.Borders = new Borders();
Border border = new Border();
styleSheet.Borders.Append(border);
styleSheet.Fills = new Fills();
Fill fill = new Fill();
styleSheet.Fills.Append(fill);

styleSheet.Fonts = new Fonts();
Font font = new Font();
styleSheet.Fonts.Append(font);

// **** This code is the code I think you are missing: ****
CellStyles css = new CellStyles();
CellStyle cs = new CellStyle();
cs.FormatId = 0;
cs.BuiltinId = 0;
css.Append(cs);
css.Count = UInt32Value.FromUInt32((uint)css.ChildElements.Count);
styleSheet.Append(css);
//**** end of your suspected missing code ****

workbookpart.Workbook = new Workbook();
workbookpart.AddNewPart<WorkbookStylesPart>();

// Add a WorksheetPart to the WorkbookPart.
WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = new Worksheet(new SheetData());

// Add Sheets to the Workbook.
Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());

// Append a new worksheet and associate it with the workbook.
Sheet sheet = new Sheet()
{
    Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart),
    SheetId = 1,
    Name = "mySheet"
};
sheets.Append(sheet);
Worksheet worksheet = new Worksheet();
SheetData sheetData = new SheetData();

Row row = new Row();

Cell cell = new Cell();

cell.StyleIndex = 0;
cell.DataType = CellValues.Date;
string columnValue = DateTime.FromOADate(40651.67778).ToOADate().ToString().Replace(",", ".");
cell.CellValue = new CellValue(columnValue);

row.Append(cell);
sheetData.Append(row);
worksheet.Append(sheetData);
worksheetPart.Worksheet = worksheet;
workbookpart.WorkbookStylesPart.Stylesheet = styleSheet;

// Close the document.
spreadsheetDocument.Close();

The XML generated from this for the worksheet was:

<?xml version="1.0" encoding="utf-8"?>
<x:worksheet xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
    <x:sheetData>
        <x:row>
            <x:c s="0" t="d">
                <x:v>40651.67778</x:v>
            </x:c>
        </x:row>
    </x:sheetData>
</x:worksheet>

And the XML generated from this for the stylesheet was:

<?xml version="1.0" encoding="utf-8"?>
<x:styleSheet xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
    <x:numFmts>
        <x:numFmt numFmtId="164" formatCode="dd-mm-yyyy hh:mm:ss" />
    </x:numFmts>
    <x:fonts>
        <x:font />
    </x:fonts>
    <x:fills>
        <x:fill />
    </x:fills>
    <x:borders>
        <x:border />
    </x:borders>
    <x:cellXfs>
        <x:xf numFmtId="164" fontId="0" fillId="0" borderId="0" xfId="0" applyNumberFormat="1" />
    </x:cellXfs>
    <x:cellStyles count="1">
        <x:cellStyle xfId="0" builtinId="0" />
    </x:cellStyles>
</x:styleSheet>

I output t="d" on my cell which denotes that the cell is a date but I don't think this is required.

A great resource for stylesheets (among other OpenXML elements) can be found at http://polymathprogrammer.com/2009/11/09/how-to-create-stylesheet-in-excel-open-xml/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM