英文:
How to fill a sheet of Excel file in OpenXML with a List of any class without any mapping
问题
需要将自己的类或任何类的List
(List<T>
) 转换为.xlsx
文件的Excel表,使用OpenXML
。
同时,这必须考虑属性的类型(String
和 Int
分别对应 TEXT
和 NUMBER
)。
每列的标题也必须是类属性的名称,单词之间用空格分隔。
例如:
internal class Lines
{
public string Tipo_de_Línea { get; set; }
public long Total { get; set; }
}
返回:
英文:
I need to convert a List
of my own class or any class (List<T>
) into an Excel sheet of an .xlsx
file with OpenXML
Also this has to respect the type of the properties (String
and Int
for TEXT
and NUMBER
)
Also need the headers of every column has to be the names of the Properties in my class, with space between the words.
Like:
internal class Lines
{
public string Tipo_de_Línea { get; set; }
public long Total { get; set; }
}
returns:
答案1
得分: 0
I will post only the sheet information because there is plenty information on how to create the file and adding a sheet.
One method to parse the list and fill the sheet:
private static SheetData MakeSheetData<T>(List<T> input) {
// 设置标题行的行数
int rowCount = 1;
// 创建标题行
Row header = new Row();
// 设置行索引
header.RowIndex = (UInt32)1;
// 要返回的工作表数据
SheetData sheetData = new SheetData();
// 获取列表元素的类属性
PropertyInfo[] properties = typeof(T).GetProperties();
// 列计数器
int colCount = 1;
// 遍历每个属性
foreach (PropertyInfo property in properties)
{
// 创建单元格并用属性名称填充。如果属性名称是 "My_Data",那么该列的标题将是 "My Data"
Cell headerCell = CreateCell(colCount, rowCount, property.Name.Replace('_', ' '));
// 将单元格添加到行(在这种情况下是标题行)
header.AppendChild(headerCell);
// 增加列计数
colCount++;
}
// 现在添加另一行...
rowCount++;
// 不要忘记添加标题!
sheetData.AppendChild(header);
// 对于列表中的每个元素,每个元素都差不多
foreach (T fila in input)
{
colCount = 1;
Row newrow = new Row();
newrow.RowIndex = (UInt32)rowCount;
foreach (PropertyInfo property in properties)
{
// 我将在这里传递属性类型的名称,以获取单元格的 "类型"(我只使用文本和数字,但您可以选择您需要的任何类型)
Cell newcell = CreateCell(colCount, rowCount, property.GetValue(fila).ToString(), property.PropertyType.Name);
newrow.AppendChild(newcell);
colCount++;
}
sheetData.AppendChild(newrow);
rowCount++;
}
return sheetData;
}
Another method to create the cell with the respective type, set the reference (A1...B2...) and put the data:
private static Cell CreateCell(int positionX, int positionY, string data, string propertyType = "String")
{
// 仅用于单元格的引用,来自我的行计数和列计数
int unicode = 64 + positionX;
char character = (char)unicode;
Cell newCell = new Cell() { CellReference = character.ToString() + positionY };
// 单元格数据本身
newCell.CellValue = new CellValue(data);
// 我将设置类型,默认为 "string"(文本)
switch (propertyType.ToLower()) {
// 长整数或整数(在我的情况下,是一样的,您可以指定您想要的任何其他类型)
case "int32":
case "int64":
newCell.DataType = new EnumValue<CellValues>(CellValues.Number);
break;
// case "string":
default:
newCell.DataType = new EnumValue<CellValues>(CellValues.String);
break;
}
return newCell;
}
Usage:
SheetData mysheetData = MakeSheetData(myList);
Add sheetdata:
WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = new Worksheet(mysheetData );
Sheet sheet = new Sheet() { Id = myDoc.WorkbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "Name of the sheet" };
sheets.Append(sheet);
英文:
I will post only the sheet information because there is plenty information on how to create the file and adding a sheet.
One method to parse the list and fill the sheet:
private static SheetData MakeSheetData<T>(List<T> input) {
//set the row count for header
int rowCount = 1;
//create the header
Row header = new Row();
//create the row index
header.RowIndex = (UInt32)1;
//the sheet itself to return
SheetData sheetData = new SheetData();
//properties of class of my list
PropertyInfo[] properties = typeof(T).GetProperties();
//column counter
int colCount = 1;
//for each property
foreach (PropertyInfo property in properties)
{
//I will create a cell and fill with the name of the property. If the property name is "My_Data" the header of that column will be "My Data"
Cell headerCell = CreateCell(colCount, rowCount, property.Name.Replace('_',' '));
// append the cell to the row (header row in this case)
header.AppendChild(headerCell);
//increments the column count
colCount++;
}
//and now another row...
rowCount++;
//dont forget to append my header!
sheetData.AppendChild(header);
//for each element in my list, every is pretty the same
foreach (T fila in input)
{
colCount = 1;
Row newrow = new Row();
newrow.RowIndex = (UInt32)rowCount;
foreach (PropertyInfo property in properties)
{
//I will pass here also the name of the property type, to get the "type" of the cell (I'm working only with Text and Number but you can choose whatever you need)
Cell newcell = CreateCell(colCount, rowCount, property.GetValue(fila).ToString(),property.PropertyType.Name);
newrow.AppendChild(newcell);
colCount++;
}
sheetData.AppendChild(newrow);
rowCount++;
}
return sheetData;
}
Another method to create the cell with the respective type, set the reference (A1...B2...) and put the data:
private static Cell CreateCell(int positionX, int positionY, string data, string propertyType = "String")
{
//this only will be for the reference of the cell, coming from my row count and my column count
int unicode = 64 + positionX;
char character = (char)unicode;
Cell newCell = new Cell() { CellReference = character.ToString() + positionY };
//the data itself
newCell.CellValue = new CellValue(data);
//I will set the type, default is "string" (text)
switch (propertyType.ToLower()) {
//long or int (for my case, is the same, you can specify whatever you want or another type)
case "int32":
case "int64":
newCell.DataType = new EnumValue<CellValues>(CellValues.Number);
break;
// case "string":
default:
newCell.DataType = new EnumValue<CellValues>(CellValues.String);
break;
}
return newCell;
}
Usage:
SheetData mysheetData = MakeSheetData(myList);
Add sheetdata:
WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = new Worksheet(mysheetData );
Sheet sheet = new Sheet() { Id = myDoc.WorkbookPart.GetIdOfPart(worksheetPart),SheetId = 1,Name = "Name of the sheet"};
sheets.Append(sheet);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论