紫外工控论坛

 找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

搜索
查看: 2855|回复: 0

[C/VC] 用C#实现Word,PPT,EXECL 图片输出

[复制链接]
冰糖 发表于 2011-11-15 12:45:00 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

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

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

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

  4. 这里是实现:

  5. 添加对应的引用

  6. view plaincopy to clipboardprint?
  7. using Word = Microsoft.Office.Interop.Word; 
  8. using PPT = Microsoft.Office.Interop.PowerPoint; 
  9. using EXECL = Microsoft.Office.Interop.Excel; 
  10.  

  11. 以下是doc转图像的method

  12. view plaincopy to clipboardprint?
  13. string imagepath = null; 
  14. /// <summary>&#160;
  15. /// doc 转 图像&#160;
  16. /// </summary>&#160;
  17. /// <param name="filepath">要转换的文档的路径(要加文件名)e.g. : C:\users\jackyyf\desktop\abc.doc</param>&#160;
  18. /// <param name="temppath">临时文件的路径(不要加文件名)e.g. : C:\users\jackyyf\desktop\</param>&#160;
  19. /// <param name="outputpath">输出的文档的路径(要加文件名)e.g. : C:\users\jackyyf\desktop\abc.jpg</param>&#160;
  20. public void DOCtoIMAGE(string filepath,string temppath,string outputpath, System.Drawing.Imaging.ImageFormat imageformat)&#160;
  21. {&#160;
  22. &#160;&#160;&#160; object FileName = (object)filepath;&#160;
  23. &#160;&#160;&#160; object ReadOnly = (object)true;&#160;
  24. &#160;&#160;&#160; object PrintToFile = (object)true;&#160;
  25. &#160;&#160;&#160; object OutPutFiletemp = (object)temppath+DateTime.Now.Ticks.ToString()+".tiff";&#160;
  26. &#160;&#160;&#160; Word.ApplicationClass appclass = new Word.ApplicationClass();&#160;
  27. &#160;&#160;&#160; appclass.Documents.Open(ref FileName, ReadOnly: ref ReadOnly);&#160;
  28. &#160;&#160;&#160; appclass.ActivePrinter = "Tagged Images Printer";&#160;
  29. &#160;&#160;&#160; appclass.PrintOut(FileName: ref FileName,&#160;&#160;
  30. &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; PrintToFile: ref PrintToFile,&#160;&#160;
  31. &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; OutputFileName: ref OutPutFiletemp);&#160;
  32. &#160;&#160;&#160; appclass.Documents.Close();&#160;
  33. &#160;&#160;&#160; appclass.Quit();&#160;
  34. &#160;&#160;&#160; System.Drawing.Image img = System.Drawing.Image.FromFile(OutPutFiletemp.ToString());&#160;
  35. &#160;&#160;&#160; img.Save(outputpath, imageformat);&#160;
  36. &#160;&#160;&#160; img.Dispose();&#160;
  37. &#160;&#160;&#160; File.Delete(OutPutFiletemp.ToString());&#160;
  38. }&#160;
  39. &#160;

  40. 这是ppt转图像的代码,还没做method.&#160;

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

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

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

  48. view plaincopy to clipboardprint?
  49. object ActivePrinter = (object)"Tagged Images Printer";&#160;
  50. object PrintToFile = (object)true;&#160;
  51. object PrToFileName = (object)@"C:\users\jackyyf\desktop" + DateTime.Now.Ticks.ToString();&#160;
  52. EXECL.ApplicationClass EXECLappclass = new EXECL.ApplicationClass();&#160;
  53. EXECL.Workbook EXECLwb = EXECLappclass.Workbooks.Open(@"C:\users\jackyyf\desktop\haha007.xls");&#160;
  54. EXECLwb.PrintOutEx(ActivePrinter: ActivePrinter, PrintToFile: PrintToFile, PrToFileName: PrToFileName);&#160;
  55. EXECLwb.Close();&#160;
  56. EXECLappclass.Quit();&#160;
  57. System.Drawing.Image img = System.Drawing.Image.FromFile(PrToFileName.ToString());&#160;
  58. img.Save(@"c:\users\jackyyf\desktop\haha.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);&#160;
  59. img.Dispose();&#160;
  60. File.Delete(PrToFileName.ToString());&#160;
复制代码
原文地址:用C#实现Word,PPT,EXECL 图片输出_风语者_百度空间
http://hi.baidu.com/zzxap/blog/item/51f1e2fa7b1bb60da9d3110d.html
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则


--------------------------------------------------------------------------------------------------------------------
本站是工控技术交流站点,论坛内容均为网络收集或会员所发表,并不代表本站立场,会员拥有该内容的所有权力及责任!
本站内容如有侵犯您的版权,请按下面方式联系本站管理员,我们将及时删除处理
管理员:冰糖 QQ:5483695(请直击主题), Mail:admin#ziwai.net(#改成@) 其它非本人.
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论!

QQ|Archiver|手机版|小黑屋|紫外工控论坛. ( 苏ICP备11032118号-1 )

GMT+8, 2024-4-29 09:15 , Processed in 0.500003 second(s), 17 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表