110,568
社区成员
发帖
与我相关
我的任务
分享
添加引用:COM,MS office11.0Object Library及Interop.Excel
设置默打印机:Microsoft XPS Document Writer或PDF Writer
源程序如下:
1.word ->pdf,xps
using oWord = Microsoft.Office.Interop.Word;
/// <summary>
/// 打印Word文档
/// </summary>
/// <param name="wordfile">完整本地路径,包括文件名</param>
public void Print(string wordfile)
{
oWord.ApplicationClass word = new oWord.ApplicationClass();
Type wordType = word.GetType();
//打开WORD文档
oWord.Documents docs = word.Documents;
Type docsType = docs.GetType();
object objDocName = wordfile;
oWord.Document doc = (oWord.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { objDocName, true, true });
//打印输出到指定文件
//可以使用 doc.PrintOut();方法,次方法调用中的参数设置较繁琐,建议使用 Type.InvokeMember 来调用时可以不用将PrintOut的参数设置全,只设置4个主要参数
Type docType = doc.GetType();
object printFileName = wordfile + ".xps";
docType.InvokeMember("PrintOut", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { false, false, oWord.WdPrintOutRange.wdPrintAllDocument, printFileName });
//退出WORD
wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
}
1.execl->pdf,xps
/// <summary>
/// Execl直接打印
/// </summary>
/// <param name="execlfile">完整本地路径,包括文件名</param>
public void Print(string execlfile)
{
Excel.ApplicationClass eapp = new Excel.ApplicationClass();
Type eType = eapp.GetType();
Excel.Workbooks Ewb = eapp.Workbooks;
Type elType = Ewb.GetType();
object objelName = execlfile;
Excel.Workbook ebook = (Excel.Workbook)elType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, Ewb, new Object[] { objelName, true, true });
object printFileName = execlfile + ".xps";
Object oMissing = System.Reflection.Missing.Value;
ebook.PrintOut(oMissing, oMissing, oMissing, oMissing, oMissing, true, oMissing, printFileName);
eType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, eapp, null);
}