通过虚拟打印机WordToTIF ExcelToTif

本文介绍了一种方法,利用C#实现将Microsoft Word和Excel文档直接打印为TIF格式文件的技术细节。通过反射调用应用程序接口,可以灵活地设置打印选项,并确保文档正确输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

前提添加引用Microsoft.Office.Interop.Word;

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
using System.IO;
using System.Reflection;
using oWord = Microsoft.Office.Interop.Word;
using System.Drawing.Imaging;
using System.Runtime.Serialization.Formatters.Binary;


namespace ThreadStudy
{
    
class WordToTif
    
{

        
        
/// <summary>
        
/// 打印Word文档
        
/// </summary>
        
/// <param name="wordfile">完整本地路径,包括文件名</param>
        
/// <param name="outputFile">输出Tif文件的完整路径</param>

        public static void PrintWord(string wordfile, string outputFile)
        
{
           
            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, truetrue });
            
//Word.
            
//打印输出到指定文件
            
//可以使用 doc.PrintOut();方法,次方法调用中的参数设置较繁琐,建议使用 Type.InvokeMember 来调用时可以不用将PrintOut的参数设置全,只设置4个主要参数
            Type docType = doc.GetType();
            
object printFileName = outputFile;
            docType.InvokeMember(
"PrintOut", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] falsefalse, oWord.WdPrintOutRange.wdPrintAllDocument, printFileName });
            
//退出WORD
            wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
        }


        
/// <summary>
        
/// Execl直接打印
        
/// </summary>
        
/// <param name="execlfile">完整本地路径,包括文件名</param>

        public static void PrintExcel(string execlfile, string ExceloutputFile)
        
{
            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, truetrue });
            
object printFileName = ExceloutputFile;
            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);
        }
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值