//chen by 20101126------add------------
#region 动态生成Word文档并填充数据
/**/
/// <summary>
/// 动态生成Word文档并填充数据
/// </summary>
/// <returns>返回自定义信息</returns>
public static string CreateWordFile1()
{
string message = "";
try
{
Object oMissing = System.Reflection.Missing.Value;
string dir = System.Web.HttpContext.Current.Server.MapPath("");//首先在类库添加using System.web的引用
if (!Directory.Exists(dir + "\\file"))
{
Directory.CreateDirectory(dir + "\\file"); //创建文件所在目录
}
string name = DateTime.Now.ToLongDateString() + ".doc";
object filename = dir + "\\file\\" + name; //文件保存路径
//创建Word文档
Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
/**/
////添加页眉方法一:
//WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;
//WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
//WordApp.ActiveWindow.ActivePane.Selection.InsertAfter( "***有限公司" );//页眉内容
//添加页眉方法二:
if (WordApp.ActiveWindow.ActivePane.View.Type == Microsoft.Office.Interop.Word.WdViewType.wdNormalView || WordApp.ActiveWindow.ActivePane.View.Type == Microsoft.Office.Interop.Word.WdViewType.wdOutlineView)
{
WordApp.ActiveWindow.ActivePane.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdPrintView;
}
WordApp.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageHeader;
string sHeader = "页眉内容";
WordApp.Selection.HeaderFooter.LinkToPrevious = false;
WordApp.Selection.HeaderFooter.Range.Text = sHeader;
WordApp.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;
//WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;//设置右对齐
WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;//设置左对齐
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 oMissing);//移动焦点
WordApp.Selection.TypeParagraph();//插入段落
//文档中创建表格
Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, 12, 3, ref oMissing, ref oMissing);
//设置表格样式
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();
//插入图片
if (File.Exists(System.Web.HttpContext.Current.Server.MapPath("images//picture.jpg")))
{
string FileName = System.Web.HttpContext.Current.Server.MapPath("images//picture.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;//图片高度
}
//将图片设置为四周环绕型
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 oMissing);
WordDoc.Paragraphs.Last.Range.Text = "文档创建时间:" + DateTime.Now.ToString();//“落款”
WordDoc.Paragraphs.Last.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
//文件保存
WordDoc.SaveAs(ref filename, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
WordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
message = name + "文档生成成功";
}
catch
{
message = "文件导出异常!";
}
return message;
}
#endregion
#region 创建并打开一个空的word文档进行编辑
/**/
/// <summary>
/// 创建并打开一个空的word文档进行编辑
/// </summary>
public static void OpenNewWordFileToEdit()
{
object oMissing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Application WordApp;
Microsoft.Office.Interop.Word.Document WordDoc;
WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
WordApp.Visible = true;
WordDoc = WordApp.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
}
#endregion
object oMissing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Application WordApp;
Microsoft.Office.Interop.Word.Document WordDoc;
WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
#region 文档格式设置
WordApp.ActiveDocument.PageSetup.LineNumbering.Active = 0;//行编号
WordApp.ActiveDocument.PageSetup.Orientation = Microsoft.Office.Interop.Word.WdOrientation.wdOrientPortrait;//页面方向
WordApp.ActiveDocument.PageSetup.TopMargin = WordApp.CentimetersToPoints(float.Parse("2.54"));//上页边距
WordApp.ActiveDocument.PageSetup.BottomMargin = WordApp.CentimetersToPoints(float.Parse("2.54"));//下页边距
WordApp.ActiveDocument.PageSetup.LeftMargin = WordApp.CentimetersToPoints(float.Parse("3.17"));//左页边距
WordApp.ActiveDocument.PageSetup.RightMargin = WordApp.CentimetersToPoints(float.Parse("3.17"));//右页边距
WordApp.ActiveDocument.PageSetup.Gutter = WordApp.CentimetersToPoints(float.Parse("0"));//装订线位置
WordApp.ActiveDocument.PageSetup.HeaderDistance = WordApp.CentimetersToPoints(float.Parse("1.5"));//页眉
WordApp.ActiveDocument.PageSetup.FooterDistance = WordApp.CentimetersToPoints(float.Parse("1.75"));//页脚
WordApp.ActiveDocument.PageSetup.PageWidth = WordApp.CentimetersToPoints(float.Parse("21"));//纸张宽度
WordApp.ActiveDocument.PageSetup.PageHeight = WordApp.CentimetersToPoints(float.Parse("29.7"));//纸张高度
WordApp.ActiveDocument.PageSetup.FirstPageTray = Microsoft.Office.Interop.Word.WdPaperTray.wdPrinterDefaultBin;//纸张来源
WordApp.ActiveDocument.PageSetup.OtherPagesTray = Microsoft.Office.Interop.Word.WdPaperTray.wdPrinterDefaultBin;//纸张来源
WordApp.ActiveDocument.PageSetup.SectionStart = Microsoft.Office.Interop.Word.WdSectionStart.wdSectionNewPage;//节的起始位置:新建页
WordApp.ActiveDocument.PageSetup.OddAndEvenPagesHeaderFooter = 0;//页眉页脚-奇偶页不同
WordApp.ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = 0;//页眉页脚-首页不同
WordApp.ActiveDocument.PageSetup.VerticalAlignment = Microsoft.Office.Interop.Word.WdVerticalAlignment.wdAlignVerticalTop;//页面垂直对齐方式
WordApp.ActiveDocument.PageSetup.SuppressEndnotes = 0;//不隐藏尾注
WordApp.ActiveDocument.PageSetup.MirrorMargins = 0;//不设置首页的内外边距
WordApp.ActiveDocument.PageSetup.TwoPagesOnOne = false;//不双面打印
WordApp.ActiveDocument.PageSetup.BookFoldPrinting = false;//不设置手动双面正面打印
WordApp.ActiveDocument.PageSetup.BookFoldRevPrinting = false;//不设置手动双面背面打印
WordApp.ActiveDocument.PageSetup.BookFoldPrintingSheets = 1;//打印默认份数
WordApp.ActiveDocument.PageSetup.GutterPos = Microsoft.Office.Interop.Word.WdGutterStyle.wdGutterPosLeft;//装订线位于左侧
WordApp.ActiveDocument.PageSetup.LinesPage = 40;//默认页行数量
WordApp.ActiveDocument.PageSetup.LayoutMode = Microsoft.Office.Interop.Word.WdLayoutMode.wdLayoutModeLineGrid;//版式模式为“只指定行网格”
#endregion
#region 段落格式设定
WordApp.Selection.ParagraphFormat.LeftIndent = WordApp.CentimetersToPoints(float.Parse("0"));//左缩进
WordApp.Selection.ParagraphFormat.RightIndent = WordApp.CentimetersToPoints(float.Parse("0"));//右缩进
WordApp.Selection.ParagraphFormat.SpaceBefore = float.Parse("0");//段前间距
WordApp.Selection.ParagraphFormat.SpaceBeforeAuto = 0;//
WordApp.Selection.ParagraphFormat.SpaceAfter = float.Parse("0");//段后间距
WordApp.Selection.ParagraphFormat.SpaceAfterAuto = 0;//
WordApp.Selection.ParagraphFormat.LineSpacingRule = Microsoft.Office.Interop.Word.WdLineSpacing.wdLineSpaceSingle;//单倍行距
WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphJustify;//段落2端对齐
WordApp.Selection.ParagraphFormat.WidowControl = 0;//孤行控制
WordApp.Selection.ParagraphFormat.KeepWithNext = 0;//与下段同页
WordApp.Selection.ParagraphFormat.KeepTogether = 0;//段中不分页
WordApp.Selection.ParagraphFormat.PageBreakBefore = 0;//段前分页
WordApp.Selection.ParagraphFormat.NoLineNumber = 0;//取消行号
WordApp.Selection.ParagraphFormat.Hyphenation = 1;//取消段字
WordApp.Selection.ParagraphFormat.FirstLineIndent = WordApp.CentimetersToPoints(float.Parse("0"));//首行缩进
WordApp.Selection.ParagraphFormat.OutlineLevel = Microsoft.Office.Interop.Word.WdOutlineLevel.wdOutlineLevelBodyText;
WordApp.Selection.ParagraphFormat.CharacterUnitLeftIndent = float.Parse("0");
WordApp.Selection.ParagraphFormat.CharacterUnitRightIndent = float.Parse("0");
WordApp.Selection.ParagraphFormat.CharacterUnitFirstLineIndent = float.Parse("0");
WordApp.Selection.ParagraphFormat.LineUnitBefore = float.Parse("0");
WordApp.Selection.ParagraphFormat.LineUnitAfter = float.Parse("0");
WordApp.Selection.ParagraphFormat.AutoAdjustRightIndent = 1;
WordApp.Selection.ParagraphFormat.DisableLineHeightGrid = 0;
WordApp.Selection.ParagraphFormat.FarEastLineBreakControl = 1;
WordApp.Selection.ParagraphFormat.WordWrap = 1;
WordApp.Selection.ParagraphFormat.HangingPunctuation = 1;
WordApp.Selection.ParagraphFormat.HalfWidthPunctuationOnTopOfLine = 0;
WordApp.Selection.ParagraphFormat.AddSpaceBetweenFarEastAndAlpha = 1;
WordApp.Selection.ParagraphFormat.AddSpaceBetweenFarEastAndDigit = 1;
WordApp.Selection.ParagraphFormat.BaseLineAlignment = Microsoft.Office.Interop.Word.WdBaselineAlignment.wdBaselineAlignAuto;
#endregion
#region 字体格式设定
WordApp.Selection.Font.NameFarEast = "华文中宋";
WordApp.Selection.Font.NameAscii = "Times New Roman";
WordApp.Selection.Font.NameOther = "Times New Roman";
WordApp.Selection.Font.Name = "宋体";
WordApp.Selection.Font.Size = float.Parse("14");
WordApp.Selection.Font.Bold = 0;
WordApp.Selection.Font.Italic = 0;
WordApp.Selection.Font.Underline = Microsoft.Office.Interop.Word.WdUnderline.wdUnderlineNone;
WordApp.Selection.Font.UnderlineColor = Microsoft.Office.Interop.Word.WdColor.wdColorAutomatic;
WordApp.Selection.Font.StrikeThrough = 0;//删除线
WordApp.Selection.Font.DoubleStrikeThrough = 0;//双删除线
WordApp.Selection.Font.Outline = 0;//空心
WordApp.Selection.Font.Emboss = 0;//阳文
WordApp.Selection.Font.Shadow = 0;//阴影
WordApp.Selection.Font.Hidden = 0;//隐藏文字
WordApp.Selection.Font.SmallCaps = 0;//小型大写字母
WordApp.Selection.Font.AllCaps = 0;//全部大写字母
WordApp.Selection.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorAutomatic;
WordApp.Selection.Font.Engrave = 0;//阴文
WordApp.Selection.Font.Superscript = 0;//上标
WordApp.Selection.Font.Subscript = 0;//下标
WordApp.Selection.Font.Spacing = float.Parse("0");//字符间距
WordApp.Selection.Font.Scaling = 100;//字符缩放
WordApp.Selection.Font.Position = 0;//位置
WordApp.Selection.Font.Kerning = float.Parse("1");//字体间距调整
WordApp.Selection.Font.Animation = Microsoft.Office.Interop.Word.WdAnimation.wdAnimationNone;//文字效果
WordApp.Selection.Font.DisableCharacterSpaceGrid = false;
WordApp.Selection.Font.EmphasisMark = Microsoft.Office.Interop.Word.WdEmphasisMark.wdEmphasisMarkNone;
#endregion