乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > 怎么用<em>VB</em>把txt文本内容提取到<em>excel</em&

怎么用<em>VB</em>把txt文本内容提取到<em>excel</em&

作者:乔山办公网日期:

返回目录:excel表格制作


窗口添加2个按钮,1个文本框,一个Timer( interval=10000 )
工程---引用----Microsoft Excel
输入以下代码:
Dim VBExcel As Object
Private Sub Form_Load()
Set VBExcel = CreateObject("Excel.Application")
End Sub
Private Sub Command1_Click()‘打开,写入并保存
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlsheet As Excel.Worksheet
FileName = "C:\Documents and Settings\Administrator\桌面\a.xls"
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application") '判断e799bee5baa6e79fa5e98193e58685e5aeb9336Excel是否打开
If Err.Number <> 0 Then
Set xlApp = CreateObject("Excel.Application") '创建EXCEL对象
xlApp.Visible = False '设置EXCEL对象不可见
End If
If Dir(FileName) = "" Then '判断文件是否存在
MsgBox FileName & "未找到!", vbOKOnly, "友情提示"
Exit Sub
End If
Set xlBook = xlApp.Workbooks.Open(FileName)
Set xlsheet = xlApp.Worksheets(1)
xlsheet.Activate '激活工作表
xlsheet.Cells(1, 1) = Text1.Text '给单元格1行驶列赋值
xlBook.Save
End Sub
Private Sub Command2_Click()
With VBExcel
.Workbooks.Close'关闭工作表
End With
VBExcel.Quit
End Sub
Private Sub Timer1_Timer()'没10秒打开一次,保存一次,关闭一次i
Command1_Click
Command2_Click
End Sub

应该很容易实现吧。具体要求再明确点,是只导入一个文件,还是把所有的文件都导入
需不需要用户来选择目录 或文件 ?
应该是可以的,看具体情况吧

Option Explicit
'Dim xlApp As Excel.Application '定义EXCEL类
'Dim xlBook As Excel.Workbook '定义工件簿类
'Dim xlsheet As Excel.Worksheet '定义工作表类

Dim xlApp As Object
Dim xlBook As Object
Dim xlsheet As Object
'Public Const sConnect = "ODBC;DSN=LocalServer;UID=sa;PWD=sa;Database=lgz;"

Dim db As Database
Dim qdef As QueryDef
Dim rs As Recordset
Dim qdef1 As QueryDef
Dim rs1 As Recordset
Dim rs2 As Recordset
'Dim sConnect As String

Dim i, temp0, J, SourceFile, DestinationFile, gg1, gg2, gg3, temp1, gg4

Private Sub Command1_Click()

temp1 = "2006技术保障维护维修情况上e799bee5baa6e997aee7ad94e4b893e5b19e361报表10月.xls"
SourceFile = "f:\sql\sblr\sblrbak.xls"
'DestinationFile = "f:\sql\sblr\sblr.xls"
DestinationFile = "f:\sql\sblr\" & Combo4.Text & "年技术保障维护维修情况上报表" & Mid(Combo5.Text, 1, 2) & "月到" & Mid(Combo7.Text, 1, 2) & "月.xls"
FileCopy SourceFile, DestinationFile

Set xlApp = CreateObject("Excel.Application") '创建EXCEL应用类
xlApp.Visible = True '设置EXCEL可见
Set xlBook = xlApp.Workbooks.Open(DestinationFile) '打开EXCEL工作簿
Set xlsheet = xlBook.Worksheets(1) '打开EXCEL工作表
xlsheet.Activate '激活工作表
xlsheet.Cells(1, 1) = Combo4.Text & "年技术保障维护维修情况上报表" & Mid(Combo5.Text, 1, 2) & "月到" & Mid(Combo7.Text, 1, 2) & "月"
Adodc1.Recordset.MoveFirst
gg3 = 0
gg4 = 0
For i = 0 To Adodc1.Recordset.RecordCount - 1
gg1 = Trim(Combo4.Text) & "-" & Trim(Combo5.Text)
gg2 = Trim(Combo6.Text) & "-" & Trim(Combo7.Text)
If Trim(Adodc1.Recordset.Fields(1)) >= gg1 Then
If Trim(Adodc1.Recordset.Fields(1)) <= gg2 Then
gg3 = gg3 + 1
Text1.Text = gg3
For J = 1 To 11
gg4 = gg4 + CInt(Adodc1.Recordset.Fields(9))
temp0 = Trim(Adodc1.Recordset.Fields(J))
xlsheet.Cells(gg3 + 2, J) = temp0
Next J

End If
End If
Adodc1.Recordset.MoveNext
Next i
'xlsheet.Cells(gg3 + 4, 9) = gg4
xlsheet.Cells(gg3 + 4, 7) = "共计:" & gg3 & "件 合计:"
End Sub

Private Sub Command11_Click()

temp1 = "2006技术保障维护维修情况上报表10月.xls"
SourceFile = "f:\sql\sblr\sblrbak.xls"
'DestinationFile = "f:\sql\sblr\sblr.xls"
DestinationFile = "f:\sql\sblr\" & Combo4.Text & "nnn" & Mid(Combo5.Text, 1, 2) & "月.xls"
FileCopy SourceFile, DestinationFile

Set xlApp = CreateObject("Excel.Application") '创建EXCEL应用类
xlApp.Visible = True '设置EXCEL可见
Set xlBook = xlApp.Workbooks.Open(DestinationFile) '打开EXCEL工作簿
Set xlsheet = xlBook.Worksheets(1) '打开EXCEL工作表
xlsheet.Activate '激活工作表

Adodc1.Recordset.MoveFirst
xlsheet.Cells(1, 1) = Combo4.Text & "nnn" & Mid(Combo5.Text, 1, 2) & "月"
gg3 = 0
gg4 = 0
For i = 0 To Adodc1.Recordset.RecordCount - 1
gg1 = Trim(Combo4.Text) & "-" & Trim(Combo5.Text)
gg2 = Trim(Combo6.Text) & "-" & Trim(Combo7.Text)
If Trim(Adodc1.Recordset.Fields(1)) >= gg1 Then
If Trim(Adodc1.Recordset.Fields(1)) <= gg2 Then
gg3 = gg3 + 1
Text1.Text = gg3
For J = 1 To 11
gg4 = gg4 + CInt(Adodc1.Recordset.Fields(9))
temp0 = Trim(Adodc1.Recordset.Fields(J))
xlsheet.Cells(gg3 + 2, J) = temp0
Next J

End If
End If
Adodc1.Recordset.MoveNext
Next i
'xlsheet.Cells(gg3 + 4, 9) = gg4
xlsheet.Cells(gg3 + 4, 7) = "共计:" & gg3 & "件 合计:"
End Sub
Private Sub Command3_Click()
Adodc1.Refresh
Adodc1.Recordset.Sort = (Combo1.Text)
End Sub

相关阅读

  • <em>excel</em> 宏运行时错误<em>1004</em>

  • 乔山办公网excel表格制作
  • 在VBA 菜单栏 → 工具→ VBProject属性→保护勾选 查看时锁定工程,然后在下面输入密码,再次打开即可实现保护。,希望对你有用excelVBA运行时错误1004 此名称已被使用 怎么修改..." src
  • vb6怎么引用excel?

  • 乔山办公网excel表格制作
  • 我感觉这点事,不用写多个函数了一个过程.一次性写完好了.vb6新建工程引用microsoft excelSub OpenExcelAndCopyData(path1 As String, path2 As String) If Len(Dir(path)) = 0 Then Ms
关键词不能为空
极力推荐
  • excel气泡图excel怎么做气泡图

  • 方法/步骤选择数据源点击“插入”百,选择“其他图标”度中的“气泡图”选择图表,右键选择“选择数据”,或者选择左上角的“选择数据”弹出界面中问,选择“删除”,把里面的

ppt怎么做_excel表格制作_office365_word文档_365办公网