C# web实现word 转Html、office转Html、pdf转图片 在线预览文件
?改篇 pdf 預覽再本機沒問題,發布再iis中 不行 ,(使用剪貼板的問題..excel和word 可以,)
pdf解決:請看我的博文 ----最終解決篇
詳細配置及代碼
word 轉Html
1 /// <summary> 2 /// word轉成html 3 /// </summary> 4 /// <param name="wordFileName"></param> 5 private void WordToHtml(object wordFileName,string htmlWord)6 {7 //在此處放置用戶代碼以初始化頁面 8 Word.ApplicationClass word = new Word.ApplicationClass();9 Type wordType = word.GetType(); 10 Word.Documents docs = word.Documents; 11 //打開文件 12 Type docsType = docs.GetType(); 13 Word.Document doc = (Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { wordFileName, true, true }); 14 //轉換格式,另存為 15 string a = doc.Comments.ToString(); 16 Type docType = doc.GetType(); 17 string wordSaveFileName = wordFileName.ToString(); 18 19 20 21 string strSaveFileName = htmlWord + "\\" + Path.GetFileNameWithoutExtension(wordSaveFileName) + ".html"; 22 23 object saveFileName = (object)strSaveFileName; 24 docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML }); 25 docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null); 26 //退出 Word 27 wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null); 28 29 }引入? Microsoft.Office.Interop.Word;
office轉Html
/// <summary> /// Excel轉成html /// </summary> public void ReadExcel(string rFilePath, string rHtmlFilePath){Excel.Application repExcel = new Excel.Application();Excel.Workbook workbook = null;//xlsFile為Excel文件路徑 workbook = repExcel.Application.Workbooks.Open(rFilePath,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value);//htmlFile 是要另存的html文件名 object ofmt = Excel.XlFileFormat.xlHtml;workbook.SaveAs(rHtmlFilePath,ofmt,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Excel.XlSaveAsAccessMode.xlNoChange,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value);object osave = false;workbook.Close(osave, Missing.Value, Missing.Value);}引入Microsoft.Office.Interop.Excel;
pdf? 轉圖片
說明:本方法 用? acrobat?的官方接口??
需要安裝 ?acrobat???professional??8.0或更高版本
然后再vs中添加com組件??abode ?acrobat??版本號?? type? library? 只有安裝了上面軟件 才回有
?
在web頁 頭部 添加???AspCompat="true"??? 控制臺程序 要在main函數上加??[STAThread]? 保證單線程模型下 才能訪問 剪貼板
using System.IO;
using System.Reflection;
using DataHelp;
using System.Text;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;
?
/// <summary>/// Pdf導圖片/// </summary>/// <param name="pdfInputPathDirectory"></param>/// <param name="pngOutputPath"></param>/// <param name="format"></param>/// <returns></returns>public string PDFToPic(string pdfInputPathDirectory, string pngOutputPath, ImageFormat format){StringBuilder b = new StringBuilder();ImageFormat formate = format;// Acrobat objectsAcrobat.CAcroPDDoc pdfDoc = null;Acrobat.CAcroPDPage pdfPage = null;Acrobat.CAcroRect pdfRect = null;Acrobat.CAcroPoint pdfPoint = null;try{string[] files = new string[] { pdfInputPathDirectory };string dic = Path.GetDirectoryName(pdfInputPathDirectory);//string[] files = Directory.GetFiles(dic, "*.pdf");for (int n = 0; n < files.Length; n++){string inputFile = files[n].ToString();string fileName = files[n].Substring(files[n].LastIndexOf(@"\") + 1).Replace(".pdf", "");// Will always be available as .NET framework ships with allpdfDoc = (Acrobat.CAcroPDDoc)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.PDDoc", "");bool ret = pdfDoc.Open(inputFile);if (!ret){throw new FileNotFoundException();}else{b = b.AppendLine("<span>pdfDoc成功打開文件!!!</span>");}// Get the number of pages (to be used later if you wanted to store that information)int pageCount = pdfDoc.GetNumPages();b = b.AppendLine("<ul style='azimuth:center; list-style-type:none;' >");for (int i = 0; i < pageCount; i++){// Get the first pagepdfPage = (Acrobat.CAcroPDPage)pdfDoc.AcquirePage(i);pdfPoint = (Acrobat.CAcroPoint)pdfPage.GetSize();b = b.AppendLine("<li> <span>pdfPoint.x=" + pdfPoint.x + ";pdfPoint.y=" + pdfPoint.y + "</span> </li>");pdfRect = (Acrobat.CAcroRect)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.Rect", "");pdfRect.Left = 0;pdfRect.right = pdfPoint.x;pdfRect.Top = 0;pdfRect.bottom = pdfPoint.y;// Render to clipboard, scaled by 100 percent (ie. original size)// Even though we want a smaller image, better for us to scale in .NET// than Acrobat as it would greek out small text// see http://www.adobe.com/support/techdocs/1dd72.htm//清空剪貼板//Clipboard.Clear();//b = b.AppendLine("<li> <span>Clipboard.Clear();清空剪貼板 </span> </li>");pdfPage.CopyToClipboard(pdfRect, 0, 0, 100);//b = b.AppendLine("<li> <span>pdfPage.CopyToClipboard(pdfRect, 0, 0, 100)=" +pdfPage.CopyToClipboard(pdfRect, 0, 0, 100)+ ";復制到剪貼板</span> </li>");System.Windows.Forms.IDataObject clipboardData = Clipboard.GetDataObject();bool c = false;if (clipboardData!=null ){c = true;}b = b.AppendLine("<li> <span> Clipboard.GetDataObject()=" + c + "剪貼板中是否有數據;</span> </li>");b = b.AppendLine("<li> <span>clipboardData.GetDataPresent(DataFormats.Bitmap)=" + clipboardData.GetDataPresent(DataFormats.Bitmap) + ";</span> </li>");if (clipboardData.GetDataPresent(DataFormats.Bitmap)){Bitmap pdfBitmap = (Bitmap)clipboardData.GetData(DataFormats.Bitmap);b = b.AppendLine("<li> <span>生成圖片的大小: pdfBitmap.Size.Height=" + pdfBitmap.Size.Height + "; pdfBitmap.Size.Width=" + pdfBitmap.Size.Width + "</span> </li>");string dPath = Path.Combine(pngOutputPath, i.ToString("0000") + "." + formate.ToString());b = b.AppendLine("<li> <span>生成圖片的路徑:" + dPath + ";</span> </li>");pdfBitmap.Save(dPath, formate);pdfBitmap.Dispose();}b = b.AppendLine("<li> <img src='..\\" + imgDire + "\\" + i.ToString("0000") + "." + formate.ToString() + "' /> </li><span>第" + (i + 1) + "頁</span>");}pdfDoc.Close();// Not sure how why it is to do this, but Acrobat is not the best behaved COM object// see http://blogs.msdn.com/yvesdolc/archive/2004/04/17/115379.aspxMarshal.ReleaseComObject(pdfPage);Marshal.ReleaseComObject(pdfRect);Marshal.ReleaseComObject(pdfDoc);}b = b.AppendLine("</ul>");}catch (System.Exception ex){Response.Write(ex.ToString());}return b.ToString();}關于發布:
offic 組件發布問題
?我的服務器環境是 windows servers 2008 64位操作系統
?
1、? 在服務器上安裝Office的Excel相關軟件(推薦安裝Office 2007);
2、? Cmd中輸入命令 comexp.msc -32? 啟動組件服務(啟動組件服務 方法? 運行 mmc、mmc -32(32) 、DCOMCNFG.exe(64)) 不行一個一個
3、? 依次雙擊"組件服務"->"計算機"->"我的電腦"->"DCOM配置";
4、? 在"DCOM配置"中找到"Microsoft Excel Application",在它上面點擊右鍵,然后點擊"屬性",彈出"Microsoft Excel應用程序屬性"對話框;
5、? 點擊"標識"標簽,選擇 啟動用戶(默認選項) ;
6、? 點擊"安全"標簽,在以下? 三個項目中都選中?? “自定義”? 添加 ”everyone”? 并分配最大權限(全選)
7、設置Web.config文件?? 在Web.config文件的<system.web>中加入<identity impersonate="true"/>? (沒有這一步 會報Excel進程無法打開相應的excel文件)。
Ok了
?
?
Word 配置同上。
?
關于pdf 發布:?? 需要在 服務器上安裝 上訴軟件
???? 我在本機vs 和服務器vs中 測試沒問題? ,發布到iis中? 無法取到 剪貼板中的數據??? 也不報錯???? iis中應用程序池 的權限我也設置到最大? ,依然無果。
還是這種 iis中訪問剪貼板的 方法 違反 安全性 或剪貼板只能在單線程模型下訪問? 與這個相悖 ????????????????
歡迎留言探討》》》》》
?
?
?
?
?
?
轉載于:https://www.cnblogs.com/DamonTang/archive/2012/11/29/2794110.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的C# web实现word 转Html、office转Html、pdf转图片 在线预览文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: git ready
- 下一篇: 组合数取模 Lucas定理