vb给excel添加工作表
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vb给excel添加工作表相关的知识,希望对你有一定的参考价值。
Set xlApp = CreateObject("Excel.Application") '创建EXCEL对象
xlApp.Visible = True
xlApp.Caption = "abc"
Set xlBook = xlApp.Workbooks.Add
xlBook.Worksheets(1).Name = "BOM"
请问怎么添加第二张工作表?
Excel中的VB语言叫做VBA,在VBE中使用代码新建即可。
Excel版本参考:2010
演示:新建一个表名为test的工作表。
1、ALT+F11,进入VBE窗口。
2、右击工作表标签-插入-模块。
3、输入代码:
Sub test()
Worksheets.Add(after:=Sheets(Sheets.Count)).Name = "test"
End Sub
4、按下F5执行,查看工作表,新建的test表 已新建好。
参考技术A Public Sub asasasa()Set xlApp = CreateObject("Excel.Application") '创建EXCEL对象
xlApp.Visible = True
xlApp.Caption = "abc"
Set xlbook = xlApp.Workbooks.Add
xlbook.Worksheets(1).Name = "BOM"
xlbook.Worksheets(2).Name = "BOM111111"
xlbook.Worksheets.Add(before:=xlbook.Worksheets("sheet3")).Name = "asasas"
End Sub 参考技术B 可以用:Set xlSheet=xlBook.worksheets.add
然后可以用xlsheet来操纵新加的这张表了。本回答被提问者采纳
vb.net 教程 10-2 Excel操作4 比较完整的工作表操作
版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。
前面几篇教程已经讲述了如何载入excel工作表中的数据,本节内容将讲述如何实现工作表数据的增、改、删,并实现对工作表操作比较完整的代码。
数据的增加和删除都可以使用 sheet.Cells(Row, Column).Value = 值 来实现。
数据的删除可以使用 Wsheet.Rows(行号).Delete 来实现对整行的删除,也可以使用 sheet.Cells(Row, Column).Value = "" 来实现。
设计界面如下:
实现新增的代码:
'添加新的数据到Excel
Private Sub addToExcel(ByVal id As String, ByVal computerCompany As String,
ByVal computerType As String, ByVal computerPrice As String,
ByVal computerCount As String)
maxRow += 1
Wsheet.Cells(maxRow, 1).Value = id
Wsheet.Cells(maxRow, 2).Value = computerCompany
Wsheet.Cells(maxRow, 3).Value = computerType
Wsheet.Cells(maxRow, 4).Value = computerPrice
Wsheet.Cells(maxRow, 5).Value = computerCount
End Sub
由于是新增,所以在现有最大行数的基础上的下一行写入数据。
实现修改的代码:
'修改Excel中的数据
Private Sub editToExcel(ByVal id As String, ByVal computerCompany As String,
ByVal computerType As String, ByVal computerPrice As String,
ByVal computerCount As String)
Dim currentExcelRow As Integer = lvCurrentRow + 1 '对应Excel当前正在编辑的行
Wsheet.Cells(currentExcelRow, 1).Value = id
Wsheet.Cells(currentExcelRow, 2).Value = computerCompany
Wsheet.Cells(currentExcelRow, 3).Value = computerType
Wsheet.Cells(currentExcelRow, 4).Value = computerPrice
Wsheet.Cells(currentExcelRow, 5).Value = computerCount
End Sub
由于是修改,所以在现有选中行的基础上进行修改。
实现删除的代码:
'删除Excel中的数据
Private Sub delToExcel(ByVal rowNumber As Integer)
rowNumber = rowNumber + 2
Wsheet.Rows(rowNumber).Delete
maxRow -= 1
End Sub
需要注意的是:这里是按照 Rows(行集合) 中的数据删除。因此,如果listview中删除第一行,即rowNumber=0,但是,对应的Excel中为Rows(2),请读者自己想一下为什么是2。
最后,请不要忘了保存数据:
Wbook.Save()
全部代码如下:
Imports System.ComponentModel
Imports Microsoft.Office.Interop
Public Class Form4
Private filename As String
Dim xls As Excel.Application
Dim Wbook As Excel.Workbook
Dim Wsheet As Excel.Worksheet '要编辑的工作表
Dim currentState As Integer '0:删除现有数据;1:增加新的数据;2:编辑现有数据
Dim lvCurrentRow As Integer '用于记录当前正在编辑的行号
Dim maxColumn As Integer '记录Excel中工作表的最大有效列数
Dim maxRow As Integer '记录Excel中工作表的最大有效行数
Dim isOperate As Boolean '添加、修改、删除操作了均为True;未操作或者操作并保存了则为False
Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Width = 530
isOperate = False
filename = Application.StartupPath & "\\例子.xls"
xls = New Excel.Application
'xls.Visible = True
With lvExcel
.GridLines = True '显示表格线
.FullRowSelect = True '选择整行数据
.MultiSelect = False '只允许选择一行数据
.View = View.Details
End With
With tsslInfo
.AutoSize = False
.Width = 220
.TextAlign = ContentAlignment.MiddleLeft
.Text = ""
End With
With tInfo
.Enabled = False
.Interval = 2000
End With
End Sub
'载入Excel中的数据
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnLoad.Click
Wbook = xls.Workbooks.Open(filename)
Wsheet = Wbook.Worksheets(1) '或者工作表名("Sheet1")
lvExcel.Items.Clear()
'读入第一行数据作为column
If Wsheet.Rows.Count > 1 Then
lvExcel.Columns.Clear()
maxColumn = Wsheet.UsedRange.Columns.Count
maxRow = Wsheet.UsedRange.Rows.Count
For i As Integer = 1 To maxColumn
If IsNothing(Wsheet.Cells(1, i).Value) Then
'无数据
Else
lvExcel.Columns.Add(Wsheet.Cells(1, i).Value)
End If
Next
For j As Integer = 2 To maxRow
Dim Lv As New ListViewItem()
Dim LvText As String
If IsNothing(Wsheet.Cells(j, 1).Value) Then
LvText = ""
Else
LvText = Wsheet.Cells(j, 1).Value
End If
Lv.Text = LvText
Dim subLvText As String
For k As Integer = 2 To maxColumn
If IsNothing(Wsheet.Cells(j, k).Value) Then
subLvText = ""
Else
subLvText = Wsheet.Cells(j, k).Value
End If
Lv.SubItems.Add(subLvText)
Next
lvExcel.Items.Add(Lv)
Next
End If
Call showInfo("载入数据完毕")
End Sub
'另存为
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Wbook.Save()
isOperate = False
Call showInfo("保存数据完毕")
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
If isOperate = True Then
If MessageBox.Show("数据已经变更但是没有保存,是否继续退出?",
"尚未保存数据表",
MessageBoxButtons.OKCancel,
MessageBoxIcon.Warning) =
DialogResult.Cancel Then
Exit Sub
End If
End If
xls.Quit()
Application.Exit()
End Sub
Private Sub Form4_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
xls.Quit()
End Sub
'修改数据
Private Sub btnEdit_Click(sender As Object, e As EventArgs) Handles btnEdit.Click
If lvExcel.SelectedItems.Count < 1 Then
MessageBox.Show("尚未选择要修改的数据")
Exit Sub
End If
currentState = 2
Dim lvItem As ListViewItem = lvExcel.SelectedItems(0)
txtIndex.Text = lvItem.Text
txtCompany.Text = lvItem.SubItems(1).Text
txtType.Text = lvItem.SubItems(2).Text
txtPrice.Text = lvItem.SubItems(3).Text
txtCount.Text = lvItem.SubItems(4).Text
'记录正在编辑的行号
lvCurrentRow = lvExcel.SelectedItems(0).Index
Me.Width = 750
End Sub
'增加数据
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
currentState = 1
txtIndex.Text = ""
txtCompany.Text = ""
txtType.Text = ""
txtPrice.Text = ""
txtCount.Text = ""
Me.Width = 750
End Sub
'删除一行数据
Private Sub btnDel_Click(sender As Object, e As EventArgs) Handles btnDel.Click
If lvExcel.SelectedItems.Count < 1 Then
MessageBox.Show("尚未选择要删除的数据")
Exit Sub
End If
currentState = 0
'记录需要删除的行号
lvCurrentRow = lvExcel.SelectedItems(0).Index
Call delToLv(lvCurrentRow)
Call delToExcel(lvCurrentRow)
isOperate = True
End Sub
Private Sub btnOk_Click(sender As Object, e As EventArgs) Handles btnOk.Click
Dim id As String = txtIndex.Text.Trim
Dim computerCompany As String = txtCompany.Text.Trim
Dim computerType As String = txtType.Text.Trim
Dim computerPrice As String = txtPrice.Text.Trim
Dim computerCount As String = txtCount.Text.Trim
If id = "" OrElse computerCompany = "" OrElse computerType = "" _
OrElse computerPrice = "" OrElse computerCount = "" Then
MessageBox.Show("没有填写完整")
End If
Select Case currentState
Case 1 '新增
For Each lvId As ListViewItem In lvExcel.Items
If lvId.Text = id Then
If MessageBox.Show("已经存在这个序号了,是否继续保存。", "重复的序号", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) = DialogResult.Cancel Then
Exit Sub
End If
End If
Next
Call addToLv(id, computerCompany, computerType, computerPrice, computerCount)
Call addToExcel(id, computerCompany, computerType, computerPrice, computerCount)
Case 2 '编辑
Call editToLv(id, computerCompany, computerType, computerPrice, computerCount)
Call editToExcel(id, computerCompany, computerType, computerPrice, computerCount)
Case Else '其它
'不处理
End Select
isOperate = True
Me.Width = 530
End Sub
'添加新的数据到Listview
Private Sub addToLv(ByVal id As String, ByVal computerCompany As String,
ByVal computerType As String, ByVal computerPrice As String,
ByVal computerCount As String)
Dim newLv As New ListViewItem()
newLv.Text = id
newLv.SubItems.Add(computerCompany)
newLv.SubItems.Add(computerType)
newLv.SubItems.Add(computerPrice)
newLv.SubItems.Add(computerCount)
lvExcel.Items.Add(newLv)
End Sub
'添加新的数据到Excel
Private Sub addToExcel(ByVal id As String, ByVal computerCompany As String,
ByVal computerType As String, ByVal computerPrice As String,
ByVal computerCount As String)
maxRow += 1
Wsheet.Cells(maxRow, 1).Value = id
Wsheet.Cells(maxRow, 2).Value = computerCompany
Wsheet.Cells(maxRow, 3).Value = computerType
Wsheet.Cells(maxRow, 4).Value = computerPrice
Wsheet.Cells(maxRow, 5).Value = computerCount
Call showInfo("添加数据完毕")
End Sub
'修改Listview中的数据
Private Sub editToLv(ByVal id As String, ByVal computerCompany As String,
ByVal computerType As String, ByVal computerPrice As String,
ByVal computerCount As String)
Dim editLv As ListViewItem = lvExcel.Items(lvCurrentRow)
editLv.Text = id
editLv.SubItems(1).Text = computerCompany
editLv.SubItems(2).Text = computerType
editLv.SubItems(3).Text = computerPrice
editLv.SubItems(4).Text = computerCount
End Sub
'修改Excel中的数据
Private Sub editToExcel(ByVal id As String, ByVal computerCompany As String,
ByVal computerType As String, ByVal computerPrice As String,
ByVal computerCount As String)
Dim currentExcelRow As Integer = lvCurrentRow + 1 '对应Excel当前正在编辑的行
Wsheet.Cells(currentExcelRow, 1).Value = id
Wsheet.Cells(currentExcelRow, 2).Value = computerCompany
Wsheet.Cells(currentExcelRow, 3).Value = computerType
Wsheet.Cells(currentExcelRow, 4).Value = computerPrice
Wsheet.Cells(currentExcelRow, 5).Value = computerCount
Call showInfo("修改数据完毕")
End Sub
'修改Listview中的数据
Private Sub delToLv(ByVal rowNumber As Integer)
lvExcel.Items.RemoveAt(rowNumber)
End Sub
'删除Excel中的数据
Private Sub delToExcel(ByVal rowNumber As Integer)
'对应Excel当前需要删除的行
'注意:这里是按照 Rows(行集合) 中的数据删除
'因此,如果listview中删除第一行,即rowNumber=0,
'但是,对应的Excel中为Rows(2)
rowNumber = rowNumber + 2
Wsheet.Rows(rowNumber).Delete
maxRow -= 1
Call showInfo("删除数据完毕")
End Sub
'在状态栏显示操作信息
Private Sub showInfo(ByVal info As String)
tsslInfo.Text = info
tInfo.Enabled = True
End Sub
Private Sub tInfo_Tick(sender As Object, e As EventArgs) Handles tInfo.Tick
tsslInfo.Text = ""
tInfo.Enabled = False
End Sub
Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
Me.Width = 530
End Sub
End Class
运行时如下图:
载入数据:
添加数据:
修改数据:
删除数据:
由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供参考。
学习更多vb.net知识,请参看vb.net 教程 目录
开发者涨薪指南 48位大咖的思考法则、工作方式、逻辑体系
以上是关于vb给excel添加工作表的主要内容,如果未能解决你的问题,请参考以下文章