如何在没有任何映射的情况下用任何类的列表填充 OpenXML 中的一张 Excel 文件

问题描述 投票:0回答:1

我需要将我自己的班级或任何班级的

List
List<T>
)转换为带有
.xlsx
OpenXML

文件的Excel表格

此外,这必须尊重属性的类型(

String
Int
对于
TEXT
NUMBER

还需要每列的标题必须是我类中属性的名称,单词之间有空格。

喜欢:

internal class Lines
{
    public string Tipo_de_Línea { get; set; }

    public long Total { get; set; }

}

退货:

c# .net excel openxml export-to-excel
1个回答
0
投票

我将只发布工作表信息,因为有很多关于如何创建文件和添加工作表的信息。

解析列表并填充表格的一种方法:

 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;
        }

另一种创建具有相应类型的单元格的方法,设置引用 (A1...B2...) 并放置数据:

        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;
        }

用法:

SheetData mysheetData = MakeSheetData(myList);

添加工作表数据:

                    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);
© www.soinside.com 2019 - 2024. All rights reserved.