소스는 아래와 같다.
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Ev
entArgs) Handles Button1.Click
Dim xlApp As Microsoft.Office.Interop.Excel.Application
Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook
Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
Dim i As Integer
Dim j As Integer
SaveFileDialog1.Title = "Save File"
SaveFileDialog1.InitialDirectory = "C:\"
SaveFileDialog1.FileName = "test.xlsx"
SaveFileDialog1.Filter = "ExcelFile|*.xlsx"
If SaveFileDialog1.ShowDialog = System.Windows.Forms.DialogResult.OK Then
xlApp = New Microsoft.Office.Interop.Excel.Application
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
For i = 0 To DataGridView1.RowCount
For j = 0 To DataGridView1.ColumnCount - 1
xlWorkSheet.Cells(1, j + 1) = DataGridView1.Columns(j).Name
xlWorkSheet.Cells(i + 2, j + 1) =
DataGridView1.Rows(i).Cells(j).Value.ToString()
Next
Next
xlWorkSheet.SaveAs(SaveFileDialog1.FileName)
xlWorkBook.Close()
xlApp.Quit()
Else
MsgBox("파일저장을 취소했습니다.")
End If
End Sub
End Class
디자인은 다음과 같다.

실행시 화면

'Visual Std 2010' 카테고리의 다른 글
Datagridview 셀 더블 클릭시 해당 값을 불러오는 방법 (0) | 2015.09.14 |
---|---|
VS2010 엑셀데이터를 DataGridView로 로드하기 (Test) (0) | 2015.09.14 |
TextBox 숫자 입력 체크 함수 (0) | 2015.09.14 |
PrintForm 구성요소를 사용한 윈도우 폼 이미지 인쇄 (0) | 2015.09.14 |
Visual Studio 2010 단축키 (0) | 2015.09.14 |