簡體   English   中英

Office Open XML日期不起作用

[英]Office Open XML Date not working

我目前正在為我們的應用程序開發一個組件,該組件使我們能夠生成Excel工作表,而客戶不必擁有Excel。 因此想到了Open XML。

目前不起作用的是解析日期。

這是我的工作表:

<?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>

這是我的樣式表:

<?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>

這是我用來獲取日期的代碼。

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

結果不是我想要的,我將從日期而不是我創建的樣式表中的日期獲取double值。

我將獲得此40651,67778而不是18-4-2011 16:16

您的styles.xml樣式表似乎缺少cellStyles元素。 我寫了一些代碼來創建一個與您的文件相似的文件,沒有該元素,樣式就沒有應用。 我的整個元素看起來像這樣:

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

我用來生成文件的代碼如下。 注意我只輸出一個日期單元格,但添加其他單元格是微不足道的。

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

由此為工作表生成的XML為:

<?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>

從中為樣式表生成的XML為:

<?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>

我在單元格上輸出t="d" ,這表示該單元格是日期,但我認為這不是必需的。

可以在http://polymathprogrammer.com/2009/11/09/how-to-create-stylesheet-in-excel-open-xml/中找到有關樣式表(以及其他OpenXML元素)的重要資源。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM