Foxtable(狐表)用户栏目专家坐堂 → 请教袍兄


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

主题:请教袍兄

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


加好友 发短信
等级:狐神 帖子:5055 积分:13047 威望:0 精华:0 注册:2014/5/15 21:34:00
  发帖心情 Post By:2015/11/4 19:34:00 [只看该作者]

一天都没有找到,下面的代码还是不行,什么原因?好像有鬼?帮我看看?
Dim app As New MSWord.Application
try
    Dim doc = app.Documents.Open(tm)
    If app.ActiveWindow.Selection.Find.Execute("[" & bm & "附表]") Then
        '插入表格,方法12
        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 dt.rows.count-1
            Dim dr As Row = dt.rows(i)
          
                For j As Integer = 0 To dt.cols.count - 1
                    Dim dc As Col = dt.cols(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 = dt.rows.count-1 AndAlso j = dt.cols.count-1
                    Else
                        app.Selection.MoveRight(Unit:=12)
                    End If
                Next
            
        Next       
    End If 
    app.Documents(tm).save  
    app.quit
catch ex As exception
    app.Quit
finally
End try

 回到顶部
帅哥哟,离线,有人找我吗?
大红袍
  12楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:贵宾 帖子:39310 积分:196782 威望:0 精华:1 注册:2015/4/25 9:23:00
  发帖心情 Post By:2015/11/4 19:45:00 [只看该作者]

Dim tm = "d:\test.doc"
Dim bm = "test"
Dim dttable = DataTables("表A")

Dim app As New MSWord.Application
try
    Dim doc = app.Documents.Open(tm)
    If app.ActiveWindow.Selection.Find.Execute("" & bm & "") Then
        '插入表格,方法1或2
        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
    End If
    app.Documents(tm).save
    app.quit
catch ex As exception
    app.Quit
finally
End try


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


加好友 发短信
等级:狐神 帖子:5055 积分:13047 威望:0 精华:0 注册:2014/5/15 21:34:00
  发帖心情 Post By:2015/11/4 20:00:00 [只看该作者]

嘿,不是一模一样吗?鬼从何来?难道换成table就不行?但是用datable遍列数据写入word时,行的次序又是按默认的字母排列?

 回到顶部
帅哥哟,离线,有人找我吗?
大红袍
  14楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:贵宾 帖子:39310 积分:196782 威望:0 精华:1 注册:2015/4/25 9:23:00
  发帖心情 Post By:2015/11/4 20:08:00 [只看该作者]

可以用table啊,只要匹配,就可以啊。比如dt必须是table;

 

行的次序,你可以排序嘛。比如先用select排序得到drs集合,再循环drs。


 回到顶部
总数 14 上一页 1 2