Hi, I am using ASP.Net MVC. On Ignite UI's website , The sample contains numerous method including method that set up the columns and rows of the worksheet manually according to the grid and then the data is exported to it.
Isn't there a shorter way of exporting to Excel without manually designing the layout of the Excel Workbook? Why cant the grid provide all the column settings to the Excel Exporter,
I think in WebForms we have to write only one line like this --> this.ultraGridExcelExporter1.Export(this.ultraGrid1, "C:\\GridData.xls");
this.ultraGridExcelExporter1.Export(this.ultraGrid1, "C:\\GridData.xls");
this
This line exports the data of the grid directly to the excel sheet without specifying manually about the columns and rows of the grid, Can we do this in Ignite UI?
Why do we have to specify like this(FROM IGNITE UI SAMPLE) :
public void PopulateExcelWorkbook(List<Order> data) { Worksheet currentWorksheet = this.excelWorkbook.Worksheets.Add("WorkSheet1"); foreach (var cell in currentWorksheet.GetRegion("A1:D1")) { cell.CellFormat.Fill = CellFill.CreateSolidFill(Color.Gray); cell.CellFormat.Font.ColorInfo = new WorkbookColorInfo(Color.White); } currentWorksheet.Rows[0].Cells[0].Value = "Order ID"; currentWorksheet.Rows[0].Cells[1].Value = "Contact Name"; currentWorksheet.Rows[0].Cells[2].Value = "Shipping Address"; currentWorksheet.Rows[0].Cells[3].Value = "Order Date"; currentWorksheet.Columns[0].Width = 3000; currentWorksheet.Columns[0].CellFormat.Alignment = HorizontalCellAlignment.Left; currentWorksheet.Columns[1].Width = 7100; currentWorksheet.Columns[2].Width = 3000; currentWorksheet.Columns[2].CellFormat.Alignment = HorizontalCellAlignment.Left; currentWorksheet.Columns[3].Width = 6100; int i = 1; foreach (Order order in data) { currentWorksheet.Rows[i].Cells[0].Value = order.OrderID; currentWorksheet.Rows[i].Cells[1].Value = order.ContactName; currentWorksheet.Rows[i].Cells[2].Value = order.ShipAddress; currentWorksheet.Rows[i].Cells[3].Value = order.OrderDate != null ? string.Format("{0:d}", order.OrderDate) : ""; i++; } }
Yes you are right but I have one grid & one view only , the same view & grid will display data for different dataSource urls at runtime. Therefore the export function has to be explicitly called in if-else conditions like this :
if(entityValue="Users") { List items = new List(); ExportGenericList(items); }
if(entityValue="Roles") { List items = new List(); ExportGenericList(items); }
..
if(entityValue="Modules") { List items = new List(); ExportGenericList(items); }
if(entityValue="Permissions") { List items = new List(); ExportGenericList(items); }
This list will increase with time and there will be many if-else blocks or switch-case statements in order to initialize the items with the appropriate generic parameter T at compile time because I cannot provide generic parameter T at runtime through reflection depending on the selected entityValue string.
This is a generic method afterall but in my scenario I dont have one-to-one mapping of View & my Grid DataSourcUrl. There is only one view & many DataSourceUrls therefore all these if-else conditions would have to be listed in a single method which will be called on "Export" button click.
Anyway, for the time being I will have to stick to this method. Thanks again for your help & code sample.
Hello Omer,
Actually the method accepts any List<T> and is not dependent on the object Type.
So you can call it on any non-generic List you have for example if you have 2 Lists:
List<Item> items = new List<Item>();
…
List<Item2> items2 = new List<Item2>();
The method can be called in the same way for any of them.
For example:
ExportGenericList(items);
or
ExportGenericList(items2);
Let me know if you have any questions.
Best Regards,
Maya Kirova
Developer Support Engineer II
Infragistics, Inc.
http://ko.infragistics.com/support
Although this method will provide generic functionality but still I would have to provide Type T <> at compile time which means many if-else blocks for calling this method with the generic parameter T. But nevertheless for now I would have to use this method until there is an builtin export option for IgniteUi grid made available just like Webforms grid. Thanks for your code sample , I am going to use it . Thanks.
Please feel free to log this as a new product idea on our website <http://ideas.infragistics.com>.
Otherwise if all the data models for the grid are List<T> you could create a generic method that can accept the different list of object that you would pass and using Reflection (System.Reflection) you can get that specific collections property names and values and populate the related worksheet accordingly.
Such a method could look for example like this:
private void ExportGenericList<T>(List<T> items)
{
Workbook book = new Workbook();
Worksheet currentWorksheet = book.Worksheets.Add("WorkSheet1");
var yourObject = items[0];
List<string> propNames = new List<string>();
foreach (var prop in yourObject.GetType().GetProperties())
var propName = prop.Name;
propNames.Add(propName);
}
//export header. The header contains the property names.
for (int i = 0; i < propNames.Count; i++)
currentWorksheet.Rows[0].Cells[i].Value = propNames[i];
//populate data in worksheet
int j = 1;
foreach (var item in items)
int k = 0;
var props = item.GetType().GetProperties();
foreach (var prop in props)
var value = prop.GetValue(item,null);
currentWorksheet.Rows[j].Cells[k].Value = value;
k++;
j++;
SendForDownload(book, WorkbookFormat.Excel2007);
And you can use the following method to send the generated workbook to the client:
private void SendForDownload(Workbook document, WorkbookFormat excelFormat)
string documentFileNameRoot;
documentFileNameRoot = string.Format("Document.{0}", excelFormat == WorkbookFormat.Excel97To2003 ? "xls" : "xlsx");
Response.Clear();
Response.AppendHeader("content-disposition", "attachment; filename=" + documentFileNameRoot);
Response.ContentType = "application/octet-stream";
document.SetCurrentFormat(excelFormat);
document.Save(Response.OutputStream);
Response.End();
Oh ok . I think manually doing it would require me to write the same code for different models which is not desirable in my situation because one grid displays data for different models at runtime, if I have 50 models than 50 methods for exporting their data to Excel although all of them are displayed in the same grids, only the dataSourceUrl is changed at runtime. There would be a lot of code replication.