| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2811 人关注过本帖
标题:如何将dat文件导入到EXCEL,空格转为列
只看楼主 加入收藏
kexiya123
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2019-9-25
结帖率:0
收藏
已结贴  问题点数:20 回复次数:3 
如何将dat文件导入到EXCEL,空格转为列
Sub 导数据()   
    With Application.FileDialog(msoFileDialogFilePicker)   
        .InitialFileName = ThisWorkbook.Path   
        .Title = "请选择一个或多个文件"   
        If .Show Then Set fs = .SelectedItems Else Exit Sub   
    End With   
    For Each f In fs   
        Open f For Input As #1   
        s = s & Input(LOF(1), 1)   
        Close #1   
    Next   
    Set regx = CreateObject("vbscript.regexp")   
    regx.Pattern = "^\s*\d.+[\r\n]+.*"   
    regx.Global = True: regx.MultiLine = True   
    Set mh = regx.Execute(s)   
    If mh.Count = 0 Then MsgBox "文本格式不符!": Exit Sub   
    ReDim arr(1 To mh.Count, 1 To 10)
    regx.Pattern = "([+-]\s*)?\S+"   
     For i = 0 To mh.Count - 1   
        Set mh1 = regx.Execute(Application.Clean(Replace(mh(i), "Skip", "")))   
        For j = 0 To mh1.Count - 1   
            arr(i + 1, j + 1) = mh1(j)   
        Next   
    Next   
这个程序是能够导入dat到EXCEL,但是唯一不足的是:data文件10行导入到EXCEL是5行,源程序看不太懂,有没有大师修改下呢?
搜索更多相关主题的帖子: dat 文件 EXCEL Set 导入 
2019-09-25 15:19
kexiya123
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2019-9-25
收藏
得分:0 
上述拷贝有点遗漏,全部程序如下
Sub 导数据()   
    With Application.FileDialog(msoFileDialogFilePicker)   
        .InitialFileName = ThisWorkbook.Path   
        .Title = "请选择一个或多个文件"   
        If .Show Then Set fs = .SelectedItems Else Exit Sub   
    End With   
    For Each f In fs   
        Open f For Input As #1   
        s = s & Input(LOF(1), 1)   
        Close #1   
    Next   
    Set regx = CreateObject("vbscript.regexp")   
    regx.Pattern = "^\s*\d.+[\r\n]+.*"   
    regx.Global = True: regx.MultiLine = True   
    Set mh = regx.Execute(s)   
    If mh.Count = 0 Then MsgBox "文本格式不符!": Exit Sub   
    ReDim arr(1 To mh.Count, 1 To 10) '定义一个数组arr,,行数为1到mh.Count,列数为1到5   
    regx.Pattern = "([+-]\s*)?\S+"   
     For i = 0 To mh.Count - 1   
        Set mh1 = regx.Execute(Application.Clean(Replace(mh(i), "Skip", "")))   
        For j = 0 To mh1.Count - 1   
            arr(i + 1, j + 1) = mh1(j)   
        Next   
    Next   
   
    Range("a1").CurrentRegion.Offset(1) = ""   
    Range("a2").Resize(UBound(arr), 10) = arr   
End Sub   
2019-09-25 15:20
kexiya123
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2019-9-25
收藏
得分:0 
dat文件数据如下:
1     0           0.000       0.000       0.000      
2     100         57.530      0.000       0.000      
3     200         55.805      0.000       0.000      
4     300         55.363      0.000       0.000      
5     400         55.424      0.000       0.000      
6     500         54.373      0.000       0.000      
7     600         53.367      0.000       0.000      
8     700         55.646      0.000       0.000      
9     800         56.656      0.000       0.000      
10    900         56.842      0.000       0.000     
我想要导入后EXCEL后是10行5列

2019-09-25 15:24
Cyberoe2
Rank: 2
等 级:论坛游民
威 望:3
帖 子:16
专家分:45
注 册:2019-8-9
收藏
得分:20 
程序代码:
    Dim k, n As Integer
    k = 1
     For i = 0 To mh.Count - 1
            n = 1
            Set mh1 = regx.Execute(Application.Clean(Replace(mh(i), "Skip", "")))
            For j = 0 To mh1.Count - 1
                    If j = 5 Then
                        k = k + 1
                        n = 1
                    End If
                    arr(k, n) = mh1(j)
                    n = n + 1
            Next
            k = k + 1
    Next

后面的循环这样补充你看看
2019-09-26 09:39
快速回复:如何将dat文件导入到EXCEL,空格转为列
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017685 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved