返回> 网站首页 

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

yoours2011-03-10 10:09:48 阅读 1365

简介一边听听音乐,一边写写文章。

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

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>  
p lic void DOCtoIMAGE(string filepath,string temppath,string outputpath, System.Drawing.Imaging.ImageFormat imageformat)  
{  
    object FileName = (object)filepath;  
    object ReadOnly = (object)tr;  
    object PrintToFile = (object)tr;  
    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)tr;  
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());  
 

微信小程序扫码登陆

文章评论

1365人参与,0条评论