以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  请教袍兄  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=76736)

--  作者:hbhb
--  发布时间:2015/11/4 10:47:00
--  请教袍兄
请问:狐表导出到word后,表格中的数字自动靠右,字符居中怎么写代码?指导指导的,有
--  作者:大红袍
--  发布时间:2015/11/4 10:53:00
--  
Dim app As New MSWord.Application
try
    Dim doc = app.Documents.add
    Dim dt As DataTable = DataTables("表A")
    doc.Tables.Add(Range:=app.Selection.Range,NumRows:=1, NumColumns:= dt.DataCols.Count)
    With app.Selection.Tables(1)
        .ApplyStyleHeadingRows = True
        .ApplyStyleLastRow = True
        .ApplyStyleFirstColumn = True
        .ApplyStyleLastColumn = True
    End With
    For Each dc As DataCol In dt.DataCols
        app.Selection.TypeText(Text:=dc.Name)
        app.Selection.MoveRight(Unit:=12)
    Next
    For Each dr As DataRow In dt.DataRows
        For Each dc As DataCol In dt.DataCols
            app.Selection.TypeText(Text:=dr(dc.Name))
            If dc.IsNumeric Then
                app.Selection.ParagraphFormat.Alignment = 2
            Else
                app.Selection.ParagraphFormat.Alignment = 1
            End If

            app.Selection.MoveRight(Unit:=12)
        Next
    Next
    app.Visible = True
catch ex As exception
    msgbox(ex.message)
    app.Quit
finally
   
End try

--  作者:hbhb
--  发布时间:2015/11/4 11:36:00
--  
谢谢!
不过,我写进去的是数字字符,因为是临时表,设计时列类型用的是object,所有这样判断不行。
另外,如果数字是零字符,则为空,怎么写?

--  作者:大红袍
--  发布时间:2015/11/4 11:40:00
--  

Dim app As New MSWord.Application
try
    Dim doc = app.Documents.add
    Dim dt As DataTable = DataTables("表A")
    doc.Tables.Add(Range:=app.Selection.Range,NumRows:=1, NumColumns:= dt.DataCols.Count)
    With app.Selection.Tables(1)
        .ApplyStyleHeadingRows = True
        .ApplyStyleLastRow = True
        .ApplyStyleFirstColumn = True
        .ApplyStyleLastColumn = True
    End With
    For Each dc As DataCol In dt.DataCols
        app.Selection.TypeText(Text:=dc.Name)
        app.Selection.MoveRight(Unit:=12)
    Next
    For Each dr As DataRow In dt.DataRows
        For Each dc As DataCol In dt.DataCols
            app.Selection.TypeText(Text:=dr(dc.Name))
            Dim i As Double
            If Double.TryParse(dr(dc.name),i)
                app.Selection.ParagraphFormat.Alignment = 2
            Else
                app.Selection.ParagraphFormat.Alignment = 1
            End If
            app.Selection.MoveRight(Unit:=12)
        Next
    Next
    app.Visible = True
catch ex As exception
    msgbox(ex.message)
    app.Quit
finally
   
End try


--  作者:hbhb
--  发布时间:2015/11/4 12:29:00
--  
谢谢,多谢!
还有一点就是表格最后多一空行?

--  作者:大红袍
--  发布时间:2015/11/4 14:36:00
--  
Dim app As New MSWord.Application
try
    Dim doc = app.Documents.add
    Dim dt As DataTable = DataTables("表A")
    doc.Tables.Add(Range:=app.Selection.Range,NumRows:=1, NumColumns:= dt.DataCols.Count)
    With app.Selection.Tables(1)
        .ApplyStyleHeadingRows = True
        .ApplyStyleLastRow = True
        .ApplyStyleFirstColumn = True
        .ApplyStyleLastColumn = True
    End With
    For Each dc As DataCol In dt.DataCols
        app.Selection.TypeText(Text:=dc.Name)
        app.Selection.MoveRight(Unit:=12)
    Next
    For i As Integer = 0 To dt.datarows.count-1
        Dim dr As DataRow = dt.datarows(i)
        for j as integer = 0 to dt.datacols.count - 1
            dim dc as datacol = dt.datacols(i)
            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.datarows.count-1 AndAlso j = dt.datacols.count-1
                \'不做操作
            Else
                app.Selection.MoveRight(Unit:=12)
                End If
            Next
        Next
        app.Visible = True
    catch ex As exception
        msgbox(ex.message)
        app.Quit
    finally
       
    End try

--  作者:hbhb
--  发布时间:2015/11/4 17:21:00
--  
谢谢!有问题,第一列变为第一行了?
--  作者:大红袍
--  发布时间:2015/11/4 17:27:00
--  
Dim app As New MSWord.Application
try
    Dim doc = app.Documents.add
    Dim dt As DataTable = DataTables("表A")
    doc.Tables.Add(Range:=app.Selection.Range,NumRows:=1, NumColumns:= dt.DataCols.Count)
    With app.Selection.Tables(1)
        .ApplyStyleHeadingRows = True
        .ApplyStyleLastRow = True
        .ApplyStyleFirstColumn = True
        .ApplyStyleLastColumn = True
    End With
    For Each dc As DataCol In dt.DataCols
        app.Selection.TypeText(Text:=dc.Name)
        app.Selection.MoveRight(Unit:=12)
    Next
    For i As Integer = 0 To dt.datarows.count-1
        Dim dr As DataRow = dt.datarows(i)
        For j As Integer = 0 To dt.datacols.count - 1
            Dim dc As DataCol = dt.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 = dt.datarows.count-1 AndAlso j = dt.datacols.count-1
                \'不做操作
            Else
                app.Selection.MoveRight(Unit:=12)
            End If
        Next
    Next
    app.Visible = True
catch ex As exception
    msgbox(ex.message)
    app.Quit
finally
   
End try

--  作者:hbhb
--  发布时间:2015/11/4 18:55:00
--  
大师:是哪一句代码去掉的最后一行的,我找不到?
--  作者:大红袍
--  发布时间:2015/11/4 19:14:00
--  

            If i = dt.datarows.count-1 AndAlso j = dt.datacols.count-1
                \'不做操作
            Else
                app.Selection.MoveRight(Unit:=12)
            End If