diff --git a/PCAxis.Serializers/Excel/CellValueHelper.cs b/PCAxis.Serializers/Excel/CellValueHelper.cs new file mode 100644 index 0000000..38edc0f --- /dev/null +++ b/PCAxis.Serializers/Excel/CellValueHelper.cs @@ -0,0 +1,46 @@ +using System; + +using ClosedXML.Excel; + +namespace PCAxis.Serializers.Excel +{ + internal static class CellValueHelper + { + internal static void SetTypedCellValue(IXLCell cell, object value) + { + switch (value) + { + case string s: + cell.SetValue(s); + break; + case bool b: + cell.SetValue(b); + break; + case int i: + cell.SetValue(i); + break; + case long l: + cell.SetValue(l); + break; + case double d: + cell.SetValue(d); + break; + case float f: + cell.SetValue(f); + break; + case decimal m: + cell.SetValue((double)m); + break; + case DateTime dt: + cell.SetValue(dt); + break; + case TimeSpan ts: + cell.SetValue(ts); + break; + default: + cell.SetValue(value?.ToString()); + break; + } + } + } +} diff --git a/PCAxis.Serializers/PCAxis.Serializers.csproj b/PCAxis.Serializers/PCAxis.Serializers.csproj index 9a20396..1cfbe7d 100644 --- a/PCAxis.Serializers/PCAxis.Serializers.csproj +++ b/PCAxis.Serializers/PCAxis.Serializers.csproj @@ -31,7 +31,7 @@ - + diff --git a/PCAxis.Serializers/Xlsx2Serializer.cs b/PCAxis.Serializers/Xlsx2Serializer.cs index f4bb9a7..3c12f70 100644 --- a/PCAxis.Serializers/Xlsx2Serializer.cs +++ b/PCAxis.Serializers/Xlsx2Serializer.cs @@ -96,7 +96,7 @@ protected virtual void SetCellValue(IXLCell cell, CellContentType type, object v if (type == CellContentType.Comment) cell.GetComment().AddText(value.ToString()); else - cell.SetValue(value); //Change from cell.Value = contentInfo to SetValue(..) For not format e.g 10-11 to date + CellValueHelper.SetTypedCellValue(cell, value); // ClosedXML 0.100+ requires concrete value types } protected virtual void SetCellFormat(IXLCell cell, CellContentType type, object value, FormatCellDescription changes) @@ -141,7 +141,7 @@ private XLWorkbook CreateWorkbook(PCAxis.Paxiom.PXModel model) // Writes values for the stub and data cells row = WriteAllRows(row, model, sheet, fmt); - // Writes the information + // Writes the information WriteAllTableExtraMetadata(row + 1, model, sheet); return book; @@ -233,7 +233,7 @@ private int WriteAllRows(int row, PXModel model, IXLWorksheet sheet, DataFormatt !value.IsNumeric() ? (FormatCellDescription)(c => { c.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right; }) : - (FormatCellDescription)(c => { c.DataType = XLDataType.Number; c.Style.NumberFormat.Format = FormatNumericCell(GetDecimalPrecision(value, fmt.DecimalSeparator)); }) + (FormatCellDescription)(c => { c.Style.NumberFormat.Format = FormatNumericCell(GetDecimalPrecision(value, fmt.DecimalSeparator)); }) ); if (!string.IsNullOrEmpty(n)) { @@ -285,7 +285,7 @@ private int WriteAllNotes(int row, PXModel model, IXLWorksheet sheet) //Writes mandantory contentInfo notes row = WriteValueNotes(row, model, sheet); - //Writes mandantory cellnotes + //Writes mandantory cellnotes row = WriteCellNotes(row, model, sheet); return row; } diff --git a/PCAxis.Serializers/XlsxSerializer.cs b/PCAxis.Serializers/XlsxSerializer.cs index 5c25298..69a796a 100644 --- a/PCAxis.Serializers/XlsxSerializer.cs +++ b/PCAxis.Serializers/XlsxSerializer.cs @@ -85,7 +85,7 @@ protected virtual void SetCellValue(IXLCell cell, CellContentType type, object v if (type == CellContentType.Comment) cell.GetComment().AddText(value.ToString()); else - cell.SetValue(value); //Change from cell.Value = value to SetValue(..) For not format e.g 10-11 to date + CellValueHelper.SetTypedCellValue(cell, value); // ClosedXML 0.100+ requires concrete value types } protected virtual void SetCellFormat(IXLCell cell, CellContentType type, object value, FormatCellDescription changes) @@ -109,7 +109,7 @@ private XLWorkbook CreateWorkbook(PCAxis.Paxiom.PXModel model) /* sheet.Cell(1, 1).Value = model.Meta.Title; sheet.Cell(1, 1).Style.Font.FontSize = 14; - sheet.Cell(1, 1).Style.Font.Bold = true; + sheet.Cell(1, 1).Style.Font.Bold = true; */ setCell( @@ -187,7 +187,7 @@ private XLWorkbook CreateWorkbook(PCAxis.Paxiom.PXModel model) !value.IsNumeric() ? (FormatCellDescription)(c => { c.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right; }) : - (FormatCellDescription)(c => { c.DataType = XLDataType.Number; c.Style.NumberFormat.Format = FormatNumericCell(GetDecimalPrecision(value, fmt.DecimalSeparator)); }) + (FormatCellDescription)(c => { c.Style.NumberFormat.Format = FormatNumericCell(GetDecimalPrecision(value, fmt.DecimalSeparator)); }) ); if (!string.IsNullOrEmpty(n)) { @@ -202,7 +202,7 @@ private XLWorkbook CreateWorkbook(PCAxis.Paxiom.PXModel model) if (_showDataNoteCells && !String.IsNullOrEmpty(dataNote)) { - //sheet.Cell(row, column + dataNoteNoteOffset).Value = dataNote; + //sheet.Cell(row, column + dataNoteNoteOffset).Value = dataNote; setCell( sheet.Cell(row, column + dataNoteNoteOffset), CellContentType.DataNote, @@ -337,7 +337,7 @@ private int WriteFootnotes(int row, PXModel model, IXLWorksheet sheet) } } - //Writes mandantory cellnotes + //Writes mandantory cellnotes CellNote cn; VariableValuePair vvp; for (int i = 0; i < model.Meta.CellNotes.Count; i++) @@ -610,7 +610,7 @@ private int WriteTableInformation(int row, PXModel model, IXLWorksheet sheet) row++; for (int i = 0; i < str.Length; i++) { - //sheet.Cell(row++, 2).Value = str[i]; + //sheet.Cell(row++, 2).Value = str[i]; setCell( sheet.Cell(row++, 1), CellContentType.Info, @@ -1087,7 +1087,7 @@ private int WriteTableInformation(int row, PXModel model, IXLWorksheet sheet) } //OFFICIAL STATISTICS - //If the statistics are official, insert information about that in the file + //If the statistics are official, insert information about that in the file //Reqtest error report #406 row++; if (model.Meta.OfficialStatistics) @@ -1158,7 +1158,7 @@ private void WriteHeading(PXModel model, IXLWorksheet sheet) { //INTERVAL int hInterval = CalcPostHeadingInterval(i, model); - //HEADING + //HEADING p = CalcPreHeadingInterval(i, model); //0; for (int l = 0; l <= p; l++) @@ -1294,7 +1294,7 @@ private void GetStubCell(PXModel model, IXLWorksheet sheet, int stubNr, int rowN sheet.Cell(row, column), CellContentType.Code, val.Code, - c => { c.DataType = XLDataType.Text; c.Style.Font.Bold = true; } + c => { c.Style.Font.Bold = true; } ); //sheet.Cell(row, column).DataType = XLCellValues.Text; //sheet.Cell(row, column).Style.Font.Bold = true; @@ -1322,7 +1322,7 @@ private void GetStubCell(PXModel model, IXLWorksheet sheet, int stubNr, int rowN { /* sheet.Cell(row, column).Value = val.Text; - sheet.Cell(row, column).Style.Font.Bold = true; + sheet.Cell(row, column).Style.Font.Bold = true; */ setCell(