码迷,mamicode.com
首页 > 其他好文 > 详细

[收藏]c#与word

时间:2014-06-08 04:57:55      阅读:291      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   code   a   http   

public string CreateWordFile(string CheckedInfo)
bubuko.com,布布扣{
bubuko.com,布布扣string message = "";
bubuko.com,布布扣try
bubuko.com,布布扣{
bubuko.com,布布扣Object Nothing = System.Reflection.Missing.Value;
bubuko.com,布布扣Directory.CreateDirectory("C:/CNSI"); //创建文件所在目录
bubuko.com,布布扣string name = "CNSI.doc";
bubuko.com,布布扣object filename = "C://CNSI//" + name; //文件保存路径
bubuko.com,布布扣//创建Word文档
bubuko.com,布布扣Word.Application WordApp = new Word.ApplicationClass();
bubuko.com,布布扣Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
bubuko.com,布布扣
bubuko.com,布布扣    //添加页眉
bubuko.com,布布扣WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;
bubuko.com,布布扣WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
bubuko.com,布布扣WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("[页眉内容]");
bubuko.com,布布扣WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;//设置右对齐
bubuko.com,布布扣WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;//跳出页眉设置
bubuko.com,布布扣
bubuko.com,布布扣WordApp.Selection.ParagraphFormat.LineSpacing = 15f;//设置文档的行间距
bubuko.com,布布扣
bubuko.com,布布扣/*WordDoc.PageSetup.Orientation = Word.WdOrientation.wdOrientLandscape; //设置页面为纵向
bubuko.com,布布扣            WordDoc.PageSetup.PageHeight = WordApp.CentimetersToPoints(21F);
bubuko.com,布布扣            WordDoc.PageSetup.PageWidth = WordApp.CentimetersToPoints(29.7F);
bubuko.com,布布扣            WordDoc.PageSetup.TopMargin = 57; //设置上边距
bubuko.com,布布扣            WordDoc.PageSetup.BottomMargin = 57;//设置下边距
bubuko.com,布布扣            WordDoc.PageSetup.LeftMargin = 57;//设置左边距
bubuko.com,布布扣            WordDoc.PageSetup.RightMargin = 57;//设置右边距*/
bubuko.com,布布扣
bubuko.com,布布扣//移动焦点并换行
bubuko.com,布布扣object count = 14;
bubuko.com,布布扣object WdLine = Word.WdUnits.wdLine;//换一行;
bubuko.com,布布扣WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移动焦点
bubuko.com,布布扣WordApp.Selection.TypeParagraph();//插入段落
bubuko.com,布布扣
bubuko.com,布布扣//文档中创建表格
bubuko.com,布布扣Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, 12, 3, ref Nothing, ref Nothing);
bubuko.com,布布扣//设置表格样式
bubuko.com,布布扣newTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleThickThinLargeGap;
bubuko.com,布布扣newTable.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
bubuko.com,布布扣newTable.Columns[1].Width = 100f;
bubuko.com,布布扣newTable.Columns[2].Width = 220f;
bubuko.com,布布扣newTable.Columns[3].Width = 105f;
bubuko.com,布布扣
bubuko.com,布布扣//填充表格内容
bubuko.com,布布扣newTable.Cell(1, 1).Range.Text = "产品详细信息表";
bubuko.com,布布扣newTable.Cell(1, 1).Range.Bold = 2;//设置单元格中字体为粗体
bubuko.com,布布扣//合并单元格
bubuko.com,布布扣newTable.Cell(1, 1).Merge(newTable.Cell(1, 3));
bubuko.com,布布扣WordApp.Selection.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
bubuko.com,布布扣WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中
bubuko.com,布布扣
bubuko.com,布布扣            //填充表格内容
bubuko.com,布布扣newTable.Cell(2, 1).Range.Text = "产品基本信息";
bubuko.com,布布扣newTable.Cell(2, 1).Range.Font.Color = Word.WdColor.wdColorDarkBlue;//设置单元格内字体颜色
bubuko.com,布布扣//合并单元格
bubuko.com,布布扣newTable.Cell(2, 1).Merge(newTable.Cell(2, 3));
bubuko.com,布布扣WordApp.Selection.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
bubuko.com,布布扣
bubuko.com,布布扣            //填充表格内容
bubuko.com,布布扣newTable.Cell(3, 1).Range.Text = "品牌名称:";
bubuko.com,布布扣newTable.Cell(3, 2).Range.Text = CheckedInfo;
bubuko.com,布布扣//纵向合并单元格
bubuko.com,布布扣newTable.Cell(3, 3).Select();//选中一行
bubuko.com,布布扣object moveUnit = Word.WdUnits.wdLine;
bubuko.com,布布扣object moveCount = 5;
bubuko.com,布布扣object moveExtend = Word.WdMovementType.wdExtend;
bubuko.com,布布扣WordApp.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend);
bubuko.com,布布扣WordApp.Selection.Cells.Merge();
bubuko.com,布布扣//插入图片
bubuko.com,布布扣string FileName = @"C:\\1.jpg";//图片所在路径
bubuko.com,布布扣object LinkToFile = false;
bubuko.com,布布扣object SaveWithDocument = true;
bubuko.com,布布扣object Anchor = WordDoc.Application.Selection.Range;
bubuko.com,布布扣WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
bubuko.com,布布扣WordDoc.Application.ActiveDocument.InlineShapes[1].Width = 100f;//图片宽度
bubuko.com,布布扣WordDoc.Application.ActiveDocument.InlineShapes[1].Height = 100f;//图片高度
bubuko.com,布布扣//将图片设置为四周环绕型
bubuko.com,布布扣Word.Shape s = WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
bubuko.com,布布扣s.WrapFormat.Type = Word.WdWrapType.wdWrapSquare;
bubuko.com,布布扣
bubuko.com,布布扣newTable.Cell(12, 1).Range.Text = "产品特殊属性";
bubuko.com,布布扣newTable.Cell(12, 1).Merge(newTable.Cell(12, 3));
bubuko.com,布布扣//在表格中增加行
bubuko.com,布布扣WordDoc.Content.Tables[1].Rows.Add(ref Nothing);
bubuko.com,布布扣
bubuko.com,布布扣            WordDoc.Paragraphs.Last.Range.Text = "文档创建时间:" + DateTime.Now.ToString();//“落款”
bubuko.com,布布扣WordDoc.Paragraphs.Last.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
bubuko.com,布布扣
bubuko.com,布布扣            //文件保存
bubuko.com,布布扣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);
bubuko.com,布布扣WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
bubuko.com,布布扣WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
bubuko.com,布布扣message = name + "文档生成成功,以保存到C:CNSI下";
bubuko.com,布布扣}
bubuko.com,布布扣catch
bubuko.com,布布扣{
bubuko.com,布布扣message = "文件导出异常!";
bubuko.com,布布扣}
bubuko.com,布布扣return message;
bubuko.com,布布扣}

[收藏]c#与word,布布扣,bubuko.com

[收藏]c#与word

标签:c   style   class   code   a   http   

原文地址:http://blog.csdn.net/penginpha/article/details/29204081

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!