달력

4

« 2024/4 »

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
2012. 8. 22. 10:00

XtraGrid - Export Excel File Open Study/DevExpress2012. 8. 22. 10:00

XtraGrid에서 Excel로 Export하는 경우 Export 직후에 바로 파일을 오픈함으로써 탐색기를 통해서 파일을 열어야 하는 수고를 덜 수 있습니다.
그 방법에 대해서 알아보도록 하겠습니다.

 우선 기존에 파일을 Export했던 방식은 다음과 같습니다.

.
.
.

Dim View As DevExpress.XtraGrid.Views.Grid.GridView = CType(WG1.MainView,DevExpress.XtraGrid.Views.Grid.GridView)

If Not View Is Nothing Then

If dlgSaveExcel.ShowDialog() = DialogResult.OK Then

View.OptionsPrint.ExpandAllDetails = True

View.ExportToXls(dlgSaveExcel.FileName)

MsgBox("Savecomplete.")

End If

End If

.
.
.

우선 GridView 변수를 하나 선언해서 Export하고자 하는 그리드를 변수로 받아옵니다.
그리고 화면에 선언된 저장 대화상자를 통해서 저장경로와 파일명을 선택하고 저장을 눌러주면 ExportToXls 함수에 의해서 Export가 실행되고 완료후에 "Save complete"라는 메시지를 보여줍니다(위의 예는 GridView에 대한 Export 예 입니다).

View.OptionsPrint.ExpandAllDetails = True  

이 옵션은 Master-Detail 구조인 경우 모든 Detail 부분을 펼쳐서 Export한다는 뜻입니다.

이 방법은 Excel Export에는 문제가 없으나 Export가 완료된 후에 파일을 확인하려면 탐색기를 열어서 해당 파일을 찾아서 열거나 엑셀을 실행시켜서 따로 파일을 열어서 봐야 한다는 번거로움이 있습니다.

이제 변환된 방법을 보도록 하겠습니다.
BandedGridView를 가지고 예를 들어보도록 하겠습니다.

.
.
.

Dim View As DevExpress.XtraGrid.Views.BandedGrid.AdvBandedGridView = CType(WG1.MainView, DevExpress.XtraGrid.Views.BandedGrid.AdvBandedGridView)

 

If Not View Is Nothing Then

    If dlgSaveExcel.ShowDialog() = DialogResult.OK Then

Dim currentCursor As Cursor = Cursor.Current

Cursor.Current = Cursors.WaitCursor

 

View.OptionsPrint.ExpandAllDetails = True

View.OptionsPrint.PrintBandHeader = False

View.ExportToXls(dlgSaveExcel.FileName)

'File Open

If MessageBox.Show("Do you want open this file?", "Open", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then

            Dim process As System.Diagnostics.Process = New System.Diagnostics.Process()

            process.StartInfo.FileName = dlgSaveExcel.FileName

            process.StartInfo.Verb = "Open"

            process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal

            process.Start()

End If

 

Cursor.Current = currentCursor

    End If

End If

.
.

몇가지 변화가 있습니다.
우선 Export를 하기 전에 마우스 커서를 변경하고 Export 완료후에 마우스커서를 원래대로 되돌리는 코딩이 있습니다.
그리고 BandedGridView를 사용하면서 추가된 옵션이 있습니다.

View.OptionsPrint.PrintBandHeader =
 False 

이 부분인데 PrintBandHeader 옵션은 밴드명을 출력할지 여부를 결정합니다.
BandedPrintOption에 관해서는 다음 링크를 참고하시면 됩니다.

http://documentation.devexpress.com/#WindowsForms/DevExpressXtraGridViewsBandedGridBandedGridOptionsPrintMembersTopicAll

그리고 강조된 IF문 블럭이 파일을 Open하는 부분입니다.

MessageBox.Show("Do you want open this file?", "Open", MessageBoxButtons.YesNo)

이 구문으로 아래와 같은 창이 뜹니다.



 
기에서 '예'를 선택하면 파일을 바로 오픈해서 볼 수 있습니다. 

Porcess 클래스는 Local System의 Process를 시작하고 중지할 수 있는 클래스 입니다.

process.StartInfo 속성은 프로세스 시작시에 전달할 속성들을 설정합니다.
위에서는 파일명과, Verb(동작), WindowStyle을 설정했습니다.
그리고 프로세스를 시작하면 해당파일을 여는 동작을 수행합니다.
당연히 Excel이 설치되어 있어야 하고 xls 파일의 연결 프로그램이 Excel로 되어있어야 Excel로 파일을 열 수 있습니다.

Process 클래스에 대한 정보는

http://msdn.microsoft.com/ko-kr/library/system.diagnostics.process.aspx#Y0

여기에서 확인하실 수 있습니다.

Process.StartInfo 속성에 대한 정보는

http://msdn.microsoft.com/ko-kr/library/system.diagnostics.process.startinfo.aspx  

여기에서 확인하실 수 있습니다.

여기까지 Export File Open에 대해서 살펴보았습니다. 

:
Posted by 하늘바램