dolphinzhu的个人博客分享 http://blog.sciencenet.cn/u/dolphinzhu

博文

用C#画表格

已有 5467 次阅读 2013-12-9 11:48 |个人分类:CSHARP|系统分类:教学心得

       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文档

               Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.Application();

               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 = "文件导出异常!";

           }


       }



   }

参考自:http://hi.baidu.com/blessyou312/item/68c3738e1af61557e63d1999



https://blog.sciencenet.cn/blog-465809-748310.html

上一篇:快速定位word 的模板文件
下一篇:Win7 添加/删除用户
收藏 IP: 116.235.145.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-9-27 15:21

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部