* VBNET 엑셀 파일 예제...
- 참조 추가
소스 구현을 하기에 앞서 미리 엑셀 파일을 열어 D:\통합 문서.xls 로 샘플로 하나 저장 해 놓았습니다.
샘플로 파일 하나 만들고 위치는 어디에 있던 상관 없습니다.
Form1.vb
Imports Microsoft.Office.Interop.Excel
Imports Microsoft.Office.Interop
Imports System.IO
Public Class Form1
'엑셀 사용을 위한 변수...
Dim ExcelApp As Excel.Application = Nothing
Dim wb As Excel.Workbook = Nothing
Dim ws As Excel.Worksheet = Nothing
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
'Excel Write
'원본 파일이 없다면...
If Not File.Exists("D:\통합 문서.xls") Then
Return
End If
'파일이 있다면 삭제하고...
If File.Exists("C:\Test.xls") Then
File.Delete("C:\Test.xls")
End If
Try
ExcelApp = New Excel.Application()
'wb = ExcelApp.Workbooks.Add("Test") => 엑셀 파일 만들 때 새로운 워크북 추가
'엑셀 파일 오픈...
wb = ExcelApp.Workbooks.Open("D:\통합 문서.xls")
ws = CType(wb.Worksheets("Sheet1"), Excel.Worksheet)
'시트 -> 셀에 데이터 삽입 부분
ws.Cells(1, 1) = "1"
ws.Cells(1, 2) = "12"
ws.Cells(1, 3) = "123"
ws.Cells(1, 4) = "1234"
'폼 화면에 디스플레이...
textBox1.Text += "A1: 1" + System.Environment.NewLine
textBox1.Text += "A2: 12" + System.Environment.NewLine
textBox1.Text += "A3: 123" + System.Environment.NewLine
textBox1.Text += "A4: 1234" + System.Environment.NewLine
textBox1.Text += "===========================" + System.Environment.NewLine
textBox1.Text += "Excel File Write..." + System.Environment.NewLine
textBox1.Text += "===========================" + System.Environment.NewLine
'파일 닫기...
'다른이름으로 저장하기...
wb.SaveAs("C:\Test.xls")
wb.Saved = True
wb.Close(False, Excel.XlFileFormat.xlWorkbookNormal)
ExcelApp.Quit()
Catch ex As Exception
'객체 메모리 해제...
ReleaseObject(ws)
ReleaseObject(wb)
ReleaseObject(ExcelApp)
GC.Collect()
Finally
'객체 메모리 해제...
ReleaseObject(ws)
ReleaseObject(wb)
ReleaseObject(ExcelApp)
GC.Collect()
End Try
End Sub
Private Sub button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.Click
'Excel Read
'파일이 존재 하지 않는다면...
If Not File.Exists("C:\Test.xls") Then
Return
End If
Try
ExcelApp = New Excel.Application()
'엑셀 파일 오픈...
wb = ExcelApp.Workbooks.Open("C:\Test.xls")
ws = CType(wb.Worksheets("Sheet1"), Excel.Worksheet)
'시트 -> 셀 값 얻어 오기...
Dim rg As Excel.Range = CType(ws.Cells(1, 1), Excel.Range)
Dim rg2 As Excel.Range = CType(ws.Cells(1, 2), Excel.Range)
Dim rg3 As Excel.Range = CType(ws.Cells(1, 3), Excel.Range)
Dim rg4 As Excel.Range = CType(ws.Cells(1, 4), Excel.Range)
textBox1.Text += "A1: " + rg.Value2.ToString() + System.Environment.NewLine
textBox1.Text += "A2: " + rg2.Value2.ToString() + System.Environment.NewLine
textBox1.Text += "A3: " + rg3.Value2.ToString() + System.Environment.NewLine
textBox1.Text += "A4: " + rg4.Value2.ToString() + System.Environment.NewLine
textBox1.Text += "===========================" + System.Environment.NewLine
textBox1.Text += "Excel File Read..." + System.Environment.NewLine
textBox1.Text += "===========================" + System.Environment.NewLine
'파일 닫기...
wb.Close(False, Excel.XlFileFormat.xlWorkbookNormal)
ExcelApp.Quit()
Catch ex As Exception
'객체 메모리 해제...
ReleaseObject(ws)
ReleaseObject(wb)
ReleaseObject(ExcelApp)
GC.Collect()
Finally
'객체 메모리 해제...
ReleaseObject(ws)
ReleaseObject(wb)
ReleaseObject(ExcelApp)
GC.Collect()
End Try
End Sub
Private Sub ReleaseObject(ByVal obj As Object)
Try
If Not obj Is Nothing Then
Runtime.InteropServices.Marshal.ReleaseComObject(obj)
obj = Nothing
End If
Catch
obj = Nothing
Finally
GC.Collect()
End Try
End Sub
End Class
* 예제 결과 화면
https://kdsoft-zeros.tistory.com/136
[VBNET] Excel File Print (엑셀 파일 프린트)
* VBNET Excel File Print (엑셀 파일 프린트) 예제... - 인쇄할 엑셀 파일 추가 하기... (Excel File 추가하는 것은 C# 과 VBNET 이 다를게 없으므로 기존 C# 에 작성된 그림을 가져옴) 프로젝트 클릭 -> 마우..
kdsoft-zeros.tistory.com
[C#] Excel File Write & Read 예제...
[C#] Excel File Write & Read 예제...
* 엑셀 파일 쓰고 읽기 예제... 위 그림과 같이 프로젝트에 참조를 추가 해 줍니다. 참조가 추가 되었으면 이제 메인화면을 만들어 봅니다. 소스 구현을 하기에 앞서 미리 엑셀 파일을 열어 D:\통합 문서.xls..
kdsoft-zeros.tistory.com
[VBNET] File Create Delete Read Write Ex
[VBNET] File Create Delete Read Write Ex
* VBNET 파일 예제 Form1.vb Public Class Form1 Dim strCheckFolder As String = "" Dim strFileName As String = "Test.txt" Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventA..
kdsoft-zeros.tistory.com
'VB.NET Programming' 카테고리의 다른 글
[VBNET] 폴더 및 파일, 드라이브 사이즈 (Size) 구하기 (0) | 2019.10.03 |
---|---|
[VBNET] 레지스트리를 이용한 윈도우 시작 시 자동 실행 (0) | 2019.10.01 |
[VBNET] File Create Delete Read Write Ex (0) | 2019.09.27 |
[VBNET] INIFile Create & Read & Write Example (0) | 2019.09.26 |
[VBNET] 레지스트리(Registry) Create & Delete & Read & Write (0) | 2019.09.23 |