public void SparseColumns() { var outPath = GetNewOutFilePath("-sparse"); var workbook = new ExcelPackage(new FileInfo(outPath)); const string sheetName = "One"; var exportConfig = new PocoThreeExportConfigurator(sheetName).Config; const int firstColumnIndex = 2; // this is index of the header row const int firstRowIndex = 3; exportConfig.LeftSheetColumnIndex = firstColumnIndex; exportConfig.TopSheetRowIndex = firstRowIndex; // no freezing panes exportConfig.FreezeColumnIndex = null; // move third column to the right Assert.IsNotEmpty(exportConfig.Columns[2].Caption, "Sheet column#2 has no caption"); var movedColumnConfig = exportConfig.GetAutoColumnConfig(exportConfig.Columns[2].Caption); Assert.IsNotNull(movedColumnConfig, "Failed to find column export config by caption"); movedColumnConfig.Index = exportConfig.Columns.Count + 2; // allow it to grow more at the end of the table movedColumnConfig.MaximumWidth = 300; var dataSetExportConfig = new DataSetExportAutoConfig(); dataSetExportConfig.AddSheet(exportConfig); var pocoList = Enumerable.Range(0, 100) .Select(i => new PocoThree(6)) .ToList(); var dataSet = new DataSetAdapter().Add(pocoList, sheetName); var exporter = new DataSetToWorkbookExporter(dataSetExportConfig) { DataSet = dataSet }; exporter.Export(workbook); workbook.Save(); TestContext.WriteLine($"Saved {outPath}."); workbook.Dispose(); workbook = new ExcelPackage(new FileInfo(outPath)); var columnReadingMap = exportConfig.Columns .Select(c => new KeyValuePair <string, int>(c.Caption, firstColumnIndex + c.Index)) .ToList(); const int startDataRowIndex = firstRowIndex + 1; var reader = new ExcelTableReader(workbook.Workbook.Worksheets["One"], startDataRowIndex, null, columnReadingMap); var readPocos = new PocoThreeReader().Read(reader); CheckEquality(pocoList, readPocos); workbook.Dispose(); if (_deleteExportedFiles) { File.Delete(outPath); } }
public void Polymorphic() { var outPath = GetNewOutFilePath("-polymorphic"); var workbook = new ExcelPackage(new FileInfo(outPath)); var dataSetExportConfig = new DataSetExportAutoConfig(); var configurator = new PocoExportConfigurator <PocoOne>("OneSheet", "One"); Expression <Func <PocoBase, int> > refId = o => o.Id; Expression <Func <PocoBase, DateTime> > refDateTime = o => o.DateTime; Expression <Func <PocoOne, IList <double?> > > refCollection = o => o.Values; Expression <Func <PocoOne, string> > refJoinedCollection = o => o.Values != null?string.Join(",", o.Values.Select(e => e.ToString())) : null; Expression <Func <PocoThree, int> > refPocoThreeInt = o => o.IntegerFromPocoThree; configurator .AddInheritedColumn(refId) .AddInheritedColumn(refDateTime) .AddColumn(refJoinedCollection, "Joined Values") .AddCollectionColumns(refCollection, 5, "value#{0}") .AddColumnPolymorphic(refPocoThreeInt); dataSetExportConfig.AddSheet(configurator.Config); dataSetExportConfig.AddSheet(new PocoThreeExportConfigurator("TwoSheet").Config); var dataSet = new DataSetAdapter(); var data1 = Enumerable.Range(0, 100) .Select(i => new PocoThree(6)) .ToList(); var data2 = Enumerable.Range(0, 1000) .Select(i => new PocoThree(9)) .ToList(); dataSet.Add(data1, "One"); dataSet.Add(data2, "TwoSheet"); var exporter = new DataSetToWorkbookExporter(dataSetExportConfig) { DataSet = dataSet }; exporter.Export(workbook); workbook.Save(); TestContext.WriteLine($"Saved {outPath}."); workbook.Dispose(); workbook = new ExcelPackage(new FileInfo(outPath)); var reader = ExcelTableReader.ReadContiguousTableWithHeader(workbook.Workbook.Worksheets["OneSheet"], 1); var readPocos = new PocoThreeReader().Read(reader); CheckEquality(data1, readPocos); workbook.Dispose(); if (_deleteExportedFiles) { File.Delete(outPath); } }