您的位置:程序门 -> .net技术 -> vb.net



高分求!!!!!!!!能用程序创建word表格并写入数据的方法


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


高分求!!!!!!!!能用程序创建word表格并写入数据的方法[已结贴,结贴人:hkbd894]
发表于:2007-02-06 11:26:33 楼主
要把数据写入一个word表格.但表格并不规则.如何能创建表格并确定位置写入数据呢??表格样式如下:
    a                     b                             c                     d
┌──┬──────┬──────┬───┐
├──┴──────┴──────┴───┤
├──┬──────┬──────┬───┤
│  ├──────┴──────┴───┤
└──┴─────────────────┘
希望能顺便告知控制页眉页脚的方法.谢谢!编写环境vb2005+office2000
发表于:2007-02-06 11:39:061楼 得分:0
不会楼主问题,强烈建议用EXECl。
发表于:2007-02-06 11:39:592楼 得分:0
强烈建议用EXECl。
发表于:2007-02-06 11:46:273楼 得分:50
'插入表格
public   sub   inserttable(byref   table   as   datatable)
        dim   otable   as   word.table
        dim   rowindex,   colindex,   numrows,   numcolumns   as   integer
        rowindex   =   1
        colindex   =   0
        if   (table.rows.count   =   0)   then
                exit   sub
        end   if

        numrows   =   table.rows.count   +   1
        numcolumns   =   table.columns.count
        otable   =   odocument.tables.add(owordapplic.selection.range(),   numrows,   numcolumns)


        '初始化列
        dim   row   as   datarow
        dim   col   as   datacolumn
        'for   each   col   in   table.columns
        '         colindex   =   colindex   +   1
        '         otable.cell(1,   colindex).range.insertafter(col.columnname)
        'next

        '将行添入表格
        for   each   row   in   table.rows
                rowindex   =   rowindex   +   1
                colindex   =   0
                for   each   col   in   table.columns
                        colindex   =   colindex   +   1
                        otable.cell(rowindex,   colindex).range.insertafter(row(col.columnname))
                next
        next
        otable.rows(1).delete()
        otable.allowautofit   =   true
        otable.applystylefirstcolumn   =   true
        otable.applystyleheadingrows   =   true

end   sub
'插入表格(修改为在原有表格的基础上添加数据)
public   sub   inserttable2(byref   table   as   datatable,   byval   strbmerge   as   string,   byval   totalrow   as   integer)
        dim   otable   as   word.table
        dim   rowindex,   colindex,   numrows,   numcolumns   as   integer
        dim   strm()   as   string
        dim   i   as   integer
        rowindex   =   1
        colindex   =   0

        if   (table.rows.count   =   0)   then
                exit   sub
        end   if

        numrows   =   table.rows.count   +   1
        numcolumns   =   table.columns.count
        'otable   =   odocument.tables.add(owordapplic.selection.range(),   numrows,   numcolumns)


        '初始化列
        dim   row   as   datarow
        dim   col   as   datacolumn
        'for   each   col   in   table.columns
        '         colindex   =   colindex   +   1
        '         otable.cell(1,   colindex).range.insertafter(col.columnname)
        'next

        '将行添入表格
        for   each   row   in   table.rows
                colindex   =   0
                gotorightcell()
                owordapplic.selection.insertrows(1)
                for   each   col   in   table.columns
                        gotorightcell()
                        colindex   =   colindex   +   1
                        try
                                owordapplic.selection.typetext(row(col.columnname))
                        catch   ex   as   exception
                                owordapplic.selection.typetext( "   ")
                        end   try
                        'owordapplic.selection.cell(rowindex,   colindex).range.insertafter(row(col.columnname))
                next
        next
        '如果strbmerge不为空.则要合并相应的行和列
        if   strbmerge.trim().length   <>   0   then
                strm   =   strbmerge.split( "; ")
                for   i   =   1   to   strm.length   -   1
                        if   strm(i).split( ", ").length   =   2   then
                                mergedouble(totalrow,   strm(0),   strm(i).split( ", ")(1),   strm(i).split( ", ")(0))
                        end   if
                        mergesingle(totalrow,   strm(0),   strm(i))
                next
        end   if
        '删除可能多余的一行
        'gotorightcell()
        'gotodowncell()
        'owordapplic.selection.rows.delete()
        'otable.allowautofit   =   true
        'otable.applystylefirstcolumn   =   true
        'otable.applystyleheadingrows   =   true
end   sub
'插入表格(专门适应工程结算工程量清单)
public   sub   inserttableqd(byref   table   as   datatable,   byref   table1   as   datatable)
        dim   otable   as   word.table
        dim   rowindex,   colindex,   numrows,   numcolumns   as   integer
        dim   xmmc   as   string
        dim   i   as   integer
        dim   j   as   integer
        rowindex   =   1
        colindex   =   0

        if   (table.rows.count   =   0)   then
                exit   sub
        end   if

        numrows   =   table.rows.count   +   1
        numcolumns   =   table.columns.count
        'otable   =   odocument.tables.add(owordapplic.selection.range(),   numrows,   numcolumns)


        '初始化列
        dim   row   as   datarow
        dim   rowtemp   as   datarow
        dim   row1()   as   datarow
        dim   col   as   datacolumn
        dim   coltemp   as   datacolumn
        'for   each   col   in   table.columns
        '         colindex   =   colindex   +   1
        '         otable.cell(1,   colindex).range.insertafter(col.columnname)
        'next

        '将行添入表格
        for   each   row   in   table.rows
                colindex   =   0
                xmmc   =   row( "项目名称 ")
                gotorightcell()
                owordapplic.selection.insertrows(1)
                for   each   col   in   table.columns
                        gotorightcell()
                        try
                                if   (col.columnname   =   "项目序号 ")   then
                                        owordapplic.selection.typetext(inttoupint(val(row(col.columnname))))
                                else
                                        owordapplic.selection.typetext(row(col.columnname))
                                end   if
                        catch   ex   as   exception
                                owordapplic.selection.typetext( "   ")
                        end   try
                        'owordapplic.selection.cell(rowindex,   colindex).range.insertafter(row(col.columnname))
                next
                row1   =   table1.select( "项目名称= ' "   +   xmmc   +   " ' ")

                for   i   =   0   to   row1.length   -   1
                        gotorightcell()
                        owordapplic.selection.insertrows(1)
                        for   j   =   0   to   table1.columns.count   -   1
                                if   (table1.columns(j).columnname   <>   "项目名称 ")   then
                                        gotorightcell()
                                        try
                                                owordapplic.selection.typetext(row1(i)(j))
                                        catch   ex   as   exception
                                                owordapplic.selection.typetext( "   ")
                                        end   try
                                end   if
                                'owordapplic.selection.cell(rowindex,   colindex).range.insertafter(row(col.columnname))
                        next
                next


        next
        '删除可能多余的一行
        'gotorightcell()
        'gotodowncell()
        'owordapplic.selection.rows.delete()
        'otable.allowautofit   =   true
        'otable.applystylefirstcolumn   =   true
        'otable.applystyleheadingrows   =   true
end   sub
'插入表格,为了满足要求,在中间添加一根竖线
public   sub   inserttable3(byref   table   as   datatable,   byval   introw   as   integer,   byval   intcol   as   integer)
        dim   rowindex,   colindex,   numrows,   numcolumns   as   integer
        dim   row   as   datarow
        dim   col   as   datacolumn
        if   (table.rows.count   =   0)   then
                exit   sub
        end   if
        '首先是拆分选中的单元格
        odocument.tables(1).cell(introw,   3).split(table.rows.count,   2)
        '选中初始的单元格
        odocument.tables(1).cell(introw,   3).select()
        '将行添入表格
        for   each   row   in   table.rows
                try
                        odocument.tables(1).cell(introw,   3).range.insertafter(row(0))
                        odocument.tables(1).cell(introw,   4).range.insertafter(row(1))
                catch   ex   as   exception
                        odocument.tables(1).cell(introw,   3).range.insertafter( "   ")
                        odocument.tables(1).cell(introw,   4).range.insertafter( "   ")
                end   try
                introw   =   introw   +   1
        next
end   sub
发表于:2007-02-06 11:48:004楼 得分:0
http://msdn2.microsoft.com/en-us/library/aa165321
发表于:2007-02-06 13:13:015楼 得分:50
简单示意一下:
public   class   form1

        dim   demo   as   new   worddemo
        private   sub   button1_click(byval   sender   as   system.object,   byval   e   as   system.eventargs)   handles   button1.click
                demo.adddocument()
        end   sub

        private   sub   button2_click(byval   sender   as   system.object,   byval   e   as   system.eventargs)   handles   button2.click
                demo.addtable()
        end   sub

        private   sub   button3_click(byval   sender   as   system.object,   byval   e   as   system.eventargs)   handles   button3.click
                demo.settableformat()
        end   sub

        private   sub   button4_click(byval   sender   as   system.object,   byval   e   as   system.eventargs)   handles   button4.click
                demo.setvalue()
        end   sub

        private   sub   button5_click(byval   sender   as   system.object,   byval   e   as   system.eventargs)   handles   button5.click
                demo.quit()
        end   sub
end   class

public   class   worddemo
        private   gwordapplication   as   object
        private   gdoc   as   object
        private   grange   as   object

        sub   new()
                gwordapplication   =   createobject( "word.application ")

                gwordapplication.visible   =   true
        end   sub

        public   sub   adddocument()
                gdoc   =   gwordapplication.documents.add()
        end   sub

        public   sub   addtable()
                gdoc.tables.add(range:=gdoc.range(start:=0,   end:=0),   numrows:=4,   numcolumns:=5)
        end   sub

        public   sub   settableformat()
                with   gdoc.tables(1)
                        grange   =   gdoc.range(.cell(3,   1).range.start,   .cell(4,   1).range.end)
                        grange.select()
                        gdoc.activewindow.selection.cells.merge()

                        gdoc.range(.cell(2,   1).range.start,   .cell(2,   5).range.end).cells.merge()
                        gdoc.range(.cell(4,   2).range.start,   .cell(4,   5).range.end).cells.merge()
                        .cell(3,   1).verticalalignment   =   1   'const   wdcellalignverticalcenter   =   1
                end   with
        end   sub

        public   sub   setvalue()
                with   gdoc.tables(1)
                        .cell(1,   1).range.insertafter( "a ")
                        .cell(1,   2).range.insertafter( "b ")
                        .cell(1,   3).range.insertafter( "c ")
                        .cell(1,   4).range.insertafter( "d ")
                        .cell(1,   5).range.insertafter( "e ")
                        .cell(3,   1).range.insertafter( "中国 ")
                end   with
        end   sub

        public   sub   quit()
                gwordapplication.quit()
        end   sub
end   class
发表于:2007-02-06 13:31:336楼 得分:0
终于不在头疼了..给个例题我就能知道怎么做了..感谢oliverlin和水如烟.谢谢.万分谢谢


快速检索

最新资讯
热门点击