Winfom实现word和Excel 预览

源码下载 http://download.csdn.net/detail/woxpp/4122663

思路:

word  to rtf 通过 RichTextBox LoadFile 实现预览

word  to html 通过 WebBrowser  实现预览

excel to html 通过 WebBrowser  实现预览

代码 word  to rtf

    // 指定原文件和目标文件
            object Target = Application.StartupPath + "\\temp.rtf";
            try
            {
                object isread = false;
                object isvisible = true;
                object miss = System.Reflection.Missing.Value;
                // 缺省参数  
                object Unknown = Type.Missing;
                object saveChanges = false;
                object readOnly = true;
                Microsoft.Office.Interop.Word.ApplicationClass wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
                // 打开doc文件
                Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(ref WordPath, ref miss, ref isread, ref miss, ref miss, ref miss, ref miss, ref miss,
                  ref miss, ref miss, ref miss, ref isvisible, ref miss, ref miss, ref miss, ref miss);
                doc.Activate();
                try
                {

                    // 指定另存为格式(rtf)
                    object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatRTF;

                    // 转换格式
                    doc.SaveAs(ref Target, ref format,
                               ref Unknown, ref Unknown, ref Unknown,
                               ref Unknown, ref Unknown, ref Unknown,
                               ref Unknown, ref Unknown, ref Unknown,
                               ref Unknown, ref Unknown, ref Unknown,
                               ref Unknown, ref Unknown);

                }
                catch
                {
                }
                finally
                {
                    if (doc != null)
                    {
                        doc.Close(ref miss, ref miss, ref miss);
                        doc = null;
                    }
                    if (wordApp != null)
                    {
                        wordApp.Quit(ref miss, ref miss, ref miss);
                        wordApp = null;
                    }
                }
                rtbText.LoadFile(Target.ToString());//richtextbox控件加载temp.rtf 
            }
            catch
            {
            }

word to html

  // 指定原文件和目标文件 object Target = Application.StartupPath + "\\temp.html"; try { object isread = false; object isvisible = true; object miss = System.Reflection.Missing.Value; // 缺省参数 object Unknown = Type.Missing; object saveChanges = false; object readOnly = true; Microsoft.Office.Interop.Word.ApplicationClass wordApp = new Microsoft.Office.Interop.Word.ApplicationClass(); // 打开doc文件 Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(ref WordPath, ref miss, ref isread, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref isvisible, ref miss, ref miss, ref miss, ref miss); doc.Activate(); try { // 指定另存为格式(rtf) object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML; // 转换格式 doc.SaveAs(ref Target, ref format, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown); } catch { } finally { if (doc != null) { doc.Close(ref miss, ref miss, ref miss); doc = null; } if (wordApp != null) { wordApp.Quit(ref miss, ref miss, ref miss); wordApp = null; } } webBrowserDoc.Url = new Uri(Target.ToString());//richtextbox控件加载temp.rtf  } catch { }

excel to html

  object trueObject = true; object Target = Application.StartupPath + "\\tempexcel.html"; object missing = Type.Missing; Microsoft.Office.Interop.Excel.Application excel = null; Microsoft.Office.Interop.Excel.Workbook xls = null; try { excel = new Microsoft.Office.Interop.Excel.Application(); excel.Visible = false; excel.DisplayAlerts = false; xls = excel.Workbooks.Open(ExcelPath, missing, trueObject, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); IEnumerator wsEnumerator = excel.ActiveWorkbook.Worksheets.GetEnumerator(); object format = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml; xls.SaveAs(Target, format, missing, missing, missing, missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, missing, missing, missing, missing, missing); } catch { } finally { if (xls != null) { xls.Close(missing, missing, missing); xls = null; } if (excel != null) { excel.Quit(); excel = null; } } webBrowserDoc.Url = new Uri(Target.ToString());

 

你可能感兴趣的:(Excel)