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++; } }
Hello Omer,
Thank you for posting in our forum.
I’ve looked into this and there’s currently no method that allows the igGrid/igHierarchicalGrid to be exported with its current layout in excel.
The current recommended approach is to export the grid’s data manually by defining the layouts and filling the data.
If you wish to see such a method implemented in future version please log it as a new product idea so that it may be considered for implementation.
You can suggest new product ideas for future versions (or vote for existing ones) at <http://ideas.infragistics.com>.
Steps to create your idea:
1. Log into the Infragistics Product Ideas site at http://ideas.infragistics.com (creating a new login if needed).
2. Navigate to the product / platform channel of your choice (e.g. WPF, Windows Forms, ASP.NET, HTML5 / Ignite UI, iOS / NucliOS, etc.)
3. Add your product idea and be sure to be specific and provide as much detail as possible. Explain the context in which a feature would be used, why it is needed, why it can’t be accomplished today, and who would benefit from it. You can even add screenshots to build a stronger case. Remember that for your suggestion to be successful, you need other members of the community to vote for it. Be convincing!
The benefits of submitting the product idea yourself include:
- Direct communication with our product management team regarding your product idea.
- Notifications whenever new information regarding your idea becomes available.
Additional benefits of the product idea system include:
- Ability to vote on your favorite product ideas to let us know which ones are the most important to you. You will have ten votes for this and can change which ideas you are voting for at any time.
- Allow you to shape the future of our products by requesting new controls and products altogether.
- You and other developers can discuss existing product ideas with members of our Product Management team.
The product ideas site allows you to track the progress of your ideas at any time, see how many votes it got, read comments from other developers in the community, and see if someone from the product team has additional questions for you.
Let me know if you have any additional questions or concerns.
Best Regards,
Maya Kirova
Developer Support Engineer II
Infragistics, Inc.
http://ko.infragistics.com/support
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.
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();
Let me know if you have any questions.
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.
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);
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.