最近需要利用 VB.net 來匯出匯入資料到 Excel ,在這邊簡單的紀錄一下。

 

在 Visual Studio 加入參考

2011-05-22_133039

Import Namespace

Imports Excel = Microsoft.Office.Interop.Excel

 

程式碼:

Dim app As New Excel.Application
Dim book As Excel.Workbook
Dim sheet As Excel.Worksheet
Dim range As Excel.Range
 
app.DisplayAlerts = False
app.Visible = False
 
'在程式開始執行路徑取得 test.xls
book = app.Workbooks.Open(Application.StartupPath & "\test.xls")
sheet = book.Sheets(1)
range = sheet.Cells(1, 1)
 
'讀取顯示 Cell(1,1) 值
MsgBox(range.Value)
 
'寫入 Cell(1,1)
range.Value = "12345"
 
'另存檔案到 c:\test1.xls
book.SaveAs("c:\test1.xls")
book.Close()

iammic 發表在 痞客邦 PIXNET 留言(0) 人氣()