ASP. net2.0 converts Word and Excel into HTML

Source: Internet
Author: User
// Website --- add reference -- com -- Microsoft Excel 9.0 Object Library. // Microsoft Word 9.0 Object Library

Using System. reflection;
Using Excel;
Using System. diagnostics;
Using Microsoft. Office;

Public Partial Class Tel: system. Web. UI. Page
... {
Protected Void Page_load ( Object Sender, eventargs E)
... {
//Word
Response. Write (get_html ("New.doc"));
//Excel
Excelconverttohtml ("E: \ 12345.xls","E: \ aaa.html");
}

Protected Void Excelconverttohtml ( String Xlspath, String Htmlpath)
... {
Try
... {
Excel. Application app = New Excel. Application ();
App. Visible = False ;
Object o = Missing. value;

/**/ ///_ Workbook XLS = app. workbooks. open (xlspath, ref o, ref O, ref o, ref O );

_ Workbook xls = App. workbooks. Open (xlspath, O, O );
Object Filename = Htmlpath;
Object Format = Excel. xlfileformat. xlhtml; // Html

// XLS. saveas (ref filename, ref format, ref o, ref O );
XLS. saveas (filename, format, O, xlsaveasaccessmode. xlexclusive, O, O );
// XLS. saveas (
Object T = True ;
App. Quit ();

Process [] myprocesses = Process. getprocessesbyname ( " Excel " );
Foreach (Process myprocess In Myprocesses)
... {
Myprocess. Kill ();
}
}
Catch (Exception ex)
... {
System. Console. Write (ex. Message );
//MessageBox. Show (ex. Message );
}
}
Private String Get_html ( String Filename)
... {

Word. applicationclass word = New Word. applicationclass ();
Type wordtype = Word. GetType ();
Word. Documents docs = Word. documents;

// Open a file
Type docstype = Docs. GetType ();
// Raid.doc
// String newfilenamepath = This. server. mappath (mydata. config. sysuploadpath + "/" + filename );
String Newfilenamepath = This . Server. mappath ( " Download/ " + Filename );
Word. Document Doc = (Word. Document) docstype. invokemember

( " Open " , System. reflection. bindingflags. invokemethod, Null , Docs, New Object [] ... {Newfilenamepath,True,

True} );

// Convert the format and save it
Type doctype = Doc. GetType ();
Object Savefilename = Newfilenamepath. Replace ( " . Doc " , " . Htm " );
// The following is the Microsoft Word 9 Object Library statement. If it is 10, it may be written as follows:
/**/ /*
Doctype. invokemember ("saveas", system. reflection. bindingflags. invokemethod,
Null, Doc, new object [] {savefilename, word. wdsaveformat. wdformatfilteredhtml });
*/
/**/ /// Other formats:
/// Wdformathtml
/// Wdformatdocument
/// Wdformatdostext
/// Wdformatdostextlinebreaks
/// Wdformatencodedtext
/// Wdformatrtf
/// Wdformattemplate
/// Wdformattext
/// Wdformattextlinebreaks
/// Wdformatunicodetext
Doctype. invokemember ( " Saveas " , System. reflection. bindingflags. invokemethod, Null , Doc, New Object [] ... {Savefilename, word. wdsaveformat. wdformathtml} );

// Exit word
Wordtype. invokemember ( " Quit " , System. reflection. bindingflags. invokemethod, Null , Word, Null );

String Newfilename = Filename. Replace ( " . Doc " , " . Htm " );
Return (Newfilename );
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.