如何将下面的txt抓换成excel,跪求具体代码,谢谢。
0004|72280|张三|abc0005|72282|张三|abc
0006|72283|张三|123
0004|72281|张三|232
0007|72284|张三|123
0008|72280|张三|123
--------------------------------------
自己琢磨了几年也没有搞定Private Sub Command2_Click()
Dim h() As String, L() As String, i As Integer, j As Integer
Dim SaveFile, linenext As String
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Set xlApp = CreateObject("Excel.Application") '创建EXCEL对象
Set xlBook = xlApp.Workbooks.Add '打开已经存在的EXCEL工件簿文件
xlApp.Visible = True '设置EXCEL对象可见(或不可见)
Set xlSheet = xlBook.Worksheets("Sheet1") '设置活动工作表
Open "e:\1.txt" For Input As #1
Do Until EOF(1)
Line Input #1, linenext
j = j + 1 'j表示多少行
If linenext <> "" Then
i = i + 1
L = Split(linenext, "|")
For i = 0 To UBound(L)
L = Split(linenext, "|")
For j = 0 To UBound(L)
xlSheet.Cells(i + 1, j + 1) = L(j) '给单元格(row,col)赋值
Next
Next
End If
Loop
SaveFile = "E:\1.xls" '你可以在此修改保存的文件名称
If Dir(SaveFile) <> "" Then Kill SaveFile
xlBook.SaveAs filename:=SaveFile '保存工作表,结束时一定别忘了保存
xlBook.Close (True) '关闭工作簿 这里的True表示退出时保存修改
xlApp.Quit '结束EXCEL对象
Set xlApp = Nothing '释放xlApp对象
MsgBox "文件 本帖最后由 bcrun 于 2011-11-30 21:58:50 编辑 更简单,把“|”替换为制表符“chr(9)”,直接把文件另存为1.xls,即可 没看清,跟1楼的思路一样…… 楼主的思路就很好的啊~~~~,不知哪里有什么问题呢~~~~ EXCEL 录宏
文件,打开,选择文本文件,选该文件,会弹出窗口,仔细看,有步是设置分隔符的,
做完后,看宏。代码移值到你的程序里。 宏录制好像不行,你们看看,我录制的太简答了。
Sub Macro1()
'
' Macro1 Macro
'
'
ChDir "C:\Users\Administrator\Desktop"
Workbooks.OpenText Filename:= _
"C:\Users\Administrator\Desktop\KH3210110180042011110902.txt", Origin:=936, _
StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, Comma:=False _
, Space:=False, Other:=True, OtherChar:="|", FieldInfo:=Array(Array(1, 1 _
), Array(2, 1), Array(3, 1), Array(4, 2), Array(5, 1)), TrailingMinusNumbers:=True
ActiveWorkbook.SaveAs Filename:= _
"C:\Users\Administrator\Desktop\KH3210110180042011110902.xls", FileFormat:= _
xlExcel8, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
, CreateBackup:=False
End Sub
Option Explicit
Private Sub Command1_Click()
Dim bArr() As Byte, str1 As String
Open "c:\1.txt" For Binary As #1
ReDim bArr(LOF(1) - 1)
Get #1, , bArr
str1 = StrConv(bArr, vbUnicode)
Close #1
str1 = Replace(str1, "|", Chr(9))
Open "c:\1.xls" For Output As #1
Print #1, str1
Close #1
End Sub
Private Sub Form_Load()
Dim s As String
s = "0004|72280|张三|abc " & vbCrLf & _
"0005|72282|张三|abc " & vbCrLf & _
"0006|72283|张三|123 " & vbCrLf & _
"0004|72281|张三|232 " & vbCrLf & _
"0007|72284|张三|123 " & vbCrLf & _
"0008|72280|张三|123 " & vbCrLf
Open "c:\1.txt" For Output As #1
Print #1, s
Close #1
End Sub
思路和代码跟1楼是一样的…… 楼主木有仔细测试1楼代码……
If 楼主给分给我 Then
楼主木有小jj
End If
来个最简单的 使用 VsFlexgrid 添加数据进去 直接保存Excel文件 本帖最后由 bcrun 于 2011-11-30 21:59:27 编辑
Private Sub Command1_Click()
Dim xlApp As Object
Set xlApp = CreateObject("Excel.Application")
xlApp.Workbooks.OpenText FileName:= _
"D:\KH3210110180042011110902.txt", _
Other:=True, OtherChar:="|", FieldInfo:= _
Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1))
xlApp.Visible = True
xlApp.DisplayAlerts = False
xlApp.ActiveWorkbook.SaveAs FileName:= _
"D:\KH3210110180042011110902.xls", FileFormat:=43
xlApp.DisplayAlerts = True
xlApp.Quit
Set xlApp = Nothing
MsgBox "文件转换完成"
End Sub
提示类workbook的saveas 方法无效
宏有个问题,假如我第2列,15位长度,就变成科学计算法,应该如何设置呢? 另外能否让转换的时候不弹出excel对话框呢,excel不太熟悉, 以上思路都不太感觉好,我也不知道我原来的错在哪儿
xlApp.ActiveWorkbook.SaveAs Filename:= _
"C:\Users\Administrator\Desktop\KH3210110180042011110902.xls", FileFormat:= _
xlExcel8, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
, CreateBackup:=False
有木有试试7楼代码? 7楼和一楼差不多啊,其实还是应该对每个单元格赋值比较好,用vba没有通用性 给出原来的代码错误在什么地方??
补充:VB , 基础类