VB VB 에서 Exel 읽고 쓰기
페이지 정보
작성자
본문
Excel 파일의 내용을 Grid 컨트롤로 읽고,
Grid의 내용을 Excel 파일로 저장하는 소스입니다.
먼저 VB 메뉴 프로젝트 --> 참조 에서
Microsoft Excel Object 를 포함시키세요...
'------------------------------------------------------------------------
'파일 읽기
'------------------------------------------------------------------------
Private Sub ReadFile(FileName$)
Dim exl As Excel.Application
Dim sheet As Excel.Worksheet
Dim Data(10)
Dim i%
If Dir(FileName) = "" Then Exit Sub
Set exl = New Excel.Application
exl.Workbooks.Open FileName
Set sheet = exl.Workbooks(1).Sheets(1)
With grd
For i = 1 To .Rows - 1
.TextMatrix(i,1) = sheet.Cells(i + 1, 1)
Next i
end With
exl.Workbooks(1).Close
Set sheet = Nothing
Set exl = Nothing
End Sub
'------------------------------------------------------------------------
'파일 저장
'------------------------------------------------------------------------
Private Sub SaveExcel(FileName As String, grd As VSFlexGrid)
On Error Resume Next
Dim exl As Excel.Application
Dim sheet As Excel.Worksheet
Dim i&, j%
Screen.MousePointer = 11
Set exl = New Excel.Application
exl.Workbooks.Add 'Open .Workbooks.Open FileName
Set sheet = exl.Workbooks(1).Sheets(1)
'sheet.Cells.Select
'exl.Selection.NumberFormatLocal = "@"
sheet.Columns(1).Select
exl.Selection.NumberFormatLocal = "0_ "
sheet.Columns(2).Select
exl.Selection.NumberFormatLocal = "@"
sheet.Columns(3).Select
exl.Selection.NumberFormatLocal = "@"
sheet.Columns(4).Select
exl.Selection.NumberFormatLocal = "0.0000_ "
sheet.Columns(5).Select
exl.Selection.NumberFormatLocal = "0.0000_ "
sheet.Columns(6).Select
exl.Selection.NumberFormatLocal = "0.0000_ "
exl.Range(sheet.Cells(1, 1), sheet.Cells(1, grd.Cols)).Select
With exl.Selection.Interior
.ColorIndex = 15
.Pattern = xlSolid
End With
exl.Selection.NumberFormatLocal = "@"
With grd
For i = 0 To .Rows - 1
For j = 0 To .Cols - 1
sheet.Cells(i + 1, j + 1) = .TextMatrix(i, j)
'.Cell(flexcpChecked, i, j) = IIf(sheet.Cells(i + 1, j + 1) = "1", flexChecked, flexUnchecked)
Next j
Next i
End With
Screen.MousePointer = 0
exl.Workbooks(1).SaveAs FileName
exl.Workbooks(1).Close
Set sheet = Nothing
Set exl = Nothing
End Sub
[이 게시물은 오원장님에 의해 2013-02-28 16:42:35 오피스팁에서 이동 됨]댓글목록
등록된 댓글이 없습니다.