Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
210
Export raw data to Excel
posted

Good day, I'm researching as to how one would export data only to excel? I have not found any examples on how to accomplish this. I understand I can use the UltraGridExcelExporter, but it's appears to export the entire grid view, I simply want to export the row data, no formatting just plain raw data into excel, how can I accomplish this and can you please provide me with a code snippet or two?

Best regards.

Parents
  • 469350
    Offline posted

    Hi Nigel,

    There are basically two approaches you could take here. Which one you choose depends on a number of factors.

    Approach 1 - Remove the formatting from the export layout

    One approach would be to use the UltraGridExcelExporter and try to remove any formatting from the export layout. This may be tricky or it might be relatively simple - it depends on what kind of formatting you have applied to the grid and what needs to be removed. You would have to know all the things you are doing to the grid so you could undo them on the export layout.

    I assume you probably want to keep the data in the same order as it appears on the screen. Can the user re-arrange the grid columns in your application? Can they sort the rows? If so, then this approach is probably a little more complex, because you can't just reset the layout since there are certain things you want to keep (the row and column order) and certain things you don't (the appearances). So you would have to pick and choose.

    There's no way I can give you a sample of this, since as I explained, it depends a lot on what you are doing that needs to be undone.

    Approach 2 - Start from scratch

    Another approach would be the exact opposite. Instead of using the UltraGridExcelExporter and removing what you don't want, you could write the whole export process yourself. What you would do is create an Excel Workbook, then loop through the grid and write the data into it cell by cell.

    If you have a single-band grid, this is very easy. If your grid contains hierarchical data (child bands or OutlookGroupBy), then it becomes more complicated.

    Also, if you are using DataTypes in your grid that Excel does natively support, such as Color or Bitmap, you will have to find a way to deal with those. The UltraGridExcelExporter handles these kinds of things for you, so that's one reason to go with approach #1.

    In the simplest case of a flat grid, the code would look something like this:

                Infragistics.Documents.Excel.Workbook workbook = new Infragistics.Documents.Excel.Workbook(Infragistics.Documents.Excel.WorkbookFormat.Excel2007);
                Infragistics.Documents.Excel.Worksheet worksheet = workbook.Worksheets.Add("My Grid");
                UltraGridBand band = this.ultraGrid1.DisplayLayout.Bands[0];

                int rowIndex = 0;
                int colIndex;
                foreach (UltraGridRow row in this.ultraGrid1.Rows.GetFilteredInNonGroupByRows())
                {
                    colIndex = 0;
                    UltraGridColumn column = band.GetFirstVisibleCol(this.ultraGrid1.DisplayLayout.ColScrollRegions[0], true);

                    while (column != null)
                    {
                        worksheet.Rows[rowIndex].SetCellValue(colIndex, row.GetCellValue(column));

                        column = column.GetRelatedVisibleColumn(VisibleRelation.Next);
                        colIndex++;
                    }

                    rowIndex++;
                }

                string filename = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "My Grid.xlsx");
                workbook.Save(filename);
                Process.Start(filename);

Reply Children
No Data