Foxtable(狐表)用户栏目专家坐堂 → [求助]如何将表中的所有数据生成Word


  共有2632人关注过本帖树形打印复制链接

主题:[求助]如何将表中的所有数据生成Word

帅哥哟,离线,有人找我吗?
有点甜
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2017/11/13 15:21:00 [显示全部帖子]

参考代码

 

Dim dttable = DataTables("表A")

Dim app As New MSWord.Application
try
    Dim doc = app.Documents.add
    Dim dt2 As DataTable = dttable
    doc.Tables.Add(Range:=app.Selection.Range,NumRows:=1, NumColumns:= dt2.DataCols.Count)
    With app.Selection.Tables(1)
        .ApplyStyleHeadingRows = True
        .ApplyStyleLastRow = True
        .ApplyStyleFirstColumn = True
        .ApplyStyleLastColumn = True
        .Style = "网格型"
    End With
    For Each dc As DataCol In dt2.DataCols
        app.Selection.TypeText(Text:=dc.Name)
        Dim i As Double
        If Double.TryParse(dc.name,i)
            app.Selection.ParagraphFormat.Alignment = 2
        Else
            app.Selection.ParagraphFormat.Alignment = 1
        End If
        app.Selection.MoveRight(Unit:=12)
    Next
    For i As Integer = 0 To dt2.datarows.count-1
        Dim dr As DataRow = dt2.datarows(i)
       
        For j As Integer = 0 To dt2.datacols.count - 1
            Dim dc As DataCol = dt2.datacols(j)
            app.Selection.TypeText(Text:=dr(dc.Name))
            Dim d As Double
            If Double.TryParse(dr(dc.name),d)
                app.Selection.ParagraphFormat.Alignment = 2
            Else
                app.Selection.ParagraphFormat.Alignment = 1
            End If
            If i = dt2.datarows.count-1 AndAlso j = dt2.datacols.count-1
            Else
                app.Selection.MoveRight(Unit:=12)
            End If
        Next
       
    Next
    app.visible = True
catch ex As exception
    app.Quit
finally

End try


 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2017/11/13 15:58:00 [显示全部帖子]

改成自己的表名,执行看结果,然后慢慢看懂代码

 

Dim dttable = DataTables("表A")

Dim app As New MSWord.Application
try
    Dim doc = app.Documents.add
    Dim dt2 As DataTable = dttable
    doc.Tables.Add(Range:=app.Selection.Range,NumRows:=1, NumColumns:= dt2.DataCols.Count)
    With app.Selection.Tables(1)
        .ApplyStyleHeadingRows = True
        .ApplyStyleLastRow = True
        .ApplyStyleFirstColumn = True
        .ApplyStyleLastColumn = True
        .Style = "网格型"
    End With
    For Each dc As DataCol In dt2.DataCols
        app.Selection.TypeText(Text:=dc.Name)
        Dim i As Double
        If Double.TryParse(dc.name,i)
            app.Selection.ParagraphFormat.Alignment = 2
        Else
            app.Selection.ParagraphFormat.Alignment = 1
        End If
        app.Selection.MoveRight(Unit:=12)
    Next
    For i As Integer = 0 To dt2.datarows.count-1
        Dim dr As DataRow = dt2.datarows(i)
       
        For j As Integer = 0 To dt2.datacols.count - 1
            Dim dc As DataCol = dt2.datacols(j)
            app.Selection.TypeText(Text:=dr(dc.Name))
            Dim d As Double
            If Double.TryParse(dr(dc.name),d)
                app.Selection.ParagraphFormat.Alignment = 2
            Else
                app.Selection.ParagraphFormat.Alignment = 1
            End If
            If i = dt2.datarows.count-1 AndAlso j = dt2.datacols.count-1
            Else
                app.Selection.MoveRight(Unit:=12)
            End If
        Next
       
    Next
    app.visible = True
catch ex As exception
    app.Quit
finally

End try


 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  3楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2017/11/13 16:19:00 [显示全部帖子]

无语,你能不能看懂代码后处理?

 

不会做,那请具体表格发上来测试。

[此贴子已经被作者于2017/11/13 16:19:21编辑过]

 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  4楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2017/11/13 17:08:00 [显示全部帖子]

Dim t = Tables("表A")
Dim cname = "第一列"

Dim app As New MSWord.Application
try
    Dim doc = app.Documents.add
    app.Selection.font.Size = 20 '字号
    app.Selection.font.Bold = True   '加粗
    app.Selection.TypeText(Text:="xxx系统表结构" & vbcrlf)
   
    app.Selection.font.Size = 9 '字号
    app.Selection.font.Bold = False
   
    For k As Integer = 0 To t.Rows.count-1
        If t.Rows(k).IsNull(cname) = False Then
            app.Selection.TypeText(Text:=t.Rows(K)(cname) & vbcrlf)
            Dim tb = doc.Tables.Add(Range:=app.Selection.Range,NumRows:=1, NumColumns:= t.Cols.Count-1)
            With tb
                .ApplyStyleHeadingRows = True
                .ApplyStyleLastRow = True
                .ApplyStyleFirstColumn = True
                .ApplyStyleLastColumn = True
                .Style = "网格型"
            End With
            For Each dc As Col In t.Cols
                If dc.name <> cname Then
                    app.Selection.TypeText(Text:=dc.Name)
                    Dim i As Double
                    If Double.TryParse(dc.name,i)
                        app.Selection.ParagraphFormat.Alignment = 2
                    Else
                        app.Selection.ParagraphFormat.Alignment = 1
                    End If
                    app.Selection.MoveRight(Unit:=12)
                End If
            Next
            For i As Integer = k To t.rows.count-1
                Application.doevents
                Dim dr As Row = t.rows(i)
                If i > K AndAlso t.Rows(i).IsNull(cname) = False Then
                    k = i-1
                    app.Selection.MoveRight(Unit:=1,count:=1)
                    app.ActiveWindow.Selection.TypeParagraph
                    app.Selection.MoveRight(Unit:=1,count:=1)
                    app.ActiveWindow.Selection.TypeParagraph
                    Exit For
                ElseIf i>k Then
                    app.Selection.MoveRight(Unit:=12)
                End If
                output.show(i)
                For j As Integer = 0 To t.Cols.count - 1
                    Dim dc As Col = t.Cols(j)
                    If dc.name <> cname Then
                        app.Selection.TypeText(Text:=dr(dc.Name))
                        Dim d As Double
                        If Double.TryParse(dr(dc.name),d)
                            app.Selection.ParagraphFormat.Alignment = 2
                        Else
                            app.Selection.ParagraphFormat.Alignment = 1
                        End If
                        If j = t.cols.count-1
                        Else
                            app.Selection.MoveRight(Unit:=12)
                        End If
                    End If
                Next
               
            Next
        End If
    Next
    app.visible = True
catch ex As exception
    msgbox(ex.message)
    app.Quit
finally
   
End try


 回到顶部