1.添加引用
添加引用->Com->Microsoft Excel 9.0 Object Library(对应Excel2000)
*Microsoft Excel 11.0 Object Library(对应Excel2003)
2.新建对象
实例化 Dim myExcel as New Excel.Application()
添加工作簿 myExcel.Workbooks.Add()
打开工作簿 myExcel.Workbooks.Open("FileName as String")
添加工作表 myExcel.Worksheets.Add()
单元格的值 myExcel.Worksheets(1).Cells(1, 1).Value
*所有元素集合的下标均是以1开始
3.保存关闭
保存工作簿 myExcel.Workbooks(1).Save() ‘1为指定索引
另存为 myExcel.Workbooks(1).SaveAs("FileName as String")
关闭工作簿 myExcel.Workbooks.close()
对象退出 myExcel.quit()
*务必先关闭工作簿后再退出Excel
4.实用程序
1.导入Excel到自定义数据类型
自定义数据类型
Structure DataStructure Dim HeadString As String Dim Data(,) As String End Structure
导入Excel
Dim TotalData() As DataStructure Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim myExcel As New Excel.Application Dim myWorkSheet1 As Excel.Worksheet Dim RowLength As Integer = 10 ‘读入数据的行数 Dim ColLength As Integer = 10 ‘读入数据的列数 myExcel.Workbooks.Open("C:\Users\temp.xls") ReDim TotalData(myExcel.Worksheets.Count - 1) For c = 1 To TotalData.Length myWorkSheet1 = myExcel.Worksheets(c) Dim tempString(RowLength - 1, ColLength - 1) As String For i = 0 To RowLength - 1 For j = 0 To ColLength - 1 tempString(i, j) = myWorkSheet1.Cells(i + 1, j + 1).value Next Next TotalData(c - 1).Data = tempString TotalData(c - 1).HeadString = myExcel.Workbooks(1).Worksheets(c).name Next myExcel.Workbooks.Close() myExcel.Quit() End Sub
2.导入自定义类型数据到ListView
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim RowLength As Integer = 10 ‘读入数据的行数 Dim ColLength As Integer = 10 ‘读入数据的列数 Dim SelectedIndex As Integer = 0 ‘工作表索引 Dim DataString(RowLength - 1, ColLength - 1) As String DataString = TotalData(SelectedIndex).Data ListView1.Clear() For j = 0 To ColLength - 1 ListView1.Columns.Add(DataString(0, j)) Next For i = 1 To RowLength - 1 ListView1.Items.Add(DataString(i, 0)) For j = 1 To 14 ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(DataString(i, j)) Next Next End Sub
附录:常用命令
1 打开Excel:Dim myExcel As New Excel.Application()
2 添加新的工作簿:myExcel.Workbooks.add()
3 设定第二个工作表为活动工作表: myExcel.worksheets(2).Acivate()
4 打开指定的Excel文件:myExcel.workbooks.open("c:\my.xls")
5 显示Excel窗口:myExcel.visible=True
6 更改Excel的标题栏:myExcel.caption="Caption as String"
7 为Excel的单元格赋值:myExcel.cells(1,4).value=100 此语句使Excel当前工作表的第一行第四列,即D1单元格等于100, 也可以这样写: myExcel.Range("D1").value=100
8 设置指定列的宽度(单位:字符个数):myExcel.ActiveSheet.colums(1).columnwidth=20 设定当前工作表第1列的宽度为20
9 设置指定行的高度(单位:磅):myExcel.ActiveSheet.rows(1).rowHeight=1/0.035 1磅 =0.035厘米 设置第1行的高度为1CM
10 插入分页符:myExcel.Activesheet.rows(20).pagebreak=1 在第20行前插入分页符
11 删除分页符:myExcel.Activesheet.columns(20).pagebreak=0 在第20列前删除分页符
12 指定边框线的宽度:myExcel.Activesheet.range("B3:D3").borders(1).weight=3 其中borders参数指定边框的位置: 1:左 2:右 3:顶 4:底 5:斜\ 6:斜/
13 指定边框线条的类型:myExcel.Activesheet.range("B1:D3").borders(2).linestyle=1 此语句将当前工作表的B1:D3单元格的右边框设置为实线 linestyle参数: 1:细实线 2:细虚线 3:点虚线 4:双细实线
14 设置页脚:myExcel.activesheet.pagesetup.centerfooter="第&p页" 注意:设置页眉页脚时要保证计算机上装有打印机,否则出错!
15 设置页眉:myExcel.activesheet.pagesetup.centerfooter="第&p页"
16 设置页眉到顶断距离为2cm:myExcel.Activesheet.pagesetup.Headermargin=2/0.035
17 设置页脚到底端距离为2cm:myExcel.Activesheet.pagesetup.Footermargin=2/0.035
18 设置顶边边距为2cm:myExcel.Activesheet.pagesetup.topmargin=2/0.035
19 设置底边边距为2cm:myExcel.Activesheet.pagesetup.Bottommargin=2/0.035
20 设置左边边距为2cm:myExcel.Activesheet.pagesetup.Leftmargin=2/0.035
21 设置右边边距为2cm:myExcel.Activesheet.pagesetup.Rightmargin=2/0.035
22 设置页面水平居中:myExcel.activesheet.pagesetup.CenterHorizontally=true
23 设置页面垂直居中:myExcel.activesheet.pagesetup.Centervertically=true
24 设置页面纸张大小(1,窄行 8.5*11 ;39 ,宽行 14*11):myExcel.activesheet.pagesetup.papersize=1
25 打印单元格网格线:myExcel.activesheet.pagesetup.PrintGridlines=true
26 复制整个工作表:myExcel.activesheet.Usedrange.Copy
27 复制指定区域:myExcel.activesheet.range("a1:b5").Copy
28 粘贴:myExcel.worksheets("sheet2").range("A1").PasteSpecial
29 在第2行前插入一行:myExcel.activesheet.rows(2).Insert
30 在第2列前插入一列:myExcel.Activesheet.Columns(2).Insert
31 合并C4:D4单元格:myExcel.Activesheet.Range("C4:D4").Merge()
32 自动调整第2列列宽: myExcel.activesheet.Columns(2).AutoFit
33 设置字体:myExcel.Activesheet.cells(2,1).font.name="黑体"
34 设置字体大小:myExcel.Activesheet.cells(2,1).font.size=25
35 设置字体为斜体:myExcel.Activesheet.cells(2,1).font.Italic=true
36 设置字体为粗体:myExcel.Activesheet.cells(2,1).font.Bold=true
37 清除单元格内容:myExcel.activesheet.cells(2,1).ClearContents
38 打印预览工作表:myExcel.Activesheet.PrintPreview
39 打印工作表:myExcel.Activesheet.Printout
40 工作表另存为:myExcel.ActiveWorkbook.saveas("C:\book2.xls")
41 放弃存盘:myExcel.ActiveWorkbook.saved=false
42 关闭工作簿: myExcel.Workbooks.close
43 退出 Excel: myExcel.quit