I am looking for a way to export Ultrawingrid into HTML and CSV file. After knowing that there is no direct support available to export to html and csv . Is there any indirect way to do exporting or any trick to do that ?
Thanks for your inputs. I have done this in a different way. I am sending code that could help others too.
public void exportToHtml(string title, UltraGrid grid, string filePath) { try { StringBuilder strBldHtml = new StringBuilder(); string strHtml; strHtml = "<Html>" + "\n"; strHtml += "<head>" + "\n"; strHtml += "<title>" + "\n"; strHtml += title; strHtml += "</title>" + "\n"; strHtml += "</head>" + "\n"; strHtml += "<body>" + "\n"; strBldHtml.Append(strHtml); strHtml = createHtmlExportTable(grid, title ); strHtml += "</body>" + "\n"; strHtml += "</html>" + "\n"; strBldHtml.Append(strHtml); strHtml = strBldHtml.ToString(); if (!File.Exists(filePath)) { File.WriteAllText(filePath, strHtml); } else { File.Delete(filePath); File.WriteAllText(filePath, strHtml); } } catch (Exception exception) { MessageBox.Show(exception.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public string createHtmlExportTable(UltraGrid grid, string title) { StringBuilder strHtml = new StringBuilder(title); strHtml.Append("\n"); try { List<int> columnIndex = new List<int>(); UltraGridBand band = grid.DisplayLayout.Bands[0]; for (int start = 0; start < band.Columns.Count; start++) { //get the column on there appearance position in grid foreach (UltraGridColumn column1 in band.Columns) { if (column1.Header.VisiblePosition == start && !column1.Hidden) { columnIndex.Add(column1.Index); break; } } } createHtmlExportTable(grid, grid.Rows, strHtml, band, columnIndex); return strHtml.ToString(); } catch (Exception exception) { MessageBox.Show(exception.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); return strHtml.ToString(); } finally { } }
private void createHtmlExportTable(UltraGrid grid, RowsCollection rows, StringBuilder strHtml, UltraGridBand band, List<int> columnIndex) { bool alternativeRow = true; strHtml.Append("<table width=100% border=\"1\" style=\"background-color:InactiveCaptionText; font-size:small; font-family:Verdana\" > \n"); for (int i = 0; i < rows.Count; i++) { UltraGridRow row = rows[i]; if (row.IsGroupByRow) { if (row.IsExpanded) strHtml.Append("<tr style=\"background-color:White; font-size:small; font-family:Verdana\"> \n <td width=\"4\"> - </td> \n <td>" + row.Description.ToString() + "</td> \n </tr> \n"); else strHtml.Append("<tr style=\"background-color:White; font-size:small; font-family:Verdana\"> \n <td width=\"4\"> + </td> \n <td>" + row.Description.ToString() + "</td> \n </tr> \n"); if (row.IsExpanded) { strHtml.Append("<tr>\n<td> </td>\n<td>"); createHtmlExportTable(grid, row.ChildBands[0].Rows, strHtml, row.ChildBands[0].Band, columnIndex); strHtml.Append("</td>\n</tr> \n"); } } else { string ColHeader; if (i == 0) //check if header row { strHtml.Append("<tr style=\"background-color:#6495ed; font-size:small; font-family:Verdana\"> \n"); for (int columnIndexCnt = 0; columnIndexCnt < columnIndex.Count; columnIndexCnt++) { ColHeader = row.Cells[columnIndex[columnIndexCnt]].Column.Header.Caption.ToString(); strHtml.Append("<td><font color=white><b>" + ColHeader + "</b></font></td>\n"); } strHtml.Append("</tr> \n"); } if (alternativeRow) { strHtml.Append("<tr style=\"background-color:White; font-size:small; font-family:Verdana\"> \n"); alternativeRow = false; } else { strHtml.Append("<tr style=\"background-color:InactiveCaptionText; font-size:small; font-family:Verdana\"> \n"); alternativeRow = true; } for (int columnIndexCnt = 0; columnIndexCnt < columnIndex.Count; columnIndexCnt++) { string strCol = ""; strCol = row.Cells[columnIndex[columnIndexCnt]].Text.ToString(); strHtml.Append("<td>" + strCol + "</td>"); } strHtml.Append("</tr> \n"); } } strHtml.Append("</table> \n"); }
There's no great trick to this. But the grid does have some built-in support for exporting that you might be able to take advantage of. The grid itself has an Export method which takes in an object that implements an interface. The grid will then call methods on the interface to guide you through the process of exporting. It will, fire a method to tell you to begin the export, then gives you each row in order for processing, and then a final method when the export should be complete.
Converting the grid row into a csv or html format is, of course, up to you.