WorkbookStylesPart sp = workbookPart.AddNewPart<WorkbookStylesPart>();
Creare un foglio di stile,
sp.Stylesheet = new Stylesheet();
Creare un numberingformat,
sp.Stylesheet.NumberingFormats = new NumberingFormats();
// #.##% is also Excel style index 1
NumberingFormat nf2decimal = new NumberingFormat();
nf2decimal.NumberFormatId = UInt32Value.FromUInt32(3453);
nf2decimal.FormatCode = StringValue.FromString("0.0%");
sp.Stylesheet.NumberingFormat.Append(nf2decimal);
Creare un formato di cella e applicare l'id formato di numerazione
cellFormat = new CellFormat();
cellFormat.FontId = 0;
cellFormat.FillId = 0;
cellFormat.BorderId = 0;
cellFormat.FormatId = 0;
cellFormat.NumberFormatId = nf2decimal.NumberFormatId;
cellFormat.ApplyNumberFormat = BooleanValue.FromBoolean(true);
cellFormat.ApplyFont = true;
//append cell format for cells of header row
sp.Stylesheet.CellFormats.AppendChild<CellFormat>(cellFormat);
//update font count
sp.Stylesheet.CellFormats.Count = UInt32Value.FromUInt32((uint)sp.Stylesheet.CellFormats.ChildElements.Count);
//save the changes to the style sheet part
sp.Stylesheet.Save();
e quando si aggiunge il valore alla cella avere il seguente codice del centro hereonversion e applicare l'indice di stile nel mio caso ho avuto tre indice di stile quindi il 3 è stato il mio indice di stile percentuale 2 poiché gli indici iniziano da 0
string val = Convert.ToString(Convert.ToDecimal(value)/100);
Cell cell = new Cell();
cell.DataType = new EnumValue<CellValues>(CellValues.Number);
cell.CellValue = new CellValue(val);
cell.StyleIndex = 2;
row.Append(cell);
Questo flusso ha funzionato, potremmo avere una formattazione di celle con questo processo. Grazie ssyladin – Selwyn
Nessun problema. In realtà trascorrono circa 30 minuti cercando di eseguire la formattazione "manualmente" solo con OpenXML e alla fine si arrendono. Continuava a ottenere un foglio di lavoro corrotto. Quindi questa è una buona risposta a me, dal momento che aiuta a evitare le dipendenze della libreria esterna. – jklemmack
Il commento sull'indice di stile Excel indica che, se si desidera utilizzare lo stile percentuale di Excel predefinito, non è necessario aggiungere un nuovo formato, è sufficiente impostare la cella sull'indice di stile 1? – cidthecoatrack