如何使用Telerik导出到Excel(XLSX)时解释集合?

情景


我正在使用Telerik UI For Windows窗体

我有一个RadGridView ,我代表一个名为MarketInfo的自定义types:

 Public NotInheritable Class MarketInfo ... Public ReadOnly Property Participants As ReadOnlyCollection(Of ParticipantInfo) Get Return Me.GetParticipants() End Get End Property ... End Class 

它只包含文本和布尔值属性,而Participants属性则返回另一个自定义types的集合:

 Private Function GetParticipants(ByVal market As XElement) As ReadOnlyCollection(Of ParticipantInfo) Dim participantInfoList As New List(Of ParticipantInfo) For Each participantNode As XElement In market...<participant> participantInfoList.Add(New ParticipantInfo(participantNode)) Next Return New ReadOnlyCollection(Of ParticipantInfo)(participantInfoList) End Function 

这是完整的ParticipantInfo类:

 Public NotInheritable Class ParticipantInfo Private ReadOnly participantElement As XElement Public ReadOnly Property Name As String Get Return participantElement.@name End Get End Property Public ReadOnly Property Id As String Get Return participantElement.@id End Get End Property Public ReadOnly Property Odds As String Get Return participantElement.@odds End Get End Property Public ReadOnly Property OddsDecimal As String Get Return participantElement.@oddsDecimal End Get End Property Public ReadOnly Property LastUpdateDate As String Get Return participantElement.@lastUpdateDate End Get End Property Public ReadOnly Property LastUpdateTime As String Get Return participantElement.@lastUpdateTime End Get End Property Public ReadOnly Property Handicap As String Get Return participantElement.@handicap End Get End Property Public Sub New(ByVal participantElement As XElement) Me.participantElement = participantElement End Sub Private Sub New() End Sub End Class 

所以基本上我需要导出一个ParticipantInfotypes的集合,这应该可以在Excel中表示。

那么,在RadGridView我隐藏Participants的列,因为它不能表示它(因为它是一个集合),然后我加载该集合作为另一个RadGridView上的数据源。

为了更好地理解它,这是结果:

在这里输入图像说明

问题


我的问题是,我不知道如何解释这个在Excel文件(XLSX)。

这是我试图导出MarketInfo网格内容的代码:

 Dim exporter As New ExportToExcelML(rdg) With exporter .HiddenColumnOption = HiddenOption.ExportAlways .HiddenRowOption = HiddenOption.ExportAlways .ExportVisualSettings = True .SheetMaxRows = ExcelMaxRows._65536 .SheetName = "xxxxxxxx" .SummariesExportOption = SummariesOption.ExportAll .PagingExportOption = PagingExportOption.AllPages .FileExtension = ".xlsx" .RadGridViewToExport = rdg .ChildViewExportMode = ChildViewExportMode.ExportAllViews End With exporter.RunExport(fileName) 

但是,生成的文件只包含参与者的types名称:

 ... <Data ss:Type="String">System.Collections.ObjectModel.ReadOnlyCollection`1[WilliamHillLeecher.Leecher.Types.ParticipantInfo]</Data></Cell></Row> ... 

在这里输入图像说明

我希望看到每个MarketInfo创build一个Excel页面与那些缺less的属性。

我不熟悉Excel的用法和Excel的术语,我不知道如何正常地表示一个表中的集合,我想通过创build一个新的表单页面,并“链接”到相应的单元格。

我只是想在我的应用程序中表示的Excel文件中表示相同的信息。


如何用Telerik导出相关的库来做到这一点?

如果它不可能使用Telerik库,那么我怎么可以与其他第三方免费库?

(有了这个,我只是告诉我可以接受其他types的build议,但是请记住,我知道更重点的Excel库,但是我仍然不知道如何做到这一点与任何lib …可能是由于误解如何才能完成相同的任务添加/表示使用Excel用户界面的集合。)

  1. 在内存中填充一个新的RadGridView,并像其他人那样输出: http : //www.telerik.com/forums/radgrid-hierarchy-export-to-excel-showing-exporting-master-table-rows-但细节-行-是空白线

  2. 您可以控制使用XML创build的Excel XML,与OpenXML相比, ClosedXML非常易于使用。

  3. 有大量的其他选项和

  4. 使用Telerik打开支持请求可能是确认GridViewExportOptions中是否存在Sheet Per Detail页面的任何选项的最快方法。

如果你愿意使用开源库,试试这个: EPPlus一旦你在你的项目中引用了epplus.dll,下面的代码可以以你想要的方式将数据导出到xlsx格式:

 using OfficeOpenXml.Drawing; using OfficeOpenXml.Drawing.Chart; using OfficeOpenXml.Style; using OfficeOpenXml.Table; using OfficeOpenXml.Table.PivotTable; namespace OfficeOpenXml { public static class ExcelHelper { public static void Save(dynamic Table, string path) { var excel = CreateReport(Table); excel.SaveAs(path); } static void Save(this ExcelPackage excel, string path) { excel.SaveAs(path); } static ExcelWorksheet Export(ExcelPackage excel, dynamic Table, out ExcelRange range) // here it is your grid { var sheet = excel.Workbook.Worksheets.Add("Master_Data"); int row=0, col=0; foreach (dynamic dc in Table.Columns) { sheet.Cell(row, col++).Value = dc.Caption; } row++; col = 0; foreach (dynamic dr in Table.Rows) { foreach (object value in dr.ItemArray) { sheet.Cell(row, col++).Value = value; } row++; col = 0; } range= sheet.Cells[0, 0, row - 1, Table.Columns.Count - 1]; return sheet; } static ExcelPackage CreateReport(dynamic Table) { var excel = new ExcelPackage(); ExcelRange range; ExcelWorksheet sheet; sheet = Export( excel, Table, out range); CreatePivotTable(range, TableStyles.Medium12); return excel; } static ExcelPivotTable CreatePivotTable(ExcelRange range, TableStyles tableStyle) { int count = range.Worksheet.Workbook.Worksheets.Count; var summary = range.Worksheet.Workbook.Worksheets.Add("PivotTable" + count); var pivotTable = summary.PivotTables.Add(summary.Cells["A3"], range, "Summary" + count); pivotTable.ApplyBorderFormats = true; pivotTable.ApplyNumberFormats = true; pivotTable.TableStyle = tableStyle; pivotTable.WorkSheet.View.ShowGridLines = false; pivotTable.MultipleFieldFilters = false; pivotTable.RowGrandTotals = false; pivotTable.ColumGrandTotals = false; pivotTable.Compact = false; pivotTable.CompactData = false; pivotTable.GridDropZones = false; pivotTable.Outline = false; pivotTable.OutlineData = false; pivotTable.ShowError = true; pivotTable.ErrorCaption = "[error]"; pivotTable.ShowHeaders = false; pivotTable.UseAutoFormatting = true; pivotTable.ApplyWidthHeightFormats = true; pivotTable.ShowDrill = true; //pivotTable.DataOnRows = false; pivotTable.WorkSheet.View.FreezePanes(pivotTable.PageFields.Count + pivotTable.ColumnFields.Count + 3, 1); foreach (var fld in pivotTable.Fields) { pivotTable.RowFields.Add(fld); fld.Compact = false; fld.Outline = false; fld.ShowAll = false; fld.SubtotalTop = false; fld.SubTotalFunctions = eSubTotalFunctions.None; } return pivotTable; } } } 

我在一些函数中使用了dynamic关键字,因为我不知道如何访问Telerik Grid中的数据,但是我想它可能有行和列的属性。 如果不是这种情况,那么你将不得不改变那部分代码。 如果您使用DataTable作为网格的数据源,那么您可以直接传递DataTable。 一旦创build了一个文件,就可以用MS Excel或任何其他支持openxml格式的应用程序打开它。 你将不得不玩ExcelPivotTable对象,以获得所需的结果。 另请参阅下面的示例报告的屏幕截图: 示例报告