1.如何打印一个word文档
你阅读MSDN(October2001)文章:Q184663 :UseMFCtoAutomateExcel&Create/FormataNewWorkbookQ186120 :CatchMicrosoftExcel97ApplicationEventsUsingVC++会对你有帮助。
如果要在你的程序中嵌入word等,可以使用htmlview实现。================事实上也就是:启动Word进程,然后调用Application.word的打印功能。
2.C#如何打印word模板
using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using Microsoft.Office;using Microsoft.Office.Core;using Microsoft.Office.Interop;using Microsoft.Office.Interop.Word;public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { string[] demostr ={ "测试数据1", "afsdasd", "fasdfdf", "sdfasdfee33", "asdfddeegadg", "fee都受到" }; string url = "http://" + this.Request.Url.Authority + "/demo/全国工业产品生产许可证申请登记表.doc"; string urlreadword = "/demo/Officebak/" + SetDocumentBookmarkData(url, demostr, Guid.NewGuid().ToString()); Page.RegisterStartupScript("msg", ""); } //设定标签的数据 public string SetDocumentBookmarkData(string FileName, string [] demostr, string caseid) { //打开文档 Microsoft.Office.Interop.Word.Document wDoc = null; Microsoft.Office.Interop.Word.Application wApp = null; this.OpenWordDoc(FileName, ref wDoc, ref wApp); object oEndOfDoc = "\\endofdoc"; object missing = System.Reflection.Missing.Value; //设定标签数据 System.Collections.IEnumerator enu = wApp.ActiveDocument.Bookmarks.GetEnumerator(); string[] strbook = new string[demostr.Length ]; int i = 0; Microsoft.Office.Interop.Word.Bookmark bk = null; while (enu.MoveNext()) { bk = (Microsoft.Office.Interop.Word.Bookmark)enu.Current; if (bk.Name.ToString().Trim() != "Table") { strbook[i] = bk.Name.ToString(); i++; } } object tempobject = null; int length = 0; for (i = 0; i < strbook.length;="" i++)="" {="" tempobject="strbook[i].ToString();" if="" (wapp.activedocument.bookmarks.exists(strbook[i].tostring()))="" {="" wapp.activedocument.bookmarks.get_item(ref="" tempobject).select();="" wapp.selection.text="demostr[i].ToString" ();="" }="" }="" microsoft.office.interop.word.table="" wordtablexsoft="wDoc.Tables[1];" inserttabletodata(wordtablexsoft,="" ref="" wdoc,="" ref="" wapp);="" 收尾工作="" object="" o="null;" string="" guid="System.Guid.NewGuid().ToString();" string="" guid="caseid;" object="" sfilename="Server.MapPath("/demo/Officebak/"" +="" guid="" +="" ".doc");="" if="" (wdoc.saveformat="=" (int)microsoft.office.interop.word.wdsaveformat.wdformatdocument)="" {="" wdoc.application.activedocument.saveas(ref="" sfilename,="" 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);="" }="" wdoc.close(ref="" missing,="" ref="" missing,="" ref="" missing);="" wapp.quit(ref="" missing,="" ref="" missing,="" ref="" missing);="" if="" (wdoc="" !="null)" {="" system.runtime.interopservices.marshal.releasecomobject(wdoc);="" wdoc="null;" }="" if="" (wapp="" !="null)" {="" system.runtime.interopservices.marshal.releasecomobject(wapp);="" wapp="null;" }="" gc.collect();="" return="" guid="" +="" ".doc";="" }="" #region="" 打开word模板和word文件="" public="" static="" void="" openworddot()="" {="" 不建议用非标准doc="" 此处方法="" 略="" }="" private="" void="" openworddoc(string="" filename,="" ref="" microsoft.office.interop.word.document="" wdoc,="" ref="" microsoft.office.interop.word.application="" wapp)="" {="" if="" (filename="=" "")="" return;="" microsoft.office.interop.word.document="" thisdocument="null;" microsoft.office.interop.word.formfields="" formfields="null;" microsoft.office.interop.word.application="" thisapplication="new" microsoft.office.interop.word.applicationclass();="" thisapplication.visible="true;" thisapplication.caption="" ;="" thisapplication.options.checkspellingasyoutype="false;" thisapplication.options.checkgrammarasyoutype="false;" object="" filename="FileName;" object="" confirmconversions="false;" object="" readonly="true;" object="" addtorecentfiles="false;" object="" passworddocument="System.Type.Missing;" object="" passwordtemplate="System.Type.Missing;" object="" revert="System.Type.Missing;" object="" writepassworddocument="System.Type.Missing;" object="" writepasswordtemplate="System.Type.Missing;" object="" format="System.Type.Missing;" object="" encoding="System.Type.Missing;" object="" visible="System.Type.Missing;" object="" openandrepair="System.Type.Missing;" object="" documentdirection="System.Type.Missing;" object="" noencodingdialog="System.Type.Missing;" object="" xmltransform="System.Type.Missing;" try="" {="" microsoft.office.interop.word.document="" worddoc="thisApplication.Documents.Open(ref" filename,="" ref="" confirmconversions,="" ref="" readonly,="" ref="" addtorecentfiles,="" ref="">
3.怎么做一个word模板,用java调用打印功能
1、首先新建一个空白文档,并另存为“Normal.dotm(Word2007及以后版本)Normal.dot(Word2003-2007版本)”。
2、进行需要的模板设置,对字体、段落、页眉页脚,页面边距等进行设置。3、对字体、段落的设置:在空白的新文档中,右击进行对字体的设置,在设置完成后,点击“默认”按钮。
4、对页眉页脚,页面边距:同样在菜单栏,选择插入"页眉页脚"进行设置,设置后点击“默认”按钮,并同时设置“页面边距”并点击“默认”按钮。5、在全面进行模板设置后,需要保存,在操作中会出现对一些“默认的设置”如果所示,要选择“是”,“你准备将默认的字体更改为。
..,是否将此更改应用于所有基于NORMAL模板的新文档?”6、把刚才保存的模板,复制起来,在打开“C:\Users\Administrator\AppData\Roaming\Microsoft\Templates”替换原有的Normal.dotm或Normal.dot,关闭。7、测试效果:打开一个Word文档,并按下“CTRL+N”新建一个空白文档,任意输入内容,可以看到是刚才设置的模板。
java实现调用默认打印机打印文档(以D盘zhidao.txt文档为例),代码如下:import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import javax.print.Doc; import javax.print.DocFlavor; import javax.print.DocPrintJob; import javax.print.PrintException; import javax.print.PrintService; import javax.print.PrintServiceLookup; import javax.print.ServiceUI; import javax.print.SimpleDoc; import javax.print.attribute.DocAttributeSet; import javax.print.attribute.HashDocAttributeSet; import javax.print.attribute.HashPrintRequestAttributeSet; import javax.print.attribute.PrintRequestAttributeSet;/** * 调用打印机功能 * @author Administrator**/ public class PrintDemoII { public static void main(String[] args) { DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE; PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); PrintService []pservices = PrintServiceLookup.lookupPrintServices(flavor, aset); PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService(); PrintService service = ServiceUI.printDialog(null, 200, 200, pservices, defaultService, flavor, aset); if(service != null){ try { DocPrintJob pj =service.createPrintJob(); FileInputStream fis = new FileInputStream("D:"+ File.separator +"zhidao.txt");//打印D盘zhidao.txt文档。DocAttributeSet das = new HashDocAttributeSet(); Doc doc = new SimpleDoc(fis, flavor, das); pj.print(doc, aset); } catch (FileNotFoundException fe) { fe.printStackTrace(); } catch (PrintException e) { e.printStackTrace(); } }else { System.out.println("打印失败"); } } }。
4.c#如何将word文档打印为pdf
今天,用实例代码,说明.NET是如何打印WORD、EXCEL等OFFICE文件,以及PDF文件的。
采用指定的打印机打印OFFICE文件此方法又分为 “显示相应的程序窗口” 和 “不显示相应的程序窗口”两种方式。(1) 显示WORD、EXCEL等程序窗口采用操作系统自身的自动识别模式的打印,此方法实际适用于N多种文件,并不限于WORD,EXCEL,PDF之类的,但是这种方法,有一个缺陷,就是:对于某些类型的文档,如WORD,EXCEL,PDF等,打印时,会有相应的程序窗口一闪而过。
实现代码如下:System.Diagnostics.Process p = new System.Diagnostics.Process();//不现实调用程序窗口,但是对于某些应用无效p.StartInfo.CreateNoWindow = true;p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;//采用操作系统自动识别的模式p.StartInfo.UseShellExecute = true;//要打印的文件路径,可以是WORD,EXCEL,PDF,TXT等等p.StartInfo.FileName = @"d:\a.doc";//指定执行的动作,是打印,即print,打开是 openp.StartInfo.Verb = "print";//开始p.Start();此种方法,代码简单,性能好,可靠稳定。此种方式,如果要指定打印机,则只能利用设置默认打印机的方式来实现。
C#设置系统默认打印机的实现方法,见 “C#获取和设置系统的默认打印机” 一文添加了指定打印机功能的代码如下:System.Diagnostics.Process p = new System.Diagnostics.Process();//不现实调用程序窗口,但是对于某些应用无效p.StartInfo.CreateNoWindow = true;p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;//采用操作系统自动识别的模式p.StartInfo.UseShellExecute = true;//要打印的文件路径p.StartInfo.FileName = @"d:\a.doc";//指定执行的动作,是打印,即print,打开是 openp.StartInfo.Verb = "print";//获取当前默认打印机//string defaultPrinter = GetDefaultPrinter();//将指定的打印机设为默认打印机SetDefaultPrinter("指定的打印机");//开始打印p.Start();//等待十秒p.WaitForExit(10000);//将默认打印机还原SetDefaultPrinter(defaultPrinter);(2) 不显示WORD、EXCEL等程序窗口此种方式,使用.NET调用COM的方式来实现,利用COM对象本身的特性来设置可见性和打印机使用此方法前,需要先添加Office的COM引用,这里略过。//要打印的文件路径object wordFile = @"d:\a.doc";object oMissing = Missing.Value;//自定义object类型的布尔值object oTrue = true;object oFalse = false;object doNotSaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;//定义WORD Application相关Word.Application appWord = new Word.Application();//WORD程序不可见appWord.Visible = false;//不弹出警告框appWord.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone; //先保存默认的打印机string defaultPrinter = appWord.ActivePrinter;//打开要打印的文件Word.Document doc = appWord.Documents.Open( ref wordFile, ref oMissing, ref oTrue, ref oFalse, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);//设置指定的打印机appWord.ActivePrinter = "指定打印机的名字";//打印doc.PrintOut( ref oTrue, //此处为true,表示后台打印 ref oFalse, 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, ref oMissing );//打印完关闭WORD文件doc.Close(ref doNotSaveChanges, ref oMissing, ref oMissing);//还原原来的默认打印机appWord.ActivePrinter = defaultPrinter;//退出WORD程序appWord.Quit(ref oMissing, ref oMissing, ref oMissing);doc = null;appWord = null;此方法,于COM交互,性能有些损失,要注意COM对象的释放,以及异常控制,此处忽略。
5.我怎么能用它在C 打开Word文档
基本步骤(1)创建)一个 MFC 的程序工程。
注意:在VC中对WORD进行操作需要在MFC AppWizard - Step 2 of4中的Automaiton选项上打上勾。
(2)Ctrl+W 执行 ClassWizard(本文按照 VC6 操作,示例程序是在VC6 下编写测试的)。
(3)Add Class。\From a type Library。 在 Office目录中,找到想使用的类型库。(我使用的是 Office2003,其Word 的类型库文件,保存在 E:\ProgramFiles\Microsoft Office\Office12\MSWOR.OLB)。
(4)选择类型库文件后,在弹出的对话窗中继续选择要添加的类。具体选择什么类,要看你将来在程序中打算调用什么功能。当然,也可以不用考虑这么多,用鼠标和Shift键配合,全部选择也可以。
(5)初始化COM。方法一,找到App的InitInstance()函数,在其中添加AfxOleInit()函数的调用;方法二,在需要调用COM功能的地方 CoInitialize(NULL),调用完毕后CoUninitialize()。
(6)在你需要调用 Office 功能函数的 cpp 文件中 #include<atlbase.h>; //为了方便操作 VARIANT 类型变量,使用 CComVariant 模板类 #include "文件名.h" //具体的头文件名,是由装载类型库的文件名决定的,如MSWORD。
示例程序:
\//word应用程序 _Application app; //初始化连接 app.CreateDispatch("word.Application"); Documents doc; CComVarianta(_T(strWord)),b(false),c(0),d(true),aa(0),bb(1); _Document doc1; doc.AttachDispatch(app.GetDocuments()); doc1.AttachDispatch(doc.Add(&a,&b,&c,&d)); Range range; //求出文档的所选区域 range=doc1.GetContent();//取出文件内容 str=range.GetText(); m_richedit.SetWindowText(str); //关闭 app.Quit(&b,&c,&c); //释放环境 app.ReleaseDispatch();
转载请注明出处51数据库 » c调用word模板打印