vb.net操作Excel
Excel2007添加引用Microsoft Excel 12.0 library object
'導入命名空間
Imports Microsoft.Office.Interop
Private Sub A1()
'導出Excel
Dim Xls As Excel.Application = New Excel.Application '定义excel应用程序
Dim Xlsbook As Excel.Workbook '定义工作簿
Dim Xlssheet As Excel.Worksheet '定义工作表
Xlsbook = Xls.Application.Workbooks.Add '添加新工作簿
Xlssheet = Xlsbook.Sheets(1) '第1个工作表的控制句柄
Xlssheet.Name = "測試表" '表名
Xls.Caption = "測試工作簿" '工作簿名
Xlssheet.Cells(1, 1).value = "測試列" '單元格值
Xlssheet.Cells.EntireColumn.AutoFit() '自動列寬
Xls.Visible = True '显示excel 程序
Xlssheet.Cells(2, 1).value = "0001" '單元格值
Xlssheet.Cells.NumberFormatLocal = "@" '修改單元格格式
Xlssheet.Cells(3, 1).value = "0001" '單元格值
Xlssheet.Cells(3, 1).Font.Color = RGB(255, 0, 0) '文字顏色
'二维数组
Dim i As Integer
Dim DataArray(100, 2)
For i = 1 To 100
DataArray(i, 0) = "A" & i
DataArray(i, 1) = "B" & i
Next
Xlssheet.Range("A10").Resize(101, 2).Value = DataArray
Xlssheet.Rows(60).delete() '刪除第60行
Xls.DisplayAlerts = False '不显示对话框
Xls.ActiveWorkbook.SaveAs("D:\測試.xls") '儲存檔案
Xls.Workbooks.Close() '關閉檔案
Xls = Nothing '释放EXCEL对象
End Sub