Downloading an UltraGrid to an Excel File, by letting the user choose the download location

UltraGrid is a component from Infragistics, which provide a rich set of features for the Grid Control. And moreover, it can download the entire Grid (along with its content) to an Excel file. It retains the entire formatting, entire Column Positions and much more.

Please note:
btnDownload is the button on whose click Event the code to CreateExcel file is written. /// <summary> /// The Button Event /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnDownload_Click(object sender, System.EventArgs e) { try { //Saving to Excel file. This launches the Save dialog for the user to select the Save Path CreateExcel(FindSavePath()); } catch(Exception ex) { //Handle Exception MessageBox.Show(ex.Message); } finally { //Any cleanup code this.Cursor = Cursors.Default; } } /// <summary> /// Creates an Excel from the UltraGrid and saves it to the user mentioned Save Path /// </summary> /// <param name="myFilepath"></param> private void CreateExcel(String myFilepath ) { try { if (myFilepath!= null) { //You need to Create the ExcelExporter component in the design view this.ultraGridExcelExporter1.Export(this.ultragrid1, myFilepath); MessageBox.Show("Grid data successfully downloaded to "+ myFilepath); } } catch(Exception ex) { throw ex; } }


/// <summary> /// Finding path for saving excel sheet. /// </summary> /// <returns>full path</returns> private String FindSavePath() { Stream myStream; string myFilepath=null; try { SaveFileDialog saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.Filter = "excel files (*.xls)|*.xls"; saveFileDialog1.FilterIndex = 2 ; saveFileDialog1.RestoreDirectory = true ; if(saveFileDialog1.ShowDialog() == DialogResult.OK) { if((myStream = saveFileDialog1.OpenFile()) != null) { myFilepath=saveFileDialog1.FileName; myStream.Close(); } } } catch(Exception ex) { throw ex; } return myFilepath; }
}
By [)ia6l0 iii   Popularity  (1676 Views)