一、c#,winForm嵌入word并能操作文档
在 Visual C# .NET 中新建一个 Windows 应用程序项目。
默认情况下会创建 Form1。 在工具菜单上,单击自定义工具箱以打开自定义工具箱对话框。
在 COM 组件选项卡上,添加一个对 Microsoft WebBrowser 的引用。单击确定,将 WebBrowser 控件添加到 Windows 窗体工具箱。
WebBrowser 控件会显示出来,并且在工具箱中带有 Explorer(资源管理器)字样。 使用该工具箱向 Form1 添加一个 WebBrowser 控件、一个 OpenFileDialog 控件和一个 CommandButton 按钮。
这就会向 Form1 类添加 AxWebBrowser1、OpenFileDialog1 和 Button1 成员变量。 在 Form1 上,双击 button1。
这就会向 Form1 添加 Button1_Click 事件。 在 Form1 的代码窗口中,向列表添加以下命名空间:using System.Reflection; 如下所示在 Form1 类中定义一个私有成员:private Object oDocument; 在 Form1 类的 InitializeComponent 方法的末尾,添加以下代码以处理 Form1_Load、Form1_Closed 和 axWebBrowser1_NavigateComplete2 事件:this.axWebBrowser1.NavigateComplete2 += new AxSHDocVw.DWebBrowserEvents2_NavigateComplete2EventHandler(this.axWebBrowser1_NavigateComplete2); this.Load += new System.EventHandler(this.Form1_Load); this.Closed += new System.EventHandler(this.Form1_Closed); 将下面的代码private void button1_Click(object sender, System.EventArgs e) { } 替换为: private void button1_Click(object sender, System.EventArgs e) { String strFileName; //Find the Office document. openFileDialog1.FileName = ""; openFileDialog1.ShowDialog(); strFileName = openFileDialog1.FileName; //If the user does not cancel, open the document. if(strFileName.Length != 0) { Object refmissing = System.Reflection.Missing.Value; oDocument = null; axWebBrowser1.Navigate(strFileName, ref refmissing , ref refmissing , ref refmissing , ref refmissing); } } public void Form1_Load(object sender, System.EventArgs e) { button1.Text = "Browse"; openFileDialog1.Filter = "Office Documents(*.doc, *.xls, *.ppt)|*.doc;*.xls;*.ppt" ; openFileDialog1.FilterIndex = 1; } public void Form1_Closed(object sender, System.EventArgs e) { oDocument = null; } public void axWebBrowser1_NavigateComplete2(object sender, AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event e) { //Note: You can use the reference to the document object to // automate the document server. Object o = e.pDisp; oDocument = o.GetType().InvokeMember("Document",BindingFlags.GetProperty,null,o,null); Object oApplication = o.GetType().InvokeMember("Application",BindingFlags.GetProperty,null,oDocument,null); Object oName = o.GetType().InvokeMember("Name",BindingFlags.GetProperty ,null,oApplication,null); MessageBox.Show("File opened by: " + oName.ToString() ); } 按 F5 键运行该项目。
单击浏览后,会出现打开对话框,您可以使用该对话框浏览到 Word 文档、Excel 工作表或 PowerPoint 演示文稿。选择任一文件,然后单击打开。
文档在 WebBrowser 控件内打开,并出现一个显示有 Office 文档服务器名称的消息框。
二、winform读取word文件
在Form里加一个button按钮和一个richtextbox,给按钮加一个Click事件private void button1_Click(object sender, EventArgs e) { object filepath= "D:\\Debugtest.doc"; openWord(filepath); }//把Word文档内容取出来放到richtextBox里 private void openWord(object SPath) { object file = SPath; object nullobj = System.Reflection.Missing.Value; Word.Document doc = myWordApp.Documents.Open(ref file, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj,ref nullobj,ref nullobj,ref nullobj,ref nullobj); doc.ActiveWindow.Selection.WholeStory(); doc.ActiveWindow.Selection.Copy(); IDataObject data = Clipboard.GetDataObject(); this.richTextBox1.Text = data.GetData(DataFormats.Text).ToString(); doc.Close(ref nullobj, ref nullobj, ref nullobj);}。
三、.net winfrom程序怎么把一个页面导出存为word文档
供参考: Response.Clear(); Response.Buffer= true; Response.AppendHeader("Content-Disposition","attachment;filename="+DateTime.Now.ToString("yyyyMMdd")+".xls"); Response.ContentEncoding=System.Text.Encoding.UTF8; Response.ContentType = "vnd.ms-excel/msword"; this.EnableViewState = false;。
四、C# winform 下导出到窗体datagridview到word代码~
这个过程可耻下场参考一下:
#region 使用Interop.Word.dll将DataGridView导出到Word
/// <summary>
/// 使用Interop.Word.dll将DataGridView导出到Word
/// </summary>
/// <param name="dGV"></param>
/*保会通财务软件公司*/
public static void DataGridViewExportToWord_WordDll(DataGridView dGV)
{
Word.Document mydoc = new Word.Document();//实例化Word文档对象
Word.Table mytable;//声明Word表格
Word.Selection mysel;//声明Word选区
Object myobj;
if (dGV.Rows.Count == 0)
return;
//建立Word对象
Word.Application word = new Word.Application();
myobj = System.Reflection.Missing.Value;
mydoc = word.Documents.Add(ref myobj, ref myobj, ref myobj, ref myobj);
word.Visible = true;
mydoc.Select();
mysel = word.Selection;
//将数据生成Word表格文件
mytable = mydoc.Tables.Add(mysel.Range, dGV.RowCount, dGV.ColumnCount, ref myobj, ref myobj);
//设置列宽
mytable.Columns.SetWidth(80, Word.WdRulerStyle.wdAdjustNone);
//输出列标题数据
for (int i = 0; i < dGV.ColumnCount; i++)
{
mytable.Cell(1, i + 1).Range.InsertAfter(dGV.Columns[i].HeaderText);
}
//输出控件中的记录
for (int i = 0; i < dGV.RowCount - 1; i++)
{
for (int j = 0; j < dGV.ColumnCount; j++)
{
mytable.Cell(i + 2, j + 1).Range.InsertAfter(dGV[j, i].Value.ToString());
}
}
}
#endregion
五、如何在C#中读取Word文档
1.新建一个winform工程,添加两个button控件和一个textbox控件。
2.添加引用com文件Microsoft word 11.0 Object Library
并在代码中添加命名空间using Word //注意首字母大写
3.定义全局变量
//创建word
_Application app=new Word.Application();
//创建word文档
_Document doc=null;
注意word和word文档是不同的
4.通过文件打开对话框获取word文件所在位置,代码如下
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "word文件|*.doc";
if (dlg.ShowDialog() == DialogResult.OK)
{
string fileName = dlg.FileName;
}
打开word,代码如下
object unknow = Type.Missing;
doc = app.Documents.Open(ref fileName,
ref unknow, ref unknow, ref unknow, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow, ref unknow, ref unknow);
5.读取word文档
string temp = doc.Paragraphs[i].Range.Text.Trim();//变量i为第i段
textBox1.Text = temp; //将第i段内容复制到textbox中
转载请注明出处51数据库 » winform导入word