J'ai un code existant qui génère une feuille de calcul à partir d'Excel en utilisant OpenXML. Cela fonctionne bien, mais je dois toujours aller dans la feuille de calcul et ajouter le formatage.le support openXML (C#) génère-t-il des champs numériques formatés (comme 1 000 000)
Y at-il de toute façon dans mon code C# je peux spécifier que je veux une colonne formatée avec un certain nombre de mise en forme), virgules, décimales, etc?
tout lien vers des exemples serait génial.
voici comment je sauve les données maintenant:
public void SetCell<T>(string column, uint rowIndex, T value)
{
SheetData sheetData = _positionSheet;
Cell cell = GetCell(sheetData, column, rowIndex);
string stringValue = value == null ? "" : value.ToString();
cell.CellValue = new CellValue(stringValue);
switch (typeof(T).Name)
{
case "Date":
case "DateTime":
cell.DataType = CellValues.Date;
break;
case "Int32":
cell.DataType = CellValues.Number;
break;
default:
cell.DataType = CellValues.String;
break;
}
}
ci-dessous est la méthode GetCell() (même si je suppose que c'est pas vraiment pertinent pour la question:
static private Cell GetCell(SheetData sheet, string column, uint rowIndex)
{
Cell cell;
Row row = GetRow(sheet, rowIndex);
if (row.Elements<Cell>().Where(c => c.CellReference.Value == column + rowIndex).Any())
{
cell = row.Elements<Cell>().Where(c => c.CellReference.Value == column + rowIndex).First();
}
else
{
Cell refCell = null;
var comparer = new CellComparer();
foreach (Cell existingCell in row.Elements<Cell>())
{
if (comparer.Compare(existingCell.CellReference.Value, column + rowIndex) > 0)
{
refCell = existingCell;
break;
}
}
cell = new Cell { CellReference = column + rowIndex };
row.InsertBefore(cell, refCell);
}
return cell;
}