Java implement Word document open view content

Source: Internet
Author: User

My understanding of this function is: Get the information you want to display and write it to a temporary file so that the plugin can edit it. Upload the relative path of the temporary file to the page. Page get the relative path of the document to be displayed edit the document in the Word document

Background Code section:

@RequestMapping ("Check")
public string Check (string Wid,model model,httpservletresponse response,httpservletrequest request) throws IOException {
Uuidutil uuid = new Uuidutil ();
String uid = Uuid.getuid ();//32-bit UUID with "-" removed
String fileName = "";//File name
String fileUrl = "";//file path
String Absoluteofficefiledir = Request.getservletcontext (). Getrealpath ("/") + "Resources/uploadofficefile"; Absolute path to Office files
String Relativeofficefileurl = ".. /resources/uploadofficefile "; Relative paths to Office file access
Read content is written to temporary files so that the plugin can edit it online
byte filebody[] = new byte[10240];
Words Word = Wordsbiz.getwordbywid (WID);
FileName = Word.getname () + ". docx";
Filebody = Word.getdesc (). GetBytes ();
FILEURL = relativeofficefileurl+ "/" +uid+filename;//relative path, accessed with
String infileurl= absoluteofficefiledir+ "/" +uid+filename;//absolute path, used by IO operation
FileOutputStream fs = new FileOutputStream (Infileurl);
Fs.write (Filebody);
Fs.flush ();
Fs.close ();
Model.addattribute ("FileUrl", FILEURL);
Model.addattribute ("word", word);
return "Wordscheck";
}

Front Page section:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "UTF-8"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title>office Document editing </title>
<meta content= "ie=7" http-equiv= "x-ua-compatible"/>
<!--Setting the cache--
<!--<meta http-equiv= "Cache-control" content= "No-cache,must-revalidate" >
<meta http-equiv= "Pragram" content= "No-cache" >
<meta http-equiv= "Expires" content= "0" >--
<link href= "/resources/css/stylesheet.css" rel= "StyleSheet" type= "Text/css"/>
<script type= "Text/javascript" > src= "${pagecontext.request.contextpath}/resources/js/jquery-1.11.3.js" > </script>
<body onload= ' Load ("${fileurl}"); ' >
<form id= "Form1" action= "uploadofficefile.jsp" enctype= "Multipart/form-data" style= "padding:0px;margin:0px;" >
<input size= "type=" "Text" name= "url" id= "url" value= "${fileurl}"/>
<div id= "Editmain" class= "Editmain" >
<div id= "Editmain_middle" class= "Editmain_middle" >
<div id= "Editmain_right" class= "Editmain_right" >
<div id= "Formtop" >
<table>
<tr>
&LT;TD width= "8%" > Paper &nbsp; Parts &nbsp;ID:</td>
&LT;TD width= "20%" ><input name= "Flowinid" id= "Flowinid" readonly= "readonly" type= "text" value= "${word.wid}"/ ></td>
&LT;TD width= "8%" > File name:</td>
&LT;TD width= "20%" ><input name= "filename" id= "filename" type= "text" value= "${word.name}"/></td>
<td>&nbsp;</td>
</tr>
</table>
<div id=statusbar style= "Height:20px;width:100%;background-color: #c0c0c0; font-size:12px;" ></div>

The key to JS must be loaded
<script language= "javascript" type= "Text/javascript" src= "${pagecontext.request.contextpath}/resources/js/ Ntkoofficecontrol.js "></script>
<script type= "Text/javascript" >

var office_control_obj;//control Object
var isfileopened; Whether the control opens the document
var FileType;
var filetypesimple;
function Load (FILEURL) {
alert (FILEURL);

Office_control_obj = document.all ("Tanger_ocx");//The Loading of the Browse document control must be

Ntko_ocx_opendoc (FILEURL);//required content is loaded into the document control

}
function Ntko_ocx_opendoc (FILEURL) {
Office_control_obj. Beginopenfromurl (FILEURL);
}
</script>
</div>
</div>
</div>
</div>
</form>
</body>

"Ntkoofficecontrol.js" Key code

document.write (' <!--js script for the ActiveX control used to generate the edit state--');
document.write (' <!--because of Microsoft's new ActiveX mechanism, requires an externally introduced js--> ');
document.write (' <object id= ' tanger_ocx ' classid= ' clsid:a39f1330-3322-4a1d-9bf0-0ba2bb90e970 ');
document.write (' codebase= ' http://www.ntko.com/control/officecontrol/OfficeControl.cab#version=5,0,2,7 "width=" 100% "height=" 100% ">");
document.write (' <param name= ' Isuseutf8url "value="-1 ">");
document.write (' <param name= ' Isuseutf8data "value="-1 ">");
document.write (' <param name= ' BorderStyle "value=" 1 ">");
document.write (' <param name= ' bordercolor ' value= ' 14402205 ' > ');
document.write (' <param name= ' titlebarcolor ' value= ' 15658734 ' > ');
document.write (' <param name= ' titlebartextcolor "value=" 0 ">");
document.write (' <param name= ' menubarcolor ' value= ' 14402205 ' > ');
document.write (' <param name= ' menubuttoncolor ' value= ' 16180947 ' > ');
document.write (' <param name= ' Menubarstyle "value=" 3 ">");
document.write (' <param name= ' Menubuttonstyle "value=" 7 ">");
document.write (' <param name= ' webusername ' value= ' Ntko ' > ');
document.write (' <param name= ' Caption "value=" Ntko Office Document Control Sample demo http://www.ntko.com ">");
document.write (' <span style= ' color:red ' > Cannot load document controls. Make sure you can connect to the network or check the security settings in the browser's options. <a href= "Http://www.ntko.com/control/officecontrol/officecontrol.zip" > Installation demo Products </a></SPAN> ');
document.write (' </object> ');

Java implement Word document open view content

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.