1. C#在调用Interop.Word时,程序运行时会启动word.怎么才能让WORD
1)在项目中添加引用 Microsoft.Office.Interop.Word
2)代码:
using System;
using System.Windows.Forms;
using MyWord = Microsoft.Office.Interop.Word; //为名称空间起一个别名:MyWord
……
object missing = System.Reflection.Missing.Value;
// 打开WORD
MyWord.Application app = new new MyWord.Application();
// 禁止Word界面启动 !!!
app.Visible = false;
// 打开文档
MyWord.Document doc =app.Documents.Open(ref file, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing);
……
2. ASP.NET 打印help类拜托各位大神
using System; using System.Data; using System.Configuration; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; namespace InfoSysWeb.ECont { public class WordHelper { private Microsoft.Office.Interop.Word.Document wDoc = null; private Microsoft.Office.Interop.Word.Application wApp = null; public Microsoft.Office.Interop.Word.Document Document { get { return wDoc; } set { wDoc = value; } } public Microsoft.Office.Interop.Word.Application Application { get { return wApp; } set { wApp = value; } } #region 从模板创建新的Word文档 /// /// 从模板创建新的Word文档 /// /// 模板文件名 /// public bool CreateNewWordDocument(string templateName) { try { return CreateNewWordDocument(templateName, ref wDoc, ref wApp); } catch (Exception ex) { throw ex; } } #endregion #region 从模板创建新的Word文档,并且返回对象Document,Application /// /// 从模板创建新的Word文档, /// /// 模板文件名 /// 返回的Word.Document对象 /// 返回的Word.Application对象 /// public static bool CreateNewWordDocument(string templateName, ref Microsoft.Office.Interop.Word.Document wDoc, ref Microsoft.Office.Interop.Word.Application WApp) { Microsoft.Office.Interop.Word.Document thisDocument = null; Microsoft.Office.Interop.Word.Application thisApplication = new Microsoft.Office.Interop.Word.ApplicationClass(); thisApplication.Visible = false; thisApplication.Caption = ""; thisApplication.Options.CheckSpellingAsYouType = false; thisApplication.Options.CheckGrammarAsYouType = false; Object Template = templateName;// Optional Object. The name of the template to be used for the new document. If this argument is omitted, the Normal template is used. Object NewTemplate = false;// Optional Object. True to open the document as a template. The default value is False. Object DocumentType = Microsoft.Office.Interop.Word.WdNewDocumentType.wdNewBlankDocument; // Optional Object. Can be one of the following WdNewDocumentType constants: wdNewBlankDocument, wdNewEmailMessage, wdNewFrameset, or wdNewWebPage. The default constant is wdNewBlankDocument. Object Visible = true;//Optional Object. True to open the document in a visible window. If this value is False, Microsoft Word opens the document but sets the Visible property of the document window to False. The default value is True. try { Microsoft.Office.Interop.Word.Document wordDoc = thisApplication.Documents.Add(ref Template, ref NewTemplate, ref DocumentType, ref Visible); thisDocument = wordDoc; wDoc = wordDoc; WApp = thisApplication; return true; } catch (Exception ex) { string err = string.Format("创建Word文档出错,错误原因:{0}", ex.Message); throw new Exception(err, ex); } } #endregion #region 文档另存为其他文件名 /// /// 文档另存为其他文件名 /// /// 文件名 /// Document对象 public bool SaveAs(string fileName) { try { return SaveAs(fileName, wDoc); } catch (Exception ex) { throw ex; } } #endregion。
3. C#捕获关闭office软件的事件
//using Microsoft.Office.Interop.Word;
private void Form1_Load(object sender, EventArgs e)
{
//启动一个空word
Microsoft.Office.Interop.Word.Application wapp = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document adoc = new Microsoft.Office.Interop.Word.Document();
wapp.Visible = true;
object miss = System.Reflection.Missing.Value;
adoc = wapp.Documents.Add(ref miss, ref miss, ref miss, ref miss);
//关键!
wapp.DocumentBeforeClose += new ApplicationEvents4_DocumentBeforeCloseEventHandler(wordApp_DocumentBeforeClose);
wapp = null;
}
private void wordApp_DocumentBeforeClose(Document Doc, ref bool Cancel)
{
//当关闭word的时候
MessageBox.Show("ok");
}
4. 如何C#操作word模板生成word文档并打开
codeproject上有很多操作word的C#代码:顺便给你几个链接:/KB/office/Word_Automation.aspx /KB/office/Word2007Automation.aspx /KB/office/csautomateword.aspx private void met_word_automation() { try { // Declaring the object variables we will need later object varFileName = "c:\temp\doc.docx"; object varFalseValue = false; object varTrueValue = true; object varMissing = Type.Missing; string varText; // Create a reference to Microsoft Word application Microsoft.Office.Interop.Word.Application varWord = new Microsoft.Office.Interop.Word.Application(); // Creates a reference to a Word document Microsoft.Office.Interop.Word.Document varDoc = varWord.Documents.Open(ref varFileName, ref varMissing, ref varFalseValue, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing); // Activate the document varDoc.Activate(); // Change the 1st sentence of the document varText = "Altered sentence nr. 1"; varDoc.Sentences[0].Text = varText; // Change the 3rd sentence of the document varText = "Altered sentence nr. 3"; varDoc.Sentences[2].Text = varText; // Save the document varDoc.Save(); // Show the Microsoft Word window with our document on it varWord.Visible = true; // Call the print dialog in Word Microsoft.Office.Interop.Word.Dialog varDlg = varWord.Application.Dialogs[ Microsoft.Office.Interop.Word.WdWordDialog.wdDialogFilePrint]; varDlg.Show(ref varMissing); // Print the document varDoc.PrintOut(ref varTrueValue, ref varFalseValue, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varFalseValue, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing); // Send mail with this document as an attachment varDoc.SendMail(); } catch (Exception varE) { MessageBox.Show("Error:\n" + varE.Message, "Error message"); } } You need to create references to this .NET namespace in order to use the above code: Microsoft.Office.Interop.Word。
5. asp.net中,把word文档转为PDF格式文件的问题
C#编程,将Word转PDF,该方法有一定弊端,但是比起网路上的其他方法来说,还算比较好的,弊端是需要客户机上安装有WORD 2007或更高的版本。
1、添加引用: Microsoft.Office.Interop.Word版本12.0.0.0; 2、在开头添加命名空间引用:using Microsoft.Office.Interop.Word; 3、具体实现方法如下: //Word转换e799bee5baa6e79fa5e98193e78988e69d8331333332633030成pdf ///
6. ASP.NET 打印help类
using System;using System.Data;using System.Configuration;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq; namespace InfoSysWeb.ECont{ public class WordHelper { private Microsoft.Office.Interop.Word.Document wDoc = null; private Microsoft.Office.Interop.Word.Application wApp = null; public Microsoft.Office.Interop.Word.Document Document { get { return wDoc; } set { wDoc = value; } } public Microsoft.Office.Interop.Word.Application Application { get { return wApp; } set { wApp = value; } } #region 从模板创建新的Word文档 ///
7. C# Microsoft.Office.Interop.Word怎么获取页数和字数
using MSWord = Microsoft.Office.Interop.Word;
//方法内容
private MSWord.Application wordApp; //Word应用程序变量
private MSWord.Document wordDoc; //Word文档变量
private Object Nothing = Missing.Value;
//初始化
wordApp = new MSWord.ApplicationClass();
wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
// 打开Word
object FileName = strPath;
object readOnly = false;
object isVisible = true;
wordDoc = wordApp.Documents.Open(ref FileName, ref Nothing, ref readOnly,
ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
ref Nothing, ref Nothing, ref Nothing, ref isVisible, ref Nothing,
ref Nothing, ref Nothing, ref Nothing);
// 计算Word文档页数
MSWord.WdStatistic stat = MSWord.WdStatistic.wdStatisticPages;
int num = wordDoc.ComputeStatistics(stat, ref Nothing);
int wordNum=wordDoc.Characters.Count;//文档字数
8. 我要用C#,NET 导出一个word文档
楼上的 你能不能改写下这个例子啊? 我这个只能导出字符 不能导出表格 string title = "个人信息"; object titleLengh = title.Length; string first = "\n 公司最近需要利用C#对项目进行编程,其" + "中存在一个功能就是可自动生成WORD文档,但一直以来都" + "找不到什么好办法,无奈之下,只有自已学着写一个了."; object firstLengh = first.Length; string second = "\n 如果能真正生成WORD文档的好处有:"; object secondLengh = second.Length; string third = "\n1、根据数据库信息自动生成文档;"; object thirdLengh = third.Length; string forth = "\n2、免去书写文档之苦;"; object forthLengh = forth.Length; string fifth = "\n3、可以通过邮件方式传出文档。
"; object fifthLengh = fifth.Length; object missing; object zero = 0; object c = 10; object one = 1; object two = 2; object tree = 3; object four = 4; object five = 5; object six = 6; object seven = 7; object eight = 8; object nine = 9; object ten = 10; Object Nothing = System.Reflection.Missing.Value; Microsoft.Office.Interop.Word.ApplicationClass wa = new Microsoft.Office.Interop.Word.ApplicationClass(); Microsoft.Office.Interop.Word.Document WordDoc = wa.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing); missing = System.Reflection.Missing.Value; wa.Visible = true; wa.Documents.Add(ref missing, ref missing, ref missing, ref missing); Microsoft.Office.Interop.Word.Range myRange = wa.ActiveDocument.Range(ref zero, ref zero); object r = myRange; Microsoft.Office.Interop.Word.Paragraph p = wa.ActiveDocument.Paragraphs.Add(ref r); p.Range.InsertBefore(title); //p.Range.Font.Size = 1; Microsoft.Office.Interop.Word.Range titleRange = wa.ActiveDocument.Range(ref zero, ref titleLengh); //titleRange.Font.Size = 1; titleRange.Font.Name = "幼圆"; titleRange.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorBlue; //MessageBox.Show("NO.1"); //titleRange.Paragraphs.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight; Microsoft.Office.Interop.Word.Range firstR = wa.ActiveDocument.Paragraphs[1].Range;//.Item(2).Range; Microsoft.Office.Interop.Word.Table table = WordDoc.Tables.Add(titleRange, 3, 3, ref Nothing, ref Nothing); r = firstR; p = wa.ActiveDocument.Paragraphs.Add(ref r); firstR.Font.Size = 40; firstR.Paragraphs.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;//在表格第一单元格中添加自定义的文字内容 table.Cell(1, 1).Range.Text = "lllll"; firstR.InsertAfter(first);//firstR.InsertParagraphAfter(); //firstR=wa.ActiveDocument.Paragraphs.Item(3).Range; firstR.InsertAfter(second); firstR.InsertAfter(third); firstR.InsertAfter(forth); firstR.InsertAfter(fifth); Context.Response.Write("成功");。
转载请注明出处51数据库 » interopworddocumen
仰望丶陨昕10991556