Hi, I am having some problems when I export many sfDataGrids to a unique worksheet, previously in UWP, I used SfDataGrid. ExportToExcel, but this is not possible in Xamarin, they are used DataGridExcelExportingController instead, and I dont having luck in this, when I export to excel the sfDataGrids are having the itemsSource correctly (count) but the exported excel file is empty. Like the image:
To replicate the scenario just use: Export_To_MSExcel.ExportStaticListsToExcel();
My Code:
public class Export_To_MSExcel
{
public class GameConsole
{
public string Name { get; set; }
public string CreatedBy { get; set; }
}
public class Manufacturer
{
public string Name { get; set; }
public int FoundationYear { get; set; }
}
public class Values_GameConsole : ObservableCollection { }
public class Values_Manufacturer : ObservableCollection { }
public static class StaticCollections
{
public static Values_GameConsole GameConsoles = new Values_GameConsole();
public static Values_Manufacturer Manufacturers = new Values_Manufacturer();
static StaticCollections()
{
}
}
public static async void ExportStaticListsToExcel()
{
FillData();
var WorkBook = WorkbookCreator();
MemoryStream stream = new MemoryStream();
WorkBook.SaveAs(stream);
WorkBook.Close();
await DependencyService.Get().SaveFile(stream);
}
private static void FillData()
{
StaticCollections.GameConsoles.Add(new GameConsole()
{
Name = "Xbox",
CreatedBy = "Creator 1",
});
StaticCollections.GameConsoles.Add(new GameConsole()
{
Name = "SNES",
CreatedBy = "Creator 2",
});
StaticCollections.GameConsoles.Add(new GameConsole()
{
Name = "NES",
CreatedBy = "Creator 3",
});
StaticCollections.Manufacturers.Add(new Manufacturer()
{
Name = "Sony",
FoundationYear = 1000,
});
StaticCollections.Manufacturers.Add(new Manufacturer()
{
Name = "Nintendo",
FoundationYear = 1000,
});
StaticCollections.Manufacturers.Add(new Manufacturer()
{
Name = "Microsoft",
FoundationYear = 1000,
});
}
private static IWorkbook WorkbookCreator()
{
ExcelEngine excelEngine = new ExcelEngine();
IWorkbook workbook = excelEngine.Excel.Workbooks.Create(1);
workbook.Version = ExcelVersion.Excel2016;
SfDataGrid GameConsoles_SfDataGrid = new SfDataGrid() { AutoGenerateColumns = false, };
SfDataGrid Manufacturers_SfDataGrid = new SfDataGrid() { AutoGenerateColumns = false, };
GameConsoles_SfDataGrid.Columns.Add(new GridTextColumn() { HeaderText = string.Empty, MappingName = "Name" });
GameConsoles_SfDataGrid.Columns.Add(new GridTextColumn() { HeaderText = string.Empty, MappingName = "CreatedBy" });
Manufacturers_SfDataGrid.Columns.Add(new GridTextColumn() { HeaderText = string.Empty, MappingName = "Name" });
Manufacturers_SfDataGrid.Columns.Add(new GridTextColumn() { HeaderText = string.Empty, MappingName = "FoundationYear" });
GridQueryableCollectionViewWrapper GameConsoles_Wrapper = new GridQueryableCollectionViewWrapper(StaticCollections.GameConsoles, GameConsoles_SfDataGrid);
GridQueryableCollectionViewWrapper Manufacturers_Wrapper = new GridQueryableCollectionViewWrapper(StaticCollections.Manufacturers, Manufacturers_SfDataGrid);
GameConsoles_SfDataGrid.ItemsSource = GameConsoles_Wrapper;
Manufacturers_SfDataGrid.ItemsSource = Manufacturers_Wrapper;
DataGridExcelExportingController GameConsoles_Controller = new DataGridExcelExportingController();
DataGridExcelExportingController Manufacturers_Controller = new DataGridExcelExportingController();
GameConsoles_Controller.CellExporting += GameConsoles_Controller_CellExporting;
Manufacturers_Controller.CellExporting += Manufacturers_Controller_CellExporting;
DataGridExcelExportingOption GameConsoles_Options = new DataGridExcelExportingOption
{
StartColumnIndex = 1,
StartRowIndex = 1,
};
DataGridExcelExportingOption Manufacturers_Options = new DataGridExcelExportingOption
{
StartColumnIndex = 1,
StartRowIndex = 1 + 2 + StaticCollections.GameConsoles.Count,
};
GameConsoles_Options.ApplyGridStyle = true;
Manufacturers_Options.ApplyGridStyle = true;
GameConsoles_Controller.ExportToExcel(GameConsoles_SfDataGrid, GameConsoles_Wrapper, GameConsoles_Options, workbook.ActiveSheet);
Manufacturers_Controller.ExportToExcel(Manufacturers_SfDataGrid, Manufacturers_Wrapper, Manufacturers_Options, workbook.ActiveSheet);
return workbook;
}
private static void Manufacturers_Controller_CellExporting(object sender, DataGridCellExcelExportingEventArgs e)
{
if (e.CellType == ExportCellType.RecordCell)
{
if (e.Range.Column == 1)
{
e.Range.CellStyle.Font.Size = 12;
e.Range.CellStyle.VerticalAlignment = ExcelVAlign.VAlignCenter;
e.Range.CellStyle.Font.Bold = true;
}
if (e.Range.Column == 2)
{
e.Range.CellStyle.Font.Bold = false;
}
}
}
private static void GameConsoles_Controller_CellExporting(object sender, DataGridCellExcelExportingEventArgs e)
{
if (e.CellType == ExportCellType.RecordCell)
{
if (e.Range.Column == 1)
{
e.Range.CellStyle.Font.Size = 12;
e.Range.CellStyle.Font.Bold = true;
}
if (e.Range.Column == 2)
{
e.Range.CellStyle.Font.Bold = false;
}
}
}
}
I hope you can help me export correctly.
Best regards.