コード例 #1
0
        private void ExportBodyRows(IWorksheetExporter worksheetExporter, IEnumerable <dynamic> data, string[] columnHeaders, bool isDetailGrid)
        {
            //Add Cell Formatting
            SpreadCellFormat format = new SpreadCellFormat
            {
                FontSize            = 10,
                VerticalAlignment   = SpreadVerticalAlignment.Center,
                HorizontalAlignment = SpreadHorizontalAlignment.Center,
                Fill = SpreadPatternFill.CreateSolidFill(new SpreadColor(50, 190, 255)),
            };

            SpreadCellFormat detailFormat = new SpreadCellFormat
            {
                FontSize            = 10,
                VerticalAlignment   = SpreadVerticalAlignment.Center,
                HorizontalAlignment = SpreadHorizontalAlignment.Center
            };

            //Loop through data rows
            foreach (var item in data)
            {
                //Create a new row
                using IRowExporter rowExporter = worksheetExporter.CreateRowExporter();
                rowExporter.SetHeightInPoints(20 /*you can change this to suite your needs*/);

                //If the current exported grid is a detail grid leave a blank cell
                if (isDetailGrid)
                {
                    using ICellExporter cellExporter = rowExporter.CreateCellExporter();
                    cellExporter.SetValue(string.Empty);
                    cellExporter.SetFormat(detailFormat);
                }

                //Add value to each column key
                foreach (var key in columnHeaders)
                {
                    try
                    {
                        //Get Type of current datasource
                        Type type = data.FirstOrDefault().GetType();
                        var  prop = type.GetProperty(key);

                        var cellValue = prop.GetValue(item, null);
                        if (cellValue is null)
                        {
                            continue;
                        }
                        //check if the model has a List, which means it has a detail grid attached to it.
                        else if (cellValue is IList detailGrid)
                        {
                            //Dispose current rowExporter instance
                            rowExporter.Dispose();
                            ExportGrid(worksheetExporter, detailGrid.Cast <dynamic>(), true);
                        }

                        //Add value to Excel cell
                        using ICellExporter cellExporter = rowExporter.CreateCellExporter();
                        cellExporter.SetValue(cellValue.ToString());
                        cellExporter.SetFormat(isDetailGrid ? detailFormat : format);
                    }
                    catch (NullReferenceException exception)
                    {
                        Console.WriteLine(exception.Message);
                    }
                }
            }
        }