i creating excel file through OpenXML SDK 2.5. I have IDIsposable class which includesa variable
private SpreadsheetDocument ProcessDocument { get; set; }
I can insert a cell with date value:
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;
In this state when I open a created excel file in cell i see a number. that is OK, i mean, but now i need to set a cell format... I have a methods:
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;
}
In creating process i used it:
var formatIndex = excel.CreateCellFormat("dd.mm.yyyy");
excel.AddCellValue(workSheet, row, 6, DateTime.Now);
excel.SetCellStyle(workSheet, row, 6, null, null, formatIndex);
At this moment, when I open the generated excel, I receive this message: “Excel found unreadable content in 'test.xlsx'. Do you want to recover the contents of this workbook? If you trust the source of this workbook, click Yes.”
After repairing my excel report an error in the file "/xl/styles.xml".
Does anyone know where I make a mistake in creating the format of the cell? Thanks a lot