vb文本框写入D盘已有的EXCEL文件中
在D盘中已存有一个名称为123.xls的文件,form1中有一个文本框,将文本框的内容(数字)写入到EXCEL的第一列中,按一下按钮保存一次,在操作的过称中EXCEL不可...
在D盘中已存有一个名称为123.xls的文件,form1中有一个文本框,将文本框的内容(数字)写入到EXCEL的第一列中,按一下按钮保存一次,在操作的过称中EXCEL不可见,直接保存(不提示)后关闭EXCEL,下次再打开程序写入的时候为追加写入(不覆盖上次写入的内容)
我就这点分了,全给了
太感谢wanggang519999了,问题解决了 展开
我就这点分了,全给了
太感谢wanggang519999了,问题解决了 展开
3个回答
展开全部
特别注意:先在程序页面的最顶上加:
Dim xlApp As Excel.Application '定义EXCEL类
Dim xlBook As Excel.Workbook '定义工件簿类
Dim xlsheet As Excel.Worksheet '定义工作表类
再在按钮下写:
If Dir("F:\zyn\vb\vb graduate\bbbb.bz") = "" Then '判断EXCEL是否打开
Set xlApp = CreateObject("Excel.Application") '创建EXCEL应用类
xlApp.Visible = True '设置EXCEL可见
Set xlBook = xlApp.Workbooks.Open("F:\bbb.xls") '打开EXCEL工作簿
Set xlsheet = xlBook.Worksheets(1) '打开EXCEL工作表
xlsheet.Activate '激活工作表
xlsheet.Cells(1, 1) = "编号 "
xlsheet.Cells(1, 2) = "姓名 "
xlsheet.Cells(1, 3) = "性别 "
xlsheet.Cells(1, 4) = "出生日期 "
xlsheet.Cells(1, 5) = "所在单位 "
xlsheet.Cells(1, 6) = "籍贯 "
xlsheet.Cells(1, 7) = "职务 "
xlsheet.Cells(1, 8) = "职称 "
xlsheet.Cells(1, 9) = "工龄 "
xlsheet.Cells(1, 10) = "家庭住址 "
xlsheet.Cells(1, 11) = "联系方式 "
xlsheet.Cells(1, 12) = "备注"
For i = 2 To inta
xlsheet.Cells(i, 1) = myflexgrid.TextMatrix(i - 1, 0) '给单元格1行驶列赋值
xlsheet.Cells(i, 2) = myflexgrid.TextMatrix(i - 1, 1)
xlsheet.Cells(i, 3) = myflexgrid.TextMatrix(i - 1, 2)
xlsheet.Cells(i, 4) = myflexgrid.TextMatrix(i - 1, 3)
xlsheet.Cells(i, 5) = myflexgrid.TextMatrix(i - 1, 4)
xlsheet.Cells(i, 6) = myflexgrid.TextMatrix(i - 1, 5)
xlsheet.Cells(i, 7) = myflexgrid.TextMatrix(i - 1, 6)
xlsheet.Cells(i, 8) = myflexgrid.TextMatrix(i - 1, 7)
xlsheet.Cells(i, 9) = myflexgrid.TextMatrix(i - 1, 8)
xlsheet.Cells(i, 10) = myflexgrid.TextMatrix(i - 1, 9)
xlsheet.Cells(i, 11) = myflexgrid.TextMatrix(i - 1, 10)
xlsheet.Cells(i, 12) = myflexgrid.TextMatrix(i - 1, 11)
Next i
xlBook.RunAutoMacros (xlAutoOpen) '运行EXCEL中的启动宏
Else
MsgBox ("EXCEL已打开")
End If
Dim xlApp As Excel.Application '定义EXCEL类
Dim xlBook As Excel.Workbook '定义工件簿类
Dim xlsheet As Excel.Worksheet '定义工作表类
再在按钮下写:
If Dir("F:\zyn\vb\vb graduate\bbbb.bz") = "" Then '判断EXCEL是否打开
Set xlApp = CreateObject("Excel.Application") '创建EXCEL应用类
xlApp.Visible = True '设置EXCEL可见
Set xlBook = xlApp.Workbooks.Open("F:\bbb.xls") '打开EXCEL工作簿
Set xlsheet = xlBook.Worksheets(1) '打开EXCEL工作表
xlsheet.Activate '激活工作表
xlsheet.Cells(1, 1) = "编号 "
xlsheet.Cells(1, 2) = "姓名 "
xlsheet.Cells(1, 3) = "性别 "
xlsheet.Cells(1, 4) = "出生日期 "
xlsheet.Cells(1, 5) = "所在单位 "
xlsheet.Cells(1, 6) = "籍贯 "
xlsheet.Cells(1, 7) = "职务 "
xlsheet.Cells(1, 8) = "职称 "
xlsheet.Cells(1, 9) = "工龄 "
xlsheet.Cells(1, 10) = "家庭住址 "
xlsheet.Cells(1, 11) = "联系方式 "
xlsheet.Cells(1, 12) = "备注"
For i = 2 To inta
xlsheet.Cells(i, 1) = myflexgrid.TextMatrix(i - 1, 0) '给单元格1行驶列赋值
xlsheet.Cells(i, 2) = myflexgrid.TextMatrix(i - 1, 1)
xlsheet.Cells(i, 3) = myflexgrid.TextMatrix(i - 1, 2)
xlsheet.Cells(i, 4) = myflexgrid.TextMatrix(i - 1, 3)
xlsheet.Cells(i, 5) = myflexgrid.TextMatrix(i - 1, 4)
xlsheet.Cells(i, 6) = myflexgrid.TextMatrix(i - 1, 5)
xlsheet.Cells(i, 7) = myflexgrid.TextMatrix(i - 1, 6)
xlsheet.Cells(i, 8) = myflexgrid.TextMatrix(i - 1, 7)
xlsheet.Cells(i, 9) = myflexgrid.TextMatrix(i - 1, 8)
xlsheet.Cells(i, 10) = myflexgrid.TextMatrix(i - 1, 9)
xlsheet.Cells(i, 11) = myflexgrid.TextMatrix(i - 1, 10)
xlsheet.Cells(i, 12) = myflexgrid.TextMatrix(i - 1, 11)
Next i
xlBook.RunAutoMacros (xlAutoOpen) '运行EXCEL中的启动宏
Else
MsgBox ("EXCEL已打开")
End If
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Private Sub Command1_Click()
If Not IsNumeric(Text1.Text) Then
MsgBox "请输入数字。"
Text1.SetFocus
Exit Sub
End If
Dim Row As Long
Dim ExlApp As Object
Dim ExlBook As Object
Dim ExlSheet As Object
Set ExlApp = CreateObject("Excel.Application")
Set ExlBook = ExlApp.WorkBooks.Open("D:\123.xls")
Set ExlSheet = ExlBook.WorkSheets(1)
Row = 1
Do Until ExlSheet.Cells(Row, 1).Text = ""
Row = Row + 1
Loop
ExlSheet.Cells(Row, 1).Value = Val(Text1.Text)
ExlApp.DisplayAlerts = False
ExlApp.Save
ExlApp.Quit
Set ExlSheet = Nothing
Set ExlBook = Nothing
Set ExlApp = Nothing
MsgBox "保存成功"
End Sub
If Not IsNumeric(Text1.Text) Then
MsgBox "请输入数字。"
Text1.SetFocus
Exit Sub
End If
Dim Row As Long
Dim ExlApp As Object
Dim ExlBook As Object
Dim ExlSheet As Object
Set ExlApp = CreateObject("Excel.Application")
Set ExlBook = ExlApp.WorkBooks.Open("D:\123.xls")
Set ExlSheet = ExlBook.WorkSheets(1)
Row = 1
Do Until ExlSheet.Cells(Row, 1).Text = ""
Row = Row + 1
Loop
ExlSheet.Cells(Row, 1).Value = Val(Text1.Text)
ExlApp.DisplayAlerts = False
ExlApp.Save
ExlApp.Quit
Set ExlSheet = Nothing
Set ExlBook = Nothing
Set ExlApp = Nothing
MsgBox "保存成功"
End Sub
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
工程-引用-Microsoft Excel 11.0 Object Library
下面给个简单的例子
Private Sub Command1_Click()
Dim xlExcel As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim AppExcel As Object
Set xlExcel = CreateObject("Excel.Application")
xlExcel.Workbooks.Open "C:\123.xls"
Set xlBook = xlExcel.Workbooks("123.xls")
Set xlSheet = xlBook.Worksheets(1)
Text1.Text = xlSheet.Cells(1, 1).Value
Text1.Text = Text1.Text & "ADD"
xlSheet.Cells(1, 1).Value = Text1.Text
Application.DisplayAlerts = False
xlBook.Close SaveChanges:=True
Application.DisplayAlerts = True
End Sub
下面给个简单的例子
Private Sub Command1_Click()
Dim xlExcel As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim AppExcel As Object
Set xlExcel = CreateObject("Excel.Application")
xlExcel.Workbooks.Open "C:\123.xls"
Set xlBook = xlExcel.Workbooks("123.xls")
Set xlSheet = xlBook.Worksheets(1)
Text1.Text = xlSheet.Cells(1, 1).Value
Text1.Text = Text1.Text & "ADD"
xlSheet.Cells(1, 1).Value = Text1.Text
Application.DisplayAlerts = False
xlBook.Close SaveChanges:=True
Application.DisplayAlerts = True
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询