vb中的文件操作,有顺序文件,随机文件之分,但是象 word,excel这类文件,属于哪种呢?
-----------------------------
word,excel这类文件,好象用VB的文件操作语句无效,是不是VB只能针对非常简单的文本文件呀?
而且局限性很大,因为每条记录的大小必须是固定的.所以象记事本中写字符串的功能,好象也无法实现,是这样吗?比如向一个文本文件中,写入一篇文章,用vb的文件操作语句也无法实现吧.
因为一个文章并没有记录的概念呀.
-----------------------------
word,excel这类文件,好象用VB的文件操作语句无效,是不是VB只能针对非常简单的文本文件呀?
而且局限性很大,因为每条记录的大小必须是固定的.所以象记事本中写字符串的功能,好象也无法实现,是这样吗?比如向一个文本文件中,写入一篇文章,用vb的文件操作语句也无法实现吧.
因为一个文章并没有记录的概念呀.
word 也一样~~~~~
这是写EXCEL的过程,你研究一下吧:Private Sub writeExcel(Filename1 As String)
'On Error GoTo myErr
Dim FilePath As String, tmp As Byte
Dim excel_app As Object
'建立 Excel 应用程序
Set excel_app = CreateObject("Excel.Application")
'显示Excel应用程序
' excel_app.Visible = True
'添加新工作簿:
excel_app.workbooks.Add
'检测Excel版本
If Val(excel_app.Application.Version) >= 12 Then
FilePath = Filename1 & ".XLSX"
Else
FilePath = Filename1 & ".XLS"
End If
.
.
.
DoEvents
Screen.MousePointer = vbHourglass
'设置第1个工作表为活动工作表:
excel_app.Sheets("sheet1").Select
'设置页面为横向
excel_app.ActiveSheet.PageSetup.Orientation = 2
If (InStr(Filename1, "综合") = 0) And (InStr(Filename1, "统计") = 0) Then '//日报
'设置指定列的宽度(单位:字符个数)及对齐方式
excel_app.ActiveSheet.Columns(1).ColumnWidth = 9
excel_app.ActiveSheet.Columns(2).ColumnWidth = 5
excel_app.ActiveSheet.Columns(3).ColumnWidth = 5
.
.
.
excel_app.ActiveSheet.Columns(14).ColumnWidth = 6
excel_app.ActiveSheet.Columns(15).ColumnWidth = 7
excel_app.ActiveSheet.Columns(16).ColumnWidth = 7
Dim tmpNum As Byte
For tmpNum = 1 To 16
With excel_app.ActiveSheet
'4右对齐,3居中
.Columns(tmpNum).HorizontalAlignment = 3
End With
Next tmpNum
'添加标题
excel_app.ActiveSheet.Range(excel_app.ActiveSheet.Cells(1, 1), excel_app.ActiveSheet.Cells(1, 16)).Merge
excel_app.Cells(1, 1) = Filename1
excel_app.Range("P4:P11").Merge
excel_app.Range("P12:P19").Merge
excel_app.Range("P20:P27").Merge
'设置字体
With excel_app.ActiveSheet.Range("A3:P28").font
.Name = "宋体"
.Size = 8
End With
'设置页面和套表框
With excel_app.ActiveSheet.Range("A3:P28").Borders
.LineStyle = 1
.Weight = 2
End With
excel_app.ActiveSheet.Range("P28").Select
'添加表头
excel_app.Cells(3, 1) = Trim(DataGrid1.Columns(0).Caption)
excel_app.Cells(3, 2) = "视温度"
excel_app.Cells(3, 3) = "视密度"
For tmpNum = 4 To 10
excel_app.Cells(3, tmpNum) = Trim(DataGrid1.Columns(tmpNum - 3).Caption)
Next tmpNum
excel_app.Cells(3, 11) = "ρ20-0.0011"
For tmpNum = 12 To 15
excel_app.Cells(3, tmpNum) = Trim(DataGrid1.Columns(tmpNum - 4).Caption)
Next tmpNum
excel_app.Cells(3, 16) = "小结"
'添加数据表内容
Dim iiRow As Integer
Dim iiCol As Integer
Dim tmpCol As Integer
iiRow = 4: iiCol = 0: tmpCol = 0
Do While iiRow - 4 < DataGrid1.VisibleRows
Do While iiCol <= DataGrid1.Columns.Count - 1
Select Case iiCol
Case 1
tmpCol = 2
Case 8
excel_app.Cells(iiRow, 11) = Val(Text1(6).Text) - 0.0011
tmpCol = 3
End Select
DataGrid1.Row = iiRow - 4
excel_app.Cells(iiRow, 1 + iiCol + tmpCol) = DataGrid1.Columns(iiCol).Value
iiCol = iiCol + 1
DoEvents
Loop
iiCol = 0
tmpCol = 0
iiRow = iiRow + 1
DoEvents
Loop
excel_app.Cells(28, 1) = "合计"
excel_app.Cells(28, 2) = ""
excel_app.Cells(28, 3) = ""
excel_app.Cells(28, 4) = Val(Text1(0).Text)
excel_app.Cells(28, 5) = Val(Text1(1).Text)
.
.
.
excel_app.Cells(28, 15) = Val(Text1(10).Text)
excel_app.Cells(28, 16) = ""
ElseIf InStr(Filename1, "综合") Then '//-----------------综合日报
'设置指定列的宽度(单位:字符个数)及对齐方式
excel_app.ActiveSheet.Columns(1).ColumnWidth = 15
excel_app.ActiveSheet.Columns(2).ColumnWidth = 11
excel_app.ActiveSheet.Columns(3).ColumnWidth = 7
excel_app.ActiveSheet.Columns(4).ColumnWidth = 6
excel_app.ActiveSheet.Columns(5).ColumnWidth = 7
excel_app.ActiveSheet.Columns(6).ColumnWidth = 8
excel_app.ActiveSheet.Columns(7).ColumnWidth = 12
excel_app.ActiveSheet.Columns(8).ColumnWidth = 10
excel_app.ActiveSheet.Columns(9).ColumnWidth = 10
excel_app.ActiveSheet.Columns(10).ColumnWidth = 8
excel_app.ActiveSheet.Columns(11).ColumnWidth = 5
excel_app.ActiveSheet.Columns(12).ColumnWidth = 6
excel_app.ActiveSheet.Columns(13).ColumnWidth = 7
For tmpNum = 1 To 13
With excel_app.ActiveSheet
'4右对齐,3居中
.Columns(tmpNum).HorizontalAlignment = 3
End With
Next tmpNum
'添加标题
excel_app.ActiveSheet.Range(excel_app.ActiveSheet.Cells(1, 1), excel_app.ActiveSheet.Cells(1, 13)).Merge
excel_app.Cells(1, 1) = Filename1
excel_app.Range("A13:M26").Merge
excel_app.ActiveSheet.Cells(13, 1).HorizontalAlignment = -4131
excel_app.ActiveSheet.Cells(13, 1).VerticalAlignment = -4160
excel_app.ActiveSheet.Cells(13, 1) = "备注:"
' excel_app.Range("P12:P19").Merge
' excel_app.Range("P20:P27").Merge
'设置字体
With excel_app.ActiveSheet.Range("A3:M28").font
.Name = "宋体"
.Size = 8
End With
'设置页面和套表框
With excel_app.ActiveSheet.Range("A3:M26").Borders
.LineStyle = 1
.Weight = 2
End With
excel_app.ActiveSheet.Range("M28").Select
'添加表头
For tmpNum = 1 To 13
excel_app.Cells(3, tmpNum) = Trim(DataGrid2.Columns(tmpNum - 1).Caption)
Next tmpNum
'添加数据表内容
iiRow = 4: iiCol = 0: tmpCol = 0
Do While iiRow - 4 < DataGrid2.VisibleRows
Do While iiCol <= DataGrid2.Columns.Count - 1
DataGrid2.Row = iiRow - 4
excel_app.Cells(iiRow, 1 + iiCol + tmpCol) = DataGrid2.Columns(iiCol).Value
iiCol = iiCol + 1
DoEvents
Loop
iiCol = 0
tmpCol = 0
iiRow = iiRow + 1
DoEvents
Loop
'添加表尾
excel_app.Cells(28, 2) = "收集人:"
excel_app.Cells(28, 6) = "审核人:"
excel_app.Cells(28, 9) = "负责人:"
Else '//-----------------------------------------------------统计表
'设置指定列的宽度(单位:字符个数)及对齐方式
excel_app.ActiveSheet.Columns(1).ColumnWidth = 32
excel_app.ActiveSheet.Columns(2).ColumnWidth = 13
excel_app.ActiveSheet.Columns(3).ColumnWidth = 13
excel_app.ActiveSheet.Columns(4).ColumnWidth = 13
excel_app.ActiveSheet.Columns(5).ColumnWidth = 13
excel_app.ActiveSheet.Columns(6).ColumnWidth = 32
For tmpNum = 1 To 6
With excel_app.ActiveSheet
'4右对齐,3居中
.Columns(tmpNum).HorizontalAlignment = 3
End With
Next tmpNum
'添加标题
excel_app.ActiveSheet.Range(excel_app.ActiveSheet.Cells(1, 1), excel_app.ActiveSheet.Cells(1, 6)).Merge
excel_app.Cells(1, 1) = Filename1
' excel_app.Range("A13:M26").Merge
' excel_app.Range("P12:P19").Merge
' excel_app.Range("P20:P27").Merge
'设置字体
With excel_app.ActiveSheet.Range("A3:F12").font
.Name = "宋体"
.Size = 8
End With
'设置页面和套表框
With excel_app.ActiveSheet.Range("A3:F12").Borders
.LineStyle = 1
.Weight = 2
End With
excel_app.ActiveSheet.Range("F13").Select
'添加表头
For tmpNum = 1 To 6
excel_app.Cells(3, tmpNum) = Trim(DataGrid3.Columns(tmpNum - 1).Caption)
Next tmpNum
'添加数据表内容
iiRow = 4: iiCol = 0: tmpCol = 0
Do While iiRow - 4 < DataGrid3.VisibleRows
Do While iiCol <= DataGrid3.Columns.Count - 1
DataGrid3.Row = iiRow - 4
excel_app.Cells(iiRow, 1 + iiCol + tmpCol) = DataGrid3.Columns(iiCol).Value
iiCol = iiCol + 1
DoEvents
Loop
iiCol = 0
tmpCol = 0
iiRow = iiRow + 1
DoEvents
Loop
End If
'工作表另存为:
If Not excel_app.ActiveWorkBook.Saved Then
excel_app.ActiveWorkBook.SaveAs FileName:=FilePath
End If
' Close Excel.
excel_app.Quit
Set excel_app = Nothing
Screen.MousePointer = vbDefault
MsgBox "导出了" & Format$(iiRow - 4) & "条记录", , "导出成功"
Exit Sub
myErr:
If Err.Number = 429 Then
Screen.MousePointer = vbDefault
MsgBox "请先安装EXCEL!", , "导出错误"
Exit Sub
End If
excel_app.DisplayAlerts = False '关闭时不提示保存
excel_app.Quit '关闭EXCEL
excel_app.DisplayAlerts = True '关闭时提示保存
Set excel_app = Nothing
'Me.MousePointer = 0
If tmp <> 7 Then MsgBox " 导出数据到 Excel 时出错! ", , "导出错误"
End Sub回复内容过长!不让我贴全了~~~~
至于VB能不能读入一编文章,就好比你吃西瓜的方式,只要你口够大,一口一个大西瓜的往嘴里填,绝对没问题,只要你塞的进去,别怕VB消化不良!
你试试在网上搜索一下它们的结构吧~~~