• 用C#实现Word,PPT,EXECL 图片输出


    因为要做一个在线阅读模块,所以研究了一天,终于找到了一个比较好的方案:

    1.准备两个工具,office 运行库(interop),虚拟打印机Tagged Images Printer (就是MSOFFICE中Document Imaging的组件)

    2.原理就是用虚拟打印机打印到文件,再把tiff格式转成普通图像格式

    这里是实现:

    添加对应的引用

    view plaincopy to clipboardprint?
    using Word = Microsoft.Office.Interop.Word; 
    using PPT = Microsoft.Office.Interop.PowerPoint; 
    using EXECL = Microsoft.Office.Interop.Excel; 
     

    以下是doc转图像的method

    view plaincopy to clipboardprint?
    string imagepath = null; 
    /// <summary> 
    /// doc 转 图像 
    /// </summary> 
    /// <param name="filepath">要转换的文档的路径(要加文件名)e.g. : C:\users\jackyyf\desktop\abc.doc</param> 
    /// <param name="temppath">临时文件的路径(不要加文件名)e.g. : C:\users\jackyyf\desktop\</param> 
    /// <param name="outputpath">输出的文档的路径(要加文件名)e.g. : C:\users\jackyyf\desktop\abc.jpg</param> 
    public void DOCtoIMAGE(string filepath,string temppath,string outputpath, System.Drawing.Imaging.ImageFormat imageformat) 

        object FileName = (object)filepath; 
        object ReadOnly = (object)true; 
        object PrintToFile = (object)true; 
        object OutPutFiletemp = (object)temppath+DateTime.Now.Ticks.ToString()+".tiff"; 
        Word.ApplicationClass appclass = new Word.ApplicationClass(); 
        appclass.Documents.Open(ref FileName, ReadOnly: ref ReadOnly); 
        appclass.ActivePrinter = "Tagged Images Printer"; 
        appclass.PrintOut(FileName: ref FileName,  
                          PrintToFile: ref PrintToFile,  
                          OutputFileName: ref OutPutFiletemp); 
        appclass.Documents.Close(); 
        appclass.Quit(); 
        System.Drawing.Image img = System.Drawing.Image.FromFile(OutPutFiletemp.ToString()); 
        img.Save(outputpath, imageformat); 
        img.Dispose(); 
        File.Delete(OutPutFiletemp.ToString()); 

     

    这是ppt转图像的代码,还没做method. 

    因为ppt可以直接保存 为图像,就简单一点了,呵呵

    view plaincopy to clipboardprint?
    PPT.ApplicationClass PPTappclass = new PPT.ApplicationClass(); 
    PPT.Presentation PPTpre = PPTappclass.Presentations.Open(@"C:\Users\jackyyf\Desktop\haha.ppt", Office.MsoTriState.msoFalse, Office.MsoTriState.msoFalse, Office.MsoTriState.msoFalse); 
    PPTpre.SaveAs(@"C:\Users\jackyyf\Desktop\haha.jpg", PPT.PpSaveAsFileType.ppSaveAsJPG); 
     

    这是execl转图像的代码,没做method, 文件路径自己去改了..

    view plaincopy to clipboardprint?
    object ActivePrinter = (object)"Tagged Images Printer"; 
    object PrintToFile = (object)true; 
    object PrToFileName = (object)@"C:\users\jackyyf\desktop\" + DateTime.Now.Ticks.ToString(); 
    EXECL.ApplicationClass EXECLappclass = new EXECL.ApplicationClass(); 
    EXECL.Workbook EXECLwb = EXECLappclass.Workbooks.Open(@"C:\users\jackyyf\desktop\haha007.xls"); 
    EXECLwb.PrintOutEx(ActivePrinter: ActivePrinter, PrintToFile: PrintToFile, PrToFileName: PrToFileName); 
    EXECLwb.Close(); 
    EXECLappclass.Quit(); 
    System.Drawing.Image img = System.Drawing.Image.FromFile(PrToFileName.ToString()); 
    img.Save(@"c:\users\jackyyf\desktop\haha.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); 
    img.Dispose(); 
    File.Delete(PrToFileName.ToString()); 
     

  • 相关阅读:
    EOS之session的数据获取
    c# 数据库操作之ACCESS
    基础之创建与导出
    dotNET5的MVC页面传值方式总结
    dotNET开发之MVC中Controller返回值类型ActionResult方法总结
    C# 计算农历日期方法(2021版)
    普通邮箱设置客户端授权码并开启stmp服务以及关于QQ邮箱“命令顺序不正确。 服务器响应为:Error: need EHLO and AUTH first !”问题全指导
    13 张图,深入理解 Synchronized
    Springboot 注解大全
    python中的print()函数的学习-1
  • 原文地址:https://www.cnblogs.com/zzxap/p/2175746.html
Copyright © 2020-2023  润新知