欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用c#在word文档中创建表格的方法详解

程序员文章站 2023-12-20 18:13:58
复制代码 代码如下:public string createwordfile()        {&n...
复制代码 代码如下:

public string createwordfile()
        {
            string message = "";
            try
            {
                object nothing = system.reflection.missing.value;
                string name = "xiehuan.doc";
                object filename = @"c:\users\xiehuan\xxx\" + name;  //文件保存路径
                //创建word文档
                microsoft.office.interop.word.application wordapp = new microsoft.office.interop.word.applicationclass();
                microsoft.office.interop.word.document worddoc = wordapp.documents.add(ref nothing, ref nothing, ref nothing, ref nothing);
                //添加页眉
                wordapp.activewindow.view.type = wdviewtype.wdoutlineview;
                wordapp.activewindow.view.seekview = wdseekview.wdseekprimaryheader;
                wordapp.activewindow.activepane.selection.insertafter("[页眉内容]");
                wordapp.selection.paragraphformat.alignment = microsoft.office.interop.word.wdparagraphalignment.wdalignparagraphright;//设置右对齐
                wordapp.activewindow.view.seekview = wdseekview.wdseekmaindocument;//跳出页眉设置
                wordapp.selection.paragraphformat.linespacing = 15f;//设置文档的行间距
                //移动焦点并换行
                object count = 14;
                object wdline = microsoft.office.interop.word.wdunits.wdline;//换一行;
                 wordapp.selection.movedown(ref wdline, ref count, ref nothing);//移动焦点
                 wordapp.selection.typeparagraph();//插入段落
                 //文档中创建表格
                 microsoft.office.interop.word.table newtable = worddoc.tables.add(wordapp.selection.range, 12, 3, ref nothing, ref nothing);
                 //设置表格样式
                 newtable.borders.outsidelinestyle = microsoft.office.interop.word.wdlinestyle.wdlinestylethickthinlargegap;
                 newtable.borders.insidelinestyle = microsoft.office.interop.word.wdlinestyle.wdlinestylesingle;
                 newtable.columns[1].width = 100f;
                 newtable.columns[2].width = 220f;
                 newtable.columns[3].width = 105f;
                 //填充表格内容
                 newtable.cell(1, 1).range.text = "产品详细信息表";
                 newtable.cell(1, 1).range.bold = 2;//设置单元格中字体为粗体
                 //合并单元格
                 newtable.cell(1, 1).merge(newtable.cell(1, 3));
                 wordapp.selection.cells.verticalalignment = microsoft.office.interop.word.wdcellverticalalignment.wdcellalignverticalcenter;//垂直居中
                 wordapp.selection.paragraphformat.alignment = microsoft.office.interop.word.wdparagraphalignment.wdalignparagraphcenter;//水平居中

                 //填充表格内容
                 newtable.cell(2, 1).range.text = "产品基本信息";
                 newtable.cell(2, 1).range.font.color = microsoft.office.interop.word.wdcolor.wdcolordarkblue;//设置单元格内字体颜色
                 //合并单元格
                 newtable.cell(2, 1).merge(newtable.cell(2, 3));
                 wordapp.selection.cells.verticalalignment = microsoft.office.interop.word.wdcellverticalalignment.wdcellalignverticalcenter;
                  //填充表格内容
                  newtable.cell(3, 1).range.text = "品牌名称:";
                  newtable.cell(3, 2).range.text = "brandname";
                  //纵向合并单元格
                  newtable.cell(3, 3).select();//选中一行
                  object moveunit = microsoft.office.interop.word.wdunits.wdline;
                  object movecount = 5;
                  object moveextend = microsoft.office.interop.word.wdmovementtype.wdextend;
                   wordapp.selection.movedown(ref moveunit, ref movecount, ref moveextend);
                   wordapp.selection.cells.merge();
                   //插入图片
                   string filename = picture;//图片所在路径
                   object linktofile = false;
                   object savewithdocument = true;
                   object anchor = worddoc.application.selection.range;
                   worddoc.application.activedocument.inlineshapes.addpicture(filename, ref linktofile, ref savewithdocument, ref anchor);
                    worddoc.application.activedocument.inlineshapes[1].width = 100f;//图片宽度
                    worddoc.application.activedocument.inlineshapes[1].height = 100f;//图片高度
                    //将图片设置为四周环绕型
                    microsoft.office.interop.word.shape s = worddoc.application.activedocument.inlineshapes[1].converttoshape();
                    s.wrapformat.type = microsoft.office.interop.word.wdwraptype.wdwrapsquare;

                    newtable.cell(12, 1).range.text = "产品特殊属性";
                    newtable.cell(12, 1).merge(newtable.cell(12, 3));
                     //在表格中增加行
                     worddoc.content.tables[1].rows.add(ref nothing);

                     worddoc.paragraphs.last.range.text = "文档创建时间:" + datetime.now.tostring();//“落款”
                     worddoc.paragraphs.last.alignment = microsoft.office.interop.word.wdparagraphalignment.wdalignparagraphright;
                    //文件保存
                    worddoc.saveas(ref filename, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing);
                    worddoc.close(ref nothing, ref nothing, ref nothing);
                    wordapp.quit(ref nothing, ref nothing, ref nothing);
                    message=name+"文档生成成功,以保存到c:cnsi下";
            }
            catch (system.exception e)
            {
                messagebox.show(e.message);
            }
            return message;
        }

上一篇:

下一篇: