Example #1
0
#pragma warning disable CA1822 // Mark members as static
    public Cell Cell(object?value, DefaultStyle template, UInt32Value styleIndex)
#pragma warning restore CA1822 // Mark members as static
    {
        string excelValue = value == null ? "" :
                            template == DefaultStyle.DateTime ? ExcelExtensions.ToExcelDate(((DateTime)value)) :
                            template == DefaultStyle.Date ? value is DateTime dt?ExcelExtensions.ToExcelDate(dt) : ExcelExtensions.ToExcelDate(((DateOnly)value).ToDateTime()) :
                                template == DefaultStyle.Time ? ExcelExtensions.ToExcelTime((TimeOnly)value) :
                                template == DefaultStyle.Decimal ? ExcelExtensions.ToExcelNumber(Convert.ToDecimal(value)) :
                                template == DefaultStyle.Boolean ? ToYesNo((bool)value) :
                                template == DefaultStyle.Enum ? ((Enum)value).NiceToString() :
                                value.ToString() !;

        Cell cell = IsInlineString(template)?
                    new Cell(new InlineString(new Text {
            Text = excelValue
        }))
        {
            DataType = CellValues.InlineString
        } :
        new Cell {
            CellValue = new CellValue(excelValue), DataType = null
        };

        cell.StyleIndex = styleIndex;

        return(cell);
    }
Example #2
0
    public static void SetCellValue(this Cell cell, object?value, Type type)
    {
        if (type == typeof(string))
        {
            cell.RemoveAllChildren();
            cell.Append(new InlineString(new Text((string?)value !)));
            cell.DataType = CellValues.InlineString;
        }
        else
        {
            string excelValue = value == null ? "" :
                                type.UnNullify() == typeof(DateTime) ? ExcelExtensions.ToExcelDate(((DateTime)value)) :
                                type.UnNullify() == typeof(bool) ? (((bool)value) ? "TRUE": "FALSE") :
                                IsNumber(type.UnNullify()) ? ExcelExtensions.ToExcelNumber(Convert.ToDecimal(value)) :
                                value.ToString() !;

            cell.CellValue = new CellValue(excelValue);
        }
    }