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):
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?