小弟不才,没有深入研究,所以写程序时进度很慢。以后写新技术,必须先学习其基本思路和知识,这样“磨刀不误砍柴工”。先记录下来。以后好好搞懂其理论。
上代码:
第一版(有的地方中文字符转换出问题)
调用目录类
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using Microsoft.Office;using System.IO;using Microsoft.Office.Interop.Word;namespace CopyToWord{ public partial class Form1 : Form { FolderBrowserDialog folderBrowserDialog1=new FolderBrowserDialog (); OpenFileDialog openFileDialog1=new OpenFileDialog (); bool folderDefine = false; private Listlist = new List ();//文件的集合 public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { folderBrowserDialog1.RootFolder = Environment.SpecialFolder.MyComputer;//此对象的根目录必须是特殊的枚举目录 DialogResult result = folderBrowserDialog1.ShowDialog(); if(result==DialogResult.OK){ openFileDialog1.InitialDirectory = folderBrowserDialog1.SelectedPath;//文件的根目录是 用户选择的文件夹路径 textBox1.Text = folderBrowserDialog1.SelectedPath; start.Visible = true; } //OpenFileDialog openFileDialog1 = new OpenFileDialog(); //openFileDialog1.InitialDirectory = "E:\\";//初始的文件路径 //openFileDialog1.Filter = ".net file (*.cs;*.js;*.aspx;*.aspx.cs;*.aspx.designer.cs;*.ashx)|*cs;*.aspx;*.aspx.cs;*.aspx.designer.cs;*.ashx;*.js"; 初始的文件类型后缀名 //openFileDialog1.FilterIndex = 1;//索引默认为1 //openFileDialog1.RestoreDirectory = true; //if (openFileDialog1.ShowDialog() == DialogResult.OK) { // try { // } // catch { } //} } private void start_Click(object sender, EventArgs e) { state.Text = "读入中……"; System.Windows.Forms.Application.DoEvents(); FindFile(textBox1.Text); //foreach (FileInfo f in list) //{ // Console.WriteLine(f.FullName.ToString()); //} //state.Text = "完毕!"; //System.Windows.Forms.Application.DoEvents(); //Console.WriteLine("共输出" + list.Count.ToString()+"个文件"); //FileInfo f = list[0]; //cosw.CreateWordFile(f.FullName.ToString()); int i = 0; foreach (FileInfo f in list) { if (i == 0) { CodeSaveWord cosw = new CodeSaveWord(); cosw.CreateWordFile(f.FullName.ToString()); } else { CodeSaveWord cosw = new CodeSaveWord(); cosw.InsertFile(f.FullName.ToString()); break; } i++; } state.Text = "完毕"; System.Windows.Forms.Application.DoEvents(); } public void FindFile(string dir) //参数为指定的目录 { DirectoryInfo Dir = new DirectoryInfo(dir); foreach (FileInfo f in Dir.GetFiles("*.cs")) //查找文件 { list.Add(new FileInfo(Dir + "\\" + f.ToString())); //listBox1中填加文件名 } foreach (FileInfo f in Dir.GetFiles("*.aspx")) //查找文件 { list.Add(new FileInfo(Dir + "\\" + f.ToString())); //listBox1中填加文件名 } foreach (FileInfo f in Dir.GetFiles("*.aspx.cs")) //查找文件 { list.Add(new FileInfo(Dir + "\\" + f.ToString())); //listBox1中填加文件名 } foreach (FileInfo f in Dir.GetFiles("*.aspx.designer.cs")) //查找文件 { list.Add(new FileInfo(Dir + "\\" + f.ToString())); //listBox1中填加文件名 } foreach (FileInfo f in Dir.GetFiles("*.ashx")) //查找文件 { list.Add(new FileInfo(Dir + "\\" + f.ToString())); //listBox1中填加文件名 } foreach (FileInfo f in Dir.GetFiles("*.js")) //查找文件 { list.Add(new FileInfo(Dir + "\\" + f.ToString())); //listBox1中填加文件名 } //C#读取文件夹在指定目录及子目录下查找文件,在listBox1中列出子目录及文件 try { foreach(DirectoryInfo d in Dir.GetDirectories())//查找子目录 { FindFile(Dir + "\\" + d.ToString()); // listBox1.Items.Add(Dir+d.ToString()+"\\"); //listBox1中填加目录名 } } catch(Exception e) { MessageBox.Show(e.Message); } } }}
其外观类:
namespace CopyToWord { partial class Form1 { ////// 必需的设计器变量。 /// private System.ComponentModel.IContainer components = null;
////// 清理所有正在使用的资源。 /// /// 如果应释放托管资源,为 true;否则为 false。 protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); }
#region Windows 窗体设计器生成的代码
////// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// private void InitializeComponent() { this.label1 = new System.Windows.Forms.Label(); this.button1 = new System.Windows.Forms.Button(); this.textBox1 = new System.Windows.Forms.TextBox(); this.state = new System.Windows.Forms.Label(); this.start = new System.Windows.Forms.Button(); this.SuspendLayout(); // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(12, 59); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(65, 12); this.label1.TabIndex = 0; this.label1.Text = "程序根目录"; // // button1 // this.button1.Location = new System.Drawing.Point(195, 143); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 1; this.button1.Text = "选择程序根目录"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(14, 89); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(266, 21); this.textBox1.TabIndex = 2; // // state // this.state.AutoSize = true; this.state.Location = new System.Drawing.Point(106, 205); this.state.Name = "state"; this.state.Size = new System.Drawing.Size(41, 12); this.state.TabIndex = 3; this.state.Text = "未开始"; // // start // this.start.Location = new System.Drawing.Point(47, 142); this.start.Name = "start"; this.start.Size = new System.Drawing.Size(75, 23); this.start.TabIndex = 4; this.start.Text = "开始生成"; this.start.UseVisualStyleBackColor = true; this.start.Visible = false; this.start.Click += new System.EventHandler(this.start_Click); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(292, 266); this.Controls.Add(this.start); this.Controls.Add(this.state); this.Controls.Add(this.textBox1); this.Controls.Add(this.button1); this.Controls.Add(this.label1); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1; private System.Windows.Forms.Button button1; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Label state; private System.Windows.Forms.Button start; } }
输出类
using System;using System.Data;using System.IO;using Microsoft.Office.Interop.Word;using System.Text;namespace CopyToWord{ public class CodeSaveWord { private string path; //public string _Path { // set { value = path; } // get { return path; } //} /// /// 创建WORD文件及格式 /// /// /// public string CreateWordFile(string CheckedInfo) { string message = ""; try { Object Nothing = System.Reflection.Missing.Value; Directory.CreateDirectory("C:/CNSI"); //创建文件所在目录 string name = "CNSI_" + DateTime.Now.ToLongDateString() + ".doc"; object filename = "C://CNSI//" + name; //文件保存路径 path = "C://CNSI//" + name;//存储文件 object a = Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8;//文档编码模型 //创建Word文档 Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass(); Microsoft.Office.Interop.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 = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;//设置右对齐 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 Nothing);//移动焦点 WordApp.Selection.TypeParagraph();//插入段落 //文档中创建表格 Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, 12, 3, ref Nothing, ref Nothing); //设置表格样式 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(); 插入图片 //string FileName = @"c:\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 Nothing); WordDoc.Paragraphs.Last.Range.Text = "文档创建时间:" + DateTime.Now.ToString();//“落款” WordDoc.Paragraphs.Last.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight; WordDoc.SaveEncoding=Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8; WordDoc.Content.InsertFile(@CheckedInfo, ref Nothing, ref Nothing, ref Nothing, ref Nothing); //文件保存 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 a, 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; } /// /// 插入内容(文件) /// /// public void InsertFile(string CheckedInfo) { try { Object Nothing = System.Reflection.Missing.Value; string name = "CNSI_" + DateTime.Now.ToLongDateString() + ".doc"; object filename = "C://CNSI//" + name; //文件保存路径 path = "C://CNSI//" + name;//存储文件 object a = Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8;//文档编码模型 //创建Word文档 Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass(); Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Open2000(ref filename, ref Nothing, ref Nothing,ref Nothing, ref Nothing, ref Nothing,ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref a, ref Nothing); WordDoc.Activate(); WordDoc.SaveEncoding = Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8; WordApp.ActiveDocument.SaveEncoding = Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8; object bre = Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak; WordApp.ActiveDocument.Paragraphs.Last.Range.InsertBreak(ref bre); WordApp.ActiveDocument.Paragraphs.Last.Range.InsertFile(@CheckedInfo, ref Nothing, ref Nothing, ref Nothing, ref Nothing); //object tr = true; //object bre = Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak; //WordApp.Selection.InsertFile(@CheckedInfo, ref Nothing, ref Nothing, ref Nothing, ref Nothing); //WordApp.Selection.InsertBreak(ref bre); //WordApp.ActiveDocument.SaveAs(ref filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref a, ref Nothing, ref Nothing, ref Nothing, ref Nothing); //StreamReader objReader = new StreamReader(@CheckedInfo, Encoding.UTF8); //string sLine = ""; //while (sLine != null) //{ // sLine = objReader.ReadLine(); // if (sLine != null) // // WordDoc.Content.InsertAfter(sLine); // WordDoc.Content.InsertAutoText(); //} //object tr = true; //object bre = Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak; //WordApp.Selection.InsertFile(@CheckedInfo, ref Nothing, ref Nothing, ref Nothing, ref Nothing); //WordApp.Selection.InsertBreak(ref bre); //Microsoft.Office.Interop.Word.Range ra = WordApp.Selection.Range; //Set docNew = wrdApp.Documents.Add //'Insert a file //docNew.Range.InsertFile "C:\File1.DOC" // Dim wrdApp As New Word.Application //Dim docNew As Word.Document //Set docNew = wrdApp.Documents.Add //Dim myRange As Range //'Insert a file //docNew.Range.InsertFile FileName:="C:\File1.DOC" //'Set the cursor at the end of file //Set myRange = docNew.Paragraphs(docNew.Paragraphs.Count).Range //'Insert Page break //myRange.InsertBreak Type:=wdPageBreak //'Insert second file //myRange.InsertFile "C:\File2.DOC" //'Set the cursor at the end of file //Set myRange = docNew.Paragraphs(docNew.Paragraphs.Count).Range //'Insert Page break //myRange.InsertBreak Type:=wdPageBreak //'Insert Third file //myRange.InsertFile "C:\File3.DOC" //'save the document //docNew.SaveAs "C:\NewFile.doc" // Microsoft.Office.Interop.Word.Range endOfDoc;// object s = WordDoc.Bookmarks.Count;//endOfDoc.InsertFile(@CheckedInfo, ref Nothing, ref Nothing, ref Nothing, ref Nothing); //String oFilePath = @CheckedInfo; //Object oBookMark = "My_Custom_BookMark"; //Object oTrue = true; //Object oFalse = false; //WordDoc.Bookmarks.get_Item(ref oBookMark).Range.InsertFile(oFilePath, ref Nothing, ref oFalse, ref oFalse, ref oFalse); WordApp.ActiveDocument.SaveEncoding = Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8; WordApp.ActiveDocument.Save(); // WordApp.ActiveDocument.SaveAs(ref filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref a, ref Nothing, ref Nothing, ref Nothing, ref Nothing); WordDoc.Close(ref Nothing, ref Nothing, ref Nothing); WordApp.Quit(ref Nothing, ref Nothing, ref Nothing); } catch { } } }}
第二版(绝大多数中文字符转换没有问题)
FORM类。
//沐海代码。使用请保留。
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using Microsoft.Office;using System.IO;using Microsoft.Office.Interop.Word;namespace CopyToWord{ public partial class Form1 : Form { FolderBrowserDialog folderBrowserDialog1=new FolderBrowserDialog (); OpenFileDialog openFileDialog1=new OpenFileDialog (); bool folderDefine = false; private Listlist = new List ();//文件的集合 public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { folderBrowserDialog1.RootFolder = Environment.SpecialFolder.MyComputer;//此对象的根目录必须是特殊的枚举目录 DialogResult result = folderBrowserDialog1.ShowDialog(); if(result==DialogResult.OK){ openFileDialog1.InitialDirectory = folderBrowserDialog1.SelectedPath;//文件的根目录是 用户选择的文件夹路径 textBox1.Text = folderBrowserDialog1.SelectedPath; start.Visible = true; } //OpenFileDialog openFileDialog1 = new OpenFileDialog(); //openFileDialog1.InitialDirectory = "E:\\";//初始的文件路径 //openFileDialog1.Filter = ".net file (*.cs;*.js;*.aspx;*.aspx.cs;*.aspx.designer.cs;*.ashx)|*cs;*.aspx;*.aspx.cs;*.aspx.designer.cs;*.ashx;*.js"; 初始的文件类型后缀名 //openFileDialog1.FilterIndex = 1;//索引默认为1 //openFileDialog1.RestoreDirectory = true; //if (openFileDialog1.ShowDialog() == DialogResult.OK) { // try { // } // catch { } //} } private void start_Click(object sender, EventArgs e) { state.Text = "读入中……"; System.Windows.Forms.Application.DoEvents(); FindFile(textBox1.Text); //foreach (FileInfo f in list) //{ // Console.WriteLine(f.FullName.ToString()); //} //state.Text = "完毕!"; //System.Windows.Forms.Application.DoEvents(); //Console.WriteLine("共输出" + list.Count.ToString()+"个文件"); CodeSaveWord cosw = new CodeSaveWord(); //FileInfo f = list[0]; //cosw.CreateWordFile(f.FullName.ToString()); int i = 0; foreach (FileInfo f in list) { if (i == 0) { cosw.CreateWordFile(f.FullName.ToString()); i++; } else { cosw.InsertFile(f.FullName.ToString()); } } state.Text = "完毕"; System.Windows.Forms.Application.DoEvents(); list.Clear(); } public void FindFile(string dir) //参数为指定的目录 { DirectoryInfo Dir = new DirectoryInfo(dir); foreach (FileInfo f in Dir.GetFiles("*.cs")) //查找文件 { list.Add(new FileInfo(Dir + "\\" + f.ToString())); //listBox1中填加文件名 } foreach (FileInfo f in Dir.GetFiles("*.aspx")) //查找文件 { list.Add(new FileInfo(Dir + "\\" + f.ToString())); //listBox1中填加文件名 } foreach (FileInfo f in Dir.GetFiles("*.aspx.cs")) //查找文件 { list.Add(new FileInfo(Dir + "\\" + f.ToString())); //listBox1中填加文件名 } foreach (FileInfo f in Dir.GetFiles("*.aspx.designer.cs")) //查找文件 { list.Add(new FileInfo(Dir + "\\" + f.ToString())); //listBox1中填加文件名 } foreach (FileInfo f in Dir.GetFiles("*.ashx")) //查找文件 { list.Add(new FileInfo(Dir + "\\" + f.ToString())); //listBox1中填加文件名 } foreach (FileInfo f in Dir.GetFiles("*.js")) //查找文件 { list.Add(new FileInfo(Dir + "\\" + f.ToString())); //listBox1中填加文件名 } //C#读取文件夹在指定目录及子目录下查找文件,在listBox1中列出子目录及文件 try { foreach(DirectoryInfo d in Dir.GetDirectories())//查找子目录 { FindFile(Dir + "\\" + d.ToString()); // listBox1.Items.Add(Dir+d.ToString()+"\\"); //listBox1中填加目录名 } } catch(Exception e) { MessageBox.Show(e.Message); } } }}
转换WORD类
using System;using System.Data;using System.IO;using Microsoft.Office.Interop.Word;using System.Text;namespace CopyToWord{ public class CodeSaveWord { private string path; //public string _Path { // set { value = path; } // get { return path; } //} ////// 创建WORD文件及格式 /// /// ///public string CreateWordFile(string CheckedInfo) { string message = ""; try { Object Nothing = System.Reflection.Missing.Value; Directory.CreateDirectory("C:/CNSI"); //创建文件所在目录 string name = "CNSI_" + DateTime.Now.ToLongDateString() + ".doc"; object filename = "C://CNSI//" + name; //文件保存路径 path = "C://CNSI//" + name;//存储文件 object a = Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8;//文档编码模型 //创建Word文档 Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass(); Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing); WordDoc.Activate(); //添加页眉 WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView; WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader; WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("[页眉内容]"); WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;//设置右对齐 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 Nothing);//移动焦点 WordApp.Selection.TypeParagraph();//插入段落 //文档中创建表格 //Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, 12, 3, ref Nothing, ref Nothing); 设置表格样式 //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(); //插入图片 string FileName = @"c:\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 Nothing); //WordDoc.Paragraphs.Last.Range.Text = "文档创建时间:" + DateTime.Now.ToString();//“落款” //WordDoc.Paragraphs.Last.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight; WordDoc.SaveEncoding = Microsoft.Office.Core.MsoEncoding.msoEncodingUSASCII; //WordDoc.SaveEncoding=Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8; WordDoc.Content.InsertFile(@CheckedInfo, ref Nothing, ref Nothing, ref Nothing, ref Nothing); //文件保存 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 a, 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; } /// /// 嵌入内容(文件) /// /// public void InsertFile(string CheckedInfo) { try { Object Nothing = System.Reflection.Missing.Value; //枚举值,定义为空 object filename = path; //文件保存路径 object a = Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8;//文档编码模型 //创建Word文档 Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass(); //创建整个WORD操作的应用程序级别的容器对象-- WORD打开后。没有新建文档时的情况 Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Open2000(ref filename, ref Nothing, ref Nothing,ref Nothing, ref Nothing, ref Nothing,ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref a, ref Nothing); //应用程序打开WORD。 WordDoc.Activate();//让这个WORD对象成为 当前活动的 string a2a = WordDoc.SaveEncoding.ToString(); WordDoc.SaveEncoding = Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8;//文档编码模型 WordApp.ActiveDocument.SaveEncoding = Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8;//文档编码模型 object bre = Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;//分页标志 WordApp.ActiveDocument.Paragraphs.Last.Range.InsertBreak(ref bre); //找到应用程序中的活动文档的段落集合的最后位置,并插入一个分页标志。 WordApp.ActiveDocument.Paragraphs.Last.Range.InsertFile(@CheckedInfo, ref Nothing, ref Nothing, ref Nothing, ref Nothing); //找到应用程序中的活动文档的段落集合的最后位置(分页标志之后的位置),并插入文档。 WordApp.ActiveDocument.SaveEncoding = Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8; WordApp.ActiveDocument.Save(); //WordApp.ActiveDocument.SaveAs(ref filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref a, ref Nothing, ref Nothing, ref Nothing, ref Nothing); string aa= WordApp.ActiveDocument.SaveEncoding.ToString(); WordDoc.Close(ref Nothing, ref Nothing, ref Nothing); WordApp.Quit(ref Nothing, ref Nothing, ref Nothing); } catch { } } }}