[收藏]c#与word

public string CreateWordFile(string CheckedInfo)
{
string message = "";
try
{
Object Nothing = System.Reflection.Missing.Value;
Directory.CreateDirectory("C:/CNSI"); //创建文件所在目录
string name = "CNSI.doc";
object filename = "C://CNSI//" + name; //文件保存路径
//创建Word文档
Word.Application WordApp = new Word.ApplicationClass();
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 = Word.WdParagraphAlignment.wdAlignParagraphRight;//设置右对齐
WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;//跳出页眉设置

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

/*WordDoc.PageSetup.Orientation = Word.WdOrientation.wdOrientLandscape; //设置页面为纵向
            WordDoc.PageSetup.PageHeight = WordApp.CentimetersToPoints(21F);
            WordDoc.PageSetup.PageWidth = WordApp.CentimetersToPoints(29.7F);
            WordDoc.PageSetup.TopMargin = 57; //设置上边距
            WordDoc.PageSetup.BottomMargin = 57;//设置下边距
            WordDoc.PageSetup.LeftMargin = 57;//设置左边距
            WordDoc.PageSetup.RightMargin = 57;//设置右边距*/

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

//文档中创建表格
Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, 12, 3, ref Nothing, ref Nothing);
//设置表格样式
newTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleThickThinLargeGap;
newTable.Borders.InsideLineStyle = 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 = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中

            //填充表格内容
newTable.Cell(2, 1).Range.Text = "产品基本信息";
newTable.Cell(2, 1).Range.Font.Color = Word.WdColor.wdColorDarkBlue;//设置单元格内字体颜色
//合并单元格
newTable.Cell(2, 1).Merge(newTable.Cell(2, 3));
WordApp.Selection.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;

            //填充表格内容
newTable.Cell(3, 1).Range.Text = "品牌名称:";
newTable.Cell(3, 2).Range.Text = CheckedInfo;
//纵向合并单元格
newTable.Cell(3, 3).Select();//选中一行
object moveUnit = Word.WdUnits.wdLine;
object moveCount = 5;
object moveExtend = Word.WdMovementType.wdExtend;
WordApp.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend);
WordApp.Selection.Cells.Merge();
//插入图片
string FileName = @"C:\\1.jpg";//图片所在路径
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;//图片高度
//将图片设置为四周环绕型
Word.Shape s = WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
s.WrapFormat.Type = 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 = 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
{
message = "文件导出异常!";
}
return message;
}

[收藏]c#与word

时间: 2024-07-30 19:55:48

[收藏]c#与word的相关文章

在word中怎样设置常用字体指定快捷键?建议收藏!

当我们使用word时,可能因为工作的需要会对字体有所要求,我们使用word自己写东西,也会有自己喜欢的字体想要设置的.今天小编教教大家怎样在word中设置常用字的指定快捷键,有兴趣的小伙伴一起学学吧!No.1我们首先就是要打开一个word文档,这样就可以一步一步进行以下的操作了.No.2在打开word文档之后,我们需要点击一下文档页面左上角的[文件],然后找到并点击[选项]功能.在word选项弹出的功能框中,点击一下[自定义功能区].No.3在自定义功能区中,我们需要找到页面中的[自定义],在w

盒子 收藏帖子 转移-实现向网页自动填写用户名密码并自动点击登录按钮 完成全自动凳录

//实现向网页自动填写用户名密码并自动点击登录按钮 完成全自动凳录 Function FillForm(WebBrowser: TWebBrowser2; FieldName: String; Value: String): Boolean; Var i, j: Integer; FormItem: Variant; Begin Result := False; //no form on document If WebBrowser.OleObject.Document.all.tags('FO

[No.00000C]Word快捷键大全 Word2013/2010/2007/2003常用快捷键大全

Word对于我们办公来说,是不可缺少的办公软件,因为没有它我们可能无法进行许多任务.所以现在的文员和办公室工作的人,最基础的就是会熟悉的使用Office办公软件.在此,为提高大家Word使用水平,特为大家提供Word常用快捷键命令.以下所有Word快捷键适用于Word2003.Word2007.Word2010.Word2013等所有版本(小编辛苦收集了个把月,相信世上再也没有比这个还要全面的快捷键了),请大家好好收藏! Word快捷键大全 显示和使用窗口 切换到下一个窗口. Alt+Tab 切

Office文件的奥秘——.NET平台下不借助Office实现Word、Powerpoint等文件的解析

Office文件的奥秘——.NET平台下不借助Office实现Word.Powerpoint等文件的解析 分类: 技术 2013-07-26 15:38 852人阅读 评论(0) 收藏 举报 OfficePowerPointWord格式解析 转载http://www.cnblogs.com/mayswind/archive/2013/03/17/2962205.html [题外话] 这是2010年参加比赛时候做的研究,当时为了实现对Word.Excel.PowerPoint文件文字内容的抽取研究

一些免费收费api收藏

转载:http://blog.csdn.net/sdjianfei/article/details/53157334 一 .api 1.http://apistore.baidu.com/astore/servicesearch?word=%E5%85%8D%E8%B4%B9&isFree=1 百度api集市 2.https://developers.douban.com/wiki/?title=guide 豆瓣开放 ,仅向企业开放 3.http://open.taobao.com/?spm=a

VSTO:使用C#开发Excel、Word【14】

操作workbooks集合可从Application对象的Workbooks属性中获取的Workbooks集合包含当前在应用程序中打开的Workbook对象的集合. 它还具有用于管理开放工作簿,创建新工作簿以及打开现有工作簿文件的方法. 迭代开放式工作簿集合实现一个称为GetEnumerator的特殊方法,允许它们被迭代. 您不必直接调用GetEnumerator方法,因为C#中的foreach关键字使用此方法遍历一组工作簿. 有关使用foreach的示例,请参见清单5-8. 清单5-8  使用

微信运营必须收藏的软件工具网站合集

每一个微信运营者都需要一些微信运营辅助的软件.工具.网站等,比如文章排版网站,图片处理软件,运营交流学习网站等.为了让大家运营微信公众号更轻松,小编精心为大家收集整理了如下软件工具网站合集,方便微信运营者及时保存收藏查阅. 微信第三方服务平台 微盟:www.weimob.com 有赞商城:www.youzan.com 风铃:zhan.qq.com 聚信盒子:www.juxinbox.com 微俱聚:www.weijuju.com 搜狐快站:www.kuaizhan.com 囧易:easyorz.

C# Word 类库

C# Word 类库 2009-08-06 22:10 14292人阅读 评论(11) 收藏 举报 c#objectstring文档microsoftexcel using System;using System.Collections.Generic;using System.Text;using Microsoft.Office.Interop.Word;using System.IO;using System.Web;using System.Data;using System.Refle

.net core 导出word文档

Npoi导出word(Peanuts) 标签: C#npoi导出word合并列列样式 2015-10-05 22:16 2896人阅读 评论(0) 收藏 举报  分类: C#(13)  版权声明:本文为博主原创文章,未经博主允许不得转载. 一个项目,要做一个从数据库读取数据,然后导出到word,因为涉及到后台数据库的读取,决定用npoi来导出word. NPOI源码地址:http://npoi.codeplex.com/ NPOI 2.0 api文档: http://www.npoi.info/