C#生成复杂格式的WORD

要添加COM引用,Microfoft Word Object Library

要加入using Microsoft.Office.Interop.Word;

private void TTest()
    {
        string message = "";
        try
        {
            Object Nothing = System.Reflection.Missing.Value;
            Directory.CreateDirectory("C:/CNSI"); //创建文件所在目录
            string name = "CNSI_" + DateTime.Now.ToShortDateString() + ".doc";
            object filename = "C://CNSI//" + name; //文件保存路径
            //创建Word文档
            Application WordApp = new ApplicationClass();
            Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);

            WordApp.Selection.ParagraphFormat.LineSpacing = 23f;//设置文档的行间距

 

 

            //移动焦点并换行
            object count = 14;
            object WdLine = WdUnits.wdLine;//换一行;
            WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移动焦点
            WordApp.Selection.TypeParagraph();//插入段落
            WordApp.Selection.TypeParagraph();//插入段落

            WordDoc.Paragraphs.First.Range.Text = "暂住人口信息表";
            WordDoc.Paragraphs.First.Range.Font.Name = "黑体";
            WordDoc.Paragraphs.First.Range.Bold = 2;
            WordDoc.Paragraphs.First.Range.Font.Size = 20;
            WordDoc.Paragraphs.First.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;


            //文档中创建表格
            Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, 20, 7, ref Nothing, ref Nothing);
            //设置表格样式
            newTable.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;
            newTable.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle;
            newTable.Columns[1].Width = 40f;
            newTable.Columns[2].Width = 95f;
            newTable.Columns[3].Width = 60f;
            newTable.Columns[4].Width = 85f;
            newTable.Columns[5].Width = 60f;
            newTable.Columns[6].Width = 65f;
            newTable.Columns[7].Width = 50f;


            //填充表格内容
            newTable.Cell(1, 1).Range.Text = "基本信息";
            newTable.Cell(1, 1).Range.Bold = 2;

            //合并单元格
            newTable.Cell(1, 1).Merge(newTable.Cell(8, 1));
            WordApp.Selection.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
            WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;//水平居中

            newTable.Cell(1, 2).Range.Text = "姓名";
            newTable.Cell(1, 2).Range.Bold = 2;
            newTable.Cell(1, 2).Select();
            WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

            newTable.Cell(1, 4).Range.Text = "性别";
            newTable.Cell(1, 4).Range.Bold = 2;
            newTable.Cell(1, 4).Select();
            WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

            newTable.Cell(1, 6).Range.Text = "照片";
            newTable.Cell(1, 6).Range.Bold = 2;
            newTable.Cell(1, 6).Merge(newTable.Cell(1, 7));
            newTable.Cell(1, 6).Select();
            WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;


            //2
            newTable.Cell(2, 2).Range.Text = "别名";
            newTable.Cell(2, 2).Range.Bold = 2;
            newTable.Cell(2, 2).Select();
            WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

            newTable.Cell(2, 4).Range.Text = "民族";
            newTable.Cell(2, 4).Range.Bold = 2;
            newTable.Cell(2, 4).Select();
            WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

            //3
            newTable.Cell(3, 2).Range.Text = "身份证号";
            newTable.Cell(3, 2).Range.Bold = 2;
            newTable.Cell(3, 2).Select();
            WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

            //4
            newTable.Cell(4, 2).Range.Text = "联系方式";
            newTable.Cell(4, 2).Range.Bold = 2;
            newTable.Cell(4, 2).Select();
            WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

 


            newTable.Cell(2, 6).Merge(newTable.Cell(2, 7));
            newTable.Cell(3, 6).Merge(newTable.Cell(3, 7));
            newTable.Cell(4, 6).Merge(newTable.Cell(4, 7));
            newTable.Cell(5, 6).Merge(newTable.Cell(5, 7));
            newTable.Cell(2, 6).Merge(newTable.Cell(5, 6));

            newTable.Cell(3, 3).Merge(newTable.Cell(3, 5));
            newTable.Cell(4, 3).Merge(newTable.Cell(4, 5));

            newTable.Cell(5, 2).Range.Text = "出生日期";
            newTable.Cell(5, 2).Range.Bold = 2;
            newTable.Cell(5, 2).Select();
            WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

            newTable.Cell(5, 4).Range.Text = "来本地时间";
            newTable.Cell(5, 4).Range.Bold = 2;
            newTable.Cell(5, 4).Select();
            WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

            newTable.Cell(6, 2).Range.Text = "常住户口地址";
            newTable.Cell(6, 2).Range.Bold = 2;
            newTable.Cell(6, 2).Select();
            WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

            newTable.Cell(7, 2).Range.Text = "暂住地址";
            newTable.Cell(7, 2).Range.Bold = 2;
            newTable.Cell(7, 2).Select();
            WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

            newTable.Cell(8, 2).Range.Text = "现服务处所";
            newTable.Cell(8, 2).Range.Bold = 2;
            newTable.Cell(8, 2).Select();
            WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

            newTable.Cell(6, 3).Merge(newTable.Cell(6, 7));
            newTable.Cell(7, 3).Merge(newTable.Cell(7, 7));
            newTable.Cell(8, 3).Merge(newTable.Cell(8, 7));


            newTable.Cell(9, 1).Range.Text = "公安局信息";
            newTable.Cell(9, 1).Range.Bold = 2;
            newTable.Cell(9, 1).Merge(newTable.Cell(14, 1));
            newTable.Cell(9, 1).Select();
            WordApp.Selection.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
            WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;//水平居中

            newTable.Cell(9, 2).Range.Text = "身高";
            newTable.Cell(9, 2).Range.Bold = 2;
            newTable.Cell(9, 2).Select();
            WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

            newTable.Cell(9, 4).Range.Text = "文化程序";
            newTable.Cell(9, 4).Range.Bold = 2;
            newTable.Cell(9, 4).Select();
            WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

            newTable.Cell(9, 6).Range.Text = "政治面貌";
            newTable.Cell(9, 6).Range.Bold = 2;
            newTable.Cell(9, 6).Select();
            WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

            newTable.Cell(10, 2).Range.Text = "婚姻状态";
            newTable.Cell(10, 2).Range.Bold = 2;
            newTable.Cell(10, 2).Select();
            WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

            newTable.Cell(10, 4).Range.Text = "职业";
            newTable.Cell(10, 4).Range.Bold = 2;
            newTable.Cell(10, 4).Select();
            WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

            newTable.Cell(10, 6).Range.Text = "现从事职业";
            newTable.Cell(10, 6).Range.Bold = 2;
            newTable.Cell(10, 6).Select();
            WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

            newTable.Cell(12, 2).Range.Text = "服务处所负责人";
            newTable.Cell(12, 2).Range.Bold = 2;
            newTable.Cell(12, 2).Select();
            WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

            newTable.Cell(12, 4).Range.Text = "暂住证签发日期";
            newTable.Cell(12, 4).Range.Bold = 2;
            newTable.Cell(12, 4).Select();
            WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

            newTable.Cell(12, 6).Range.Text = "有效期";
            newTable.Cell(12, 6).Range.Bold = 2;
            newTable.Cell(12, 6).Select();
            WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;


            newTable.Cell(11, 2).Range.Text = "暂住事由";
            newTable.Cell(11, 2).Range.Bold = 2;
            newTable.Cell(11, 2).Select();
            WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

            newTable.Cell(11, 3).Merge(newTable.Cell(11, 7));

            newTable.Cell(13, 2).Range.Text = "注解原因";
            newTable.Cell(13, 2).Range.Bold = 2;
            newTable.Cell(13, 2).Select();
            WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

            newTable.Cell(14, 2).Range.Text = "函调情况";
            newTable.Cell(14, 2).Range.Bold = 2;
            newTable.Cell(14, 2).Select();
            WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

            newTable.Cell(13, 6).Range.Text = "注销日期";
            newTable.Cell(13, 6).Range.Bold = 2;
            newTable.Cell(13, 6).Select();
            WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

            newTable.Cell(14, 6).Range.Text = "连接标识";
            newTable.Cell(14, 6).Range.Bold = 2;
            newTable.Cell(14, 6).Select();
            WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

            newTable.Cell(13, 3).Merge(newTable.Cell(13, 5));
            newTable.Cell(14, 3).Merge(newTable.Cell(14, 5));

            newTable.Cell(15, 1).Range.Text = "携带15周岁及以下人口";
            newTable.Cell(15, 1).Range.Bold = 2;
            newTable.Cell(15, 1).Merge(newTable.Cell(19, 1));
            newTable.Cell(15, 1).Select();
            WordApp.Selection.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
            WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;//水平居中

            newTable.Cell(15, 2).Range.Text = "关系";
            newTable.Cell(15, 2).Range.Bold = 2;
            newTable.Cell(15, 2).Select();
            WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

            newTable.Cell(15, 3).Range.Text = "姓名";
            newTable.Cell(15, 3).Range.Bold = 2;
            newTable.Cell(15, 3).Select();
            WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

            newTable.Cell(15, 4).Range.Text = "性别";
            newTable.Cell(15, 4).Range.Bold = 2;
            newTable.Cell(15, 4).Select();
            WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

            newTable.Cell(15, 5).Range.Text = "出生日期";
            newTable.Cell(15, 5).Range.Bold = 2;
            newTable.Cell(15, 5).Select();
            WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

            newTable.Cell(15, 6).Range.Text = "出生日期";
            newTable.Cell(15, 6).Range.Bold = 2;
            newTable.Cell(15, 6).Select();
            WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

            newTable.Cell(20, 1).Range.Text = "备注";
            newTable.Cell(20, 1).Range.Bold = 2;
            newTable.Cell(20, 1).Select();
            object bzhight = 80;
            newTable.Cell(20, 1).SetHeight(ref bzhight, WdRowHeightRule.wdRowHeightAtLeast);
            WordApp.Selection.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
            WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;//水平居中
            newTable.Cell(20, 2).Merge(newTable.Cell(20, 7));

 

 

 

            //文件保存
            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
        {
            message = "文件导出异常!";
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值