본문 바로가기
소프트웨어/3rd Part

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

by 알 수 없는 사용자 2009. 1. 30.
320x100
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.


UltraWinGrid에서 Excel로 내려 받는 방법입니다.
UltraGridExcelExporter1 컨트롤을 이용하면 간단히 처리할 수 있습니다.

소스코드는 외국블로그를 참고하였습니다.

간단히 동작하는 로직을 보면  내려받기 버튼을 클릭합니다.
이벤트가 발생하면 저장위치를 지정하는 파일다이얼로그를 호출합니다.
저장할 위치를 지정하고 파일명을 쓰고 엑셀파일을 IO로 씁니다.
그런후 UltraGridExcelExporter 를 이용해서 그리드를 엑셀로 기록합니다.





Excel로 내려받기 버튼을 클릭합니다. 저장이름을 입력합니다.









엑셀에서 정상적으로 잘 보여집니다.
하지만 포멧도 적용 되지만 mask형식은 적용 안되는 거 같습니다.
현재 정렬을 정상적으로 적용되어 나타난 상태입니다.





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

Please note:
btnDownload is the button on whose click Event the code to CreateExcel file is written.

참조를 하니깐 아래와 같이 선언을 해 주어야 합니다.
using Infragistics.Win.UltraWinGrid.ExcelExport;
using System.IO;


/// <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);
//위와 같이 컨트롤을 넣어도 됩니다. 저는 컨트롤을 붙이기 보다는  아래와 같이 선언을 해서 썼습니다. (동일)
//using Infragistics.Win.UltraWinGrid.ExcelExport;
//UltraGridExcelExporter UltraGridExcelExporter1 = new UltraGridExcelExporter();
//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;
}

댓글