본문 바로가기
개발언어/3rd Part

Export UltraWinGrid to Excel (WinGird에서 엑셀로 내려받기)

by 엔돌슨 2009. 1. 30.
반응형



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.





UltraGridExcelExporter1 컨트롤을 이용합니다.

외국블로그를 참고하였습니다.

우선 저장위치를 지정하는 파일다이얼로그를 호출합니다.
저장할 위치를 지정하고 엑셀파일을 IO로 씁니다. 그런후 UltraGridExcelExporter 를 이용해서 그리드를 엑셀로 기록합니다.


저장스샷






Export 된 데이타





외국 포럼에서 설명해둔 코드

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;
}