LiberOffice实现在线编辑Word和Excel文件的应用

  • gdTest
    了解作者
  • Java
    开发工具
  • 4.6MB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • 5 积分
    下载积分
  • 1
    下载次数
  • 2022-08-11 14:24
    上传日期
所有的maven包,LiberOffice实现在线编辑Word和Excel文件的应用
LiberOffice.rar
内容介绍
package com.airxiechao.oarword; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.math.BigInteger; import java.net.URLDecoder; import java.util.Enumeration; import java.util.Iterator; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.xml.bind.JAXBElement; import org.apache.commons.fileupload.FileItemIterator; import org.apache.commons.fileupload.FileItemStream; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.commons.fileupload.util.Streams; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.docx4j.Docx4jProperties; import org.docx4j.dml.wordprocessingDrawing.Inline; import org.docx4j.jaxb.Context; import org.docx4j.openpackaging.packages.WordprocessingMLPackage; import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage; import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; import org.docx4j.wml.BooleanDefaultTrue; import org.docx4j.wml.CTBorder; import org.docx4j.wml.CTHeight; import org.docx4j.wml.Drawing; import org.docx4j.wml.HpsMeasure; import org.docx4j.wml.Jc; import org.docx4j.wml.JcEnumeration; import org.docx4j.wml.ObjectFactory; import org.docx4j.wml.P; import org.docx4j.wml.PPr; import org.docx4j.wml.R; import org.docx4j.wml.RPr; import org.docx4j.wml.STBorder; import org.docx4j.wml.Tbl; import org.docx4j.wml.TblBorders; import org.docx4j.wml.TblGrid; import org.docx4j.wml.TblGridCol; import org.docx4j.wml.TblPr; import org.docx4j.wml.TblWidth; import org.docx4j.wml.Tc; import org.docx4j.wml.TcPr; import org.docx4j.wml.Text; import org.docx4j.wml.Tr; import org.docx4j.wml.TrPr; import org.docx4j.wml.UnderlineEnumeration; import org.docx4j.wml.PPrBase.Spacing; import org.docx4j.wml.TcPrInner.GridSpan; import org.docx4j.wml.TcPrInner.VMerge; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; /** * Servlet implementation class Download */ public class Download extends HttpServlet { private static final long serialVersionUID = 1L; private String sessionId = "0"; private String servletPath; private static final int BUFSIZE = 4096; /** */ public Download() { super(); Docx4jProperties.getProperties().setProperty("docx4j.Log4j.Configurator.disabled", "true"); } protected void download(HttpServletRequest request, HttpServletResponse response) throws IOException { sessionId = request.getSession().getId(); servletPath = getServletContext().getRealPath("/"); String filePara = new String(request.getParameter("file").getBytes("ISO-8859-1"), "UTF-8"); try { String filePath = servletPath + filePara; File file = new File(filePath); if( !file.exists() ) { throw new Exception(); } int length = 0; ServletOutputStream outStream = response.getOutputStream(); ServletContext context = getServletConfig().getServletContext(); String mimetype = context.getMimeType(filePath); // sets response content type if (mimetype == null) { mimetype = "application/octet-stream"; } response.setContentType(mimetype); response.setContentLength((int)file.length()); String fileName = file.getName(); // sets HTTP header response.setHeader("Content-Disposition", "attachment; filename=\"" + new String(fileName.getBytes("UTF-8"), "ISO-8859-1") + "\""); byte[] byteBuffer = new byte[BUFSIZE]; DataInputStream in = new DataInputStream(new FileInputStream(file)); // reads the file's bytes and writes them to the response stream while ((in != null) && ((length = in.read(byteBuffer)) != -1)) { outStream.write(byteBuffer,0,length); } in.close(); outStream.close(); } catch(Exception e) { response.sendError(404); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { sessionId = request.getSession().getId(); servletPath = getServletContext().getRealPath("/"); try { String type = null; String name = null; String xml = null; ServletFileUpload upload = new ServletFileUpload(); FileItemIterator iter = upload.getItemIterator(request); while (iter.hasNext()) { FileItemStream item = iter.next(); String itemName = item.getFieldName(); InputStream stream = item.openStream(); if (item.isFormField()) { if( itemName.equals("type") ) { type = Streams.asString(stream); } else if( itemName.equals("name") ) { name = Streams.asString(stream); } else if( itemName.equals("xml") ) { xml = Streams.asString(stream); } } } //System.out.println(type); //System.out.println(name); //System.out.println(xml); if( type == null ) { type = "docx"; } if( type == null || name == null || xml == null ) { throw new Exception(); } // create file WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage(); MainDocumentPart wordDocumentPart = wordMLPackage.getMainDocumentPart(); ObjectFactory factory = Context.getWmlObjectFactory(); // doc xml Document doc = DocumentHelper.parseText(xml); Element root = doc.getRootElement(); addElementToDocx(root, null, wordDocumentPart, factory, wordMLPackage); String fileDirStr = "tmp/" + sessionId + (System.currentTimeMillis() / 1000L) + "DIR"; File fileDir = new File(servletPath + fileDirStr); if( !fileDir.exists() ) { fileDir.mkdir(); } String fileName = fileDirStr + "/" + name + "." + "docx"; File file = new File(servletPath + fileName); System.out.println(file.getAbsolutePath()); wordMLPackage.save(file); // docx to doc or pdf if( type.equals("doc") ) { String docFileName = fileDirStr + "/" + name + "." + "doc"; FileConverter.convert(servletPath + fileName, servletPath + docFileName); fileName = docFileName; } else if( type.equals("pdf") ) { String pdfFileName = fileDirStr + "/" + name + "." + "pdf"; FileConverter.convert(servletPath + fileName, servletPath + pdfFileName); fileName = pdfFileName; } response.setCharacterEncoding("UTF-8"); response.getWriter().print(fileName); } catch(Exception e) { e.printStackTrace(); response.getWriter().print("error"); } } protected void addElementToDocx(Element root, Tc tc, MainDocumentPart wordDocumentPart, ObjectFactory factory, WordprocessingMLPackage wordMLPackage) throws Exception{ try { // iterate paragraph or table for ( Iterator i = root.elementIterator(); i.hasNext(); ) { Element pt = (Element) i.next(); if( pt.getName().equals("paragraph") ) { P p = factory.createP(); if( tc == null ) { wordDocumentPart.addObject(p); } else {
评论
    相关推荐
    • libiconv-1.1.tar.gz
      字符集转换程序
    • VisualC++.rar
      vc++数字图像处理 ,是一本很不错的介绍数字图像方面的书籍,这里有本书的全部源码
    • sharewareluncher.zip
      执行和去除共享软件日期限制的程序
    • opctkit.rar
      opc client 的开发工具
    • 一个XML留言本源代码(C#).rar
      用C#编写的XML源程序
    • VB6MultiThread.rar
      VB多线程源代码
    • ScrollBitmap_demo.zip
      Displaying a large bitmap file on a dialog box, in its original size, is quite difficult in the VC++ environment. However, it is possible to display a large bitmap to a predefined area of the dialog by using the StretchBlt( ) function.The major disadvantage of this is that the clarity of the image will be lost. Check out this article for displaying large bitmaps into the desired area of your dialog box in its original size with a scrolling technique used to show the entire bitmap. 滚动显示位图 在VC++环境下,在一个对话框中显示一个原始尺寸的大小的位图文件相当是困难的。然而,通过使用 StretchBlt()函数一个给定的区域显示一个大的位图是可能的。主要的缺点是图像将会失真。看了这篇通过卷动技术显示整个位图技术的文章,你将能够以它的原始尺寸在给定对话框的区域内显示一个大位图。 来源: http://www.codeguru.com/bitmap/ScrollBitmap.html