ASP. NET Word ecxel type file online preview
First you have to refer to com:
Microsoft Excel Object Library
Microsoft Word Ten Object Library
or a class library of more than 10.
What I'm using now: Resources download: http://download.csdn.net/detail/panfuy/3247641 or accessories
Microsoft Excel Object Library
Microsoft Word Ten Object Library
The code is as follows:
C # code
- Using System;
- Using System.Data;
- Using System.Configuration;
- Using System.Collections;
- Using System.Web;
- Using System.Web.Security;
- Using System.Web.UI;
- Using System.Web.UI.WebControls;
- Using System.Web.UI.WebControls.WebParts;
- Using System.Web.UI.HtmlControls;
- Using System.IO;
- Using System.Diagnostics;
- Using Word = Microsoft.Office.Interop.Word;
- Using Excel = Microsoft.Office.Interop.Excel;
- Using System.Reflection;
- Using Microsoft.Office.Interop.Excel;
- Public partial class Upload_preview:System.Web.UI.Page
- {
- protected void Page_Load (object sender, EventArgs e)
- {
- Generationwordhtml ("E://20110502.doc", "e://20110502.html");
- Generationexcelhtml ("E://20110502.xls", "e://20110502.html");
- }
- // <summary>
- /// Ecxel file generates HTML and saves
- // </summary>
- /// <param name= "FilePath" > Path of ecxel file to be generated </param>
- /// <param name= "Savefilepath" > The path to save HTML files after generation </param>
- // <returns> Whether the build succeeds, the success is true, and vice versa is false</returns>
- protected bool Generationexcelhtml (string FilePath, string savefilepath)
- {
- Try
- {
- Excel.Application app = new Excel.Application ();
- App. Visible = false;
- Object o = Missing.Value;
- /// Open File
- /* The following is the wording of the Microsoft Excel 9 Object Library: */
- /*_workbook xls = App. Workbooks.Open (FilePath, O , O, O, O, O, O, O, O, O, O, O, O); */
- /* The following is the wording of the Microsoft Excel Object Library: */
- _workbook xls = App. Workbooks.Open (FilePath, O, O, O, O, O, O, O, O, O, O, O, O, O, O);
- /// convert format, save as HTML
- /* The following is the wording of the Microsoft Excel 9 Object Library: */
- /*xls. SaveAs (Savefilepath, Excel.XlFileFormat.xlHtml, O, O, O, O, xlsaveasaccessmode.xlexclusive, O, O, O, O); */
- /* The following is the wording of the Microsoft Excel Object Library: */
- Xls. SaveAs (Savefilepath, Excel.XlFileFormat.xlHtml, O, O, O, O, xlsaveasaccessmode.xlexclusive, O, O, O, O, O);
- /// exit Excel
- App. Quit ();
- return true;
- }
- Catch
- {
- return false;
- }
- finally
- {
- //Close open Excel process last
- process[] myprocesses = Process.getprocessesbyname ("EXCEL");
- foreach (Process myprocess in myprocesses)
- {
- Myprocess.kill ();
- }
- }
- }
- // <summary>
- /// Winword file generates HTML and saves
- // </summary>
- /// <param name= "FilePath" > The path to the word file that needs to be generated </param>
- /// <param name= "Savefilepath" > The path to save HTML files after generation </param>
- // <returns> Whether the build succeeds, the success is true, and vice versa is false</returns>
- Private bool Generationwordhtml (string FilePath, string savefilepath)
- {
- Try
- {
- Word.applicationclass Word = new Word.applicationclass ();
- Type Wordtype = Word. GetType ();
- word.documents docs = Word. Documents;
- /// Open File
- Type Docstype = Docs. GetType ();
- Word.Document doc = (word.document) docstype.invokemember ("Open", System.Reflection.BindingFlags.InvokeMethod, null, Docs, new object[] {FilePath, True, true});
- /// convert format, save as HTML
- Type DocType = doc. GetType ();
- /* The following is the wording of the Microsoft Word 9 Object Library: */
- /*doctype.invokemember ("SaveAs", System.Reflection.BindingFlags.InvokeMethod, NULL, doc, new object[] { Savefilepath, Word.WdSaveFormat.wdFormatHTML}); * *
- /* The following is the wording of the Microsoft Word Object Library: */
- Doctype.invokemember ("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
- null, doc, new object[] {savefilepath, Word.WdSaveFormat.wdFormatFilteredHTML});
- /// quit Word
- Wordtype.invokemember ("Quit", System.Reflection.BindingFlags.InvokeMethod, null, Word, null);
- return true;
- }
- Catch
- {
- return false;
- }
- finally
- {
- //Finally close open Winword process
- process[] myprocesses = Process.getprocessesbyname ("Winword");
- foreach (Process myprocess in myprocesses)
- {
- Myprocess.kill ();
- }
- }
- }
- }
- Microsoft.Office.Interop.Excel_Word.dll.zip (589.3 KB)