0

I have written this class to write a collection of any type to an excel file:

public static class ExcelWriter
{
    public static void WriteToExcelFile(IEnumerable<object> collection, string filePath)
    {
        if (collection?.Any() != true || String.IsNullOrWhiteSpace(filePath))
            return;

        if (File.Exists(filePath))
            File.Delete(filePath);

        using (var document = SpreadsheetDocument.Create(filePath, SpreadsheetDocumentType.Workbook))
        {
            WorkbookPart workbookPart = document.AddWorkbookPart();
            workbookPart.Workbook = new Workbook();

            WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
            worksheetPart.Worksheet = new Worksheet();

            Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets());

            Sheet sheet = new Sheet()
            {
                Id = workbookPart.GetIdOfPart(worksheetPart),
                SheetId = 1,
                Name = "Sheet1"
            };

            sheets.Append(sheet);

            workbookPart.Workbook.Save();

            SheetData sheetData = worksheetPart.Worksheet.AppendChild(new SheetData());

            sheetData.AppendChild(generateHeaderRow(collection.First().GetType()));

            foreach (var item in collection)
            {
                sheetData.AppendChild(generateValuesRow(item));
            }

            worksheetPart.Worksheet.Save();
        }
    }

    private static Row generateHeaderRow(Type dataType)
    {
        var propertyNames = dataType.GetProperties().Select(p => p.Name).ToList();

        var headerCells = new Cell[propertyNames.Count];
        for (int i = 0; i < propertyNames.Count; i++)
        {
            headerCells[i] = createCell(propertyNames[i], CellValues.String);
        }

        return new Row(headerCells);
    }

    private static Row generateValuesRow(object rowData)
    {
        var cells = new List<Cell>();

        foreach (var property in rowData.GetType().GetProperties())
        {
            var propertyValue = property.GetValue(rowData);
            cells.Add(createCell(propertyValue.ToString(), getCellValueByType(propertyValue)));
        }

        return new Row(cells);
    }

    private static CellValues getCellValueByType(object propertyValue)
    {
        string propertyValueString = propertyValue.ToString();
        if (Double.TryParse(propertyValueString, out _))
        {
            return CellValues.Number;
        }
        if (DateTime.TryParse(propertyValueString, out _))
        {
            return CellValues.Date;
        }
        if (Boolean.TryParse(propertyValueString, out _))
        {
            return CellValues.Boolean;
        }

        return CellValues.String;
    }

    private static Cell createCell(string value, CellValues dataType)
    {
        return new Cell()
        {
            CellValue = new CellValue(value),
            DataType = new EnumValue<CellValues>(dataType)
        };
    }
}

This actually generates the excel file, but when I open that file in Excel web it opens in Viewing mode and says you need to repair it, in order to edit(Excel can't repair it though): enter image description here

I changed my code to work with a specific type and it worked without this issue. That code looks like this:

public class TypeDependentWriter
{
    public static void WriteToExcelFile(IEnumerable<TestDataType> collection, string filePath)
    {
        if (collection?.Any() != true || String.IsNullOrWhiteSpace(filePath))
            return;

        if (File.Exists(filePath))
            File.Delete(filePath);

        using (var document = SpreadsheetDocument.Create(filePath, SpreadsheetDocumentType.Workbook))
        {
            WorkbookPart workbookPart = document.AddWorkbookPart();
            workbookPart.Workbook = new Workbook();

            WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
            worksheetPart.Worksheet = new Worksheet();

            Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets());

            Sheet sheet = new Sheet()
            {
                Id = workbookPart.GetIdOfPart(worksheetPart),
                SheetId = 1,
                Name = "Sheet1"
            };

            sheets.Append(sheet);

            workbookPart.Workbook.Save();

            SheetData sheetData = worksheetPart.Worksheet.AppendChild(new SheetData());

            // Header row.
            Row row = new Row();

            row.Append(
                createCell("Id", CellValues.String),
                createCell("Name", CellValues.String),
                createCell("Birth Date", CellValues.String));

            sheetData.AppendChild(row);

            // Data rows.
            foreach (var item in collection)
            {
                row = new Row();

                row.Append(
                    createCell(item.Id.ToString(), CellValues.Number),
                    createCell(item.Name, CellValues.String),
                    createCell(item.DateOfBirth.ToString("yyyy/MM/dd"), CellValues.String));

                sheetData.AppendChild(row);
            }

            worksheetPart.Worksheet.Save();
        }
    }

    private static Cell createCell(string value, CellValues dataType)
    {
        return new Cell()
        {
            CellValue = new CellValue(value),
            DataType = new EnumValue<CellValues>(dataType)
        };
    }
}

How can I make the first code work?

Hossein Ebrahimi
  • 632
  • 10
  • 20
  • 1
    I suggest you use both code bases to generate a file based on `TestDataType`, then open the two files in BeyondCompare (or a similar tool) and see where they diverge. – John Wu Apr 12 '21 at 15:41

1 Answers1

0

The reason my second code wasn't generating the issue was this part:

row.Append(
    createCell(item.Id.ToString(), CellValues.Number),
    createCell(item.Name, CellValues.String),
    createCell(item.DateOfBirth.ToString("yyyy/MM/dd"), CellValues.String /* I'm using string for DateTime here. */));

I figured out that I should:

  • use CellValues.Number for dates.
  • and call .ToOADate() extension method on the DateTime variable before calling ToString().
  • I should define a cell style and apply that style to the cells that store dates.

So I'm now creating date cells this way:

var dateCell = new Cell()
{
    CellValue = new CellValue(((DateTime)propertyValue).ToOADate()
        .ToString(CultureInfo.InvariantCulture)),
    DataType = new EnumValue<CellValues>(CellValues.Number),
    StyleIndex = 1 /* Style index for date cells */
}

And the style for date cells can be defined this way(I took this from Dave Williams post):

var CellFormats = new CellFormats();
CellFormats.Append(new CellFormat()
{
    BorderId = 0,
    FillId = 0,
    FontId = 0,
    NumberFormatId = 14,
    FormatId = 0,
    ApplyNumberFormat = true
});
CellFormats.Count = (uint)CellFormats.ChildElements.Count;
var StyleSheet = new Stylesheet();
StyleSheet.Append(CellFormats);

But if you don't have any other defined Fill, Font... this is not going to work because we're using FillId = 0, etc. which we haven't defined, a minimum style you can define to get this work also can be found on Dave Williams post:

private static Stylesheet GetStylesheet()
{
    var StyleSheet = new Stylesheet();

     // Create "fonts" node.
    var Fonts = new Fonts();
    Fonts.Append(new Font()
    {
        FontName = new FontName() { Val = "Calibri" },
        FontSize = new FontSize() { Val = 11 },
        FontFamilyNumbering = new FontFamilyNumbering() { Val = 2 },
    });

    Fonts.Count = (uint)Fonts.ChildElements.Count;

    // Create "fills" node.
    var Fills = new Fills();
    Fills.Append(new Fill()
    {
        PatternFill = new PatternFill() { PatternType = PatternValues.None }
        });
        Fills.Append(new Fill()
        {
            PatternFill = new PatternFill() { PatternType = PatternValues.Gray125 }
        });

    Fills.Count = (uint)Fills.ChildElements.Count;

    // Create "borders" node.
    var Borders = new Borders();
    Borders.Append(new Border()
    {
        LeftBorder = new LeftBorder(),
        RightBorder = new RightBorder(),
        TopBorder = new TopBorder(),
        BottomBorder = new BottomBorder(),
        DiagonalBorder = new DiagonalBorder()
    });

    Borders.Count = (uint)Borders.ChildElements.Count;

    // Create "cellStyleXfs" node.
    var CellStyleFormats = new CellStyleFormats();
    CellStyleFormats.Append(new CellFormat()
    {
        NumberFormatId = 0,
        FontId = 0,
        FillId = 0,
        BorderId = 0
    });

    CellStyleFormats.Count = (uint)CellStyleFormats.ChildElements.Count;

    // Create "cellXfs" node.
    var CellFormats = new CellFormats();

    // A default style that works for everything but DateTime
    CellFormats.Append(new CellFormat()
    {
        BorderId = 0,
        FillId = 0,
        FontId = 0,
        NumberFormatId = 0,
        FormatId = 0,
        ApplyNumberFormat = true
    });

   // A style that works for DateTime (just the date)
   CellFormats.Append(new CellFormat()
    {
        BorderId = 0,
        FillId = 0,
        FontId = 0,
        NumberFormatId = 14, // or 22 to include the time
        FormatId = 0,
        ApplyNumberFormat = true
    });

    CellFormats.Count = (uint)CellFormats.ChildElements.Count;

    // Create "cellStyles" node.
    var CellStyles = new CellStyles();
    CellStyles.Append(new CellStyle()
    {
        Name = "Normal",
        FormatId = 0,
        BuiltinId = 0
    });
    CellStyles.Count = (uint)CellStyles.ChildElements.Count;

    // Append all nodes in order.
    StyleSheet.Append(Fonts);
    StyleSheet.Append(Fills);
    StyleSheet.Append(Borders);
    StyleSheet.Append(CellStyleFormats);
    StyleSheet.Append(CellFormats);
    StyleSheet.Append(CellStyles);

    return StyleSheet;
}
Hossein Ebrahimi
  • 632
  • 10
  • 20