Skip to content

Commit

Permalink
----
Browse files Browse the repository at this point in the history
  • Loading branch information
zyqwst committed Sep 19, 2017
1 parent 06ba15d commit 1f8651e
Show file tree
Hide file tree
Showing 29 changed files with 4,355 additions and 3 deletions.
Binary file modified .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion .classpath
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk1.7.0_17">
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.launching.macosx.MacOSXType/Java SE 7 [1.7.0_79]">
<attributes>
<attribute name="owner.project.facets" value="java"/>
</attributes>
Expand Down
Binary file added WebContent/.DS_Store
Binary file not shown.
Binary file added WebContent/WEB-INF/.DS_Store
Binary file not shown.
Empty file modified WebContent/WEB-INF/lib/dom4j-1.6.1.jar
100644 → 100755
Empty file.
Binary file added WebContent/WEB-INF/lib/ooxml-schemas-1.1.jar
Binary file not shown.
Binary file added WebContent/WEB-INF/lib/poi-3.8-20120326.jar
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added WebContent/WEB-INF/lib/xmlbeans-2.3.0.jar
Binary file not shown.
3 changes: 2 additions & 1 deletion WebContent/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ <h2>Web打印控件样例</h2>
服务端打印DEMO采用简单的Servlet 3.0。本地打印DEMO是基于Blade的轻量级MVC框架,访问端口9000。
<p></p>
<a id="printBtn" class="btn btn-primary" onclick="print_server('zjd')">点击打印</a>
<a class="btn btn-danger" href="ViewerJS/#../bill.xls">预览</a>
</div>

<iframe src = "ViewerJS/#../bill.xls" width='700' height='500' allowfullscreen webkitallowfullscreen></iframe>
<script type="text/javascript" src="static/jquery.min.js"></script>
<script type="text/javascript"
src="static/bootstrap-3.3.5-dist/js/bootstrap.min.js"></script>
Expand Down
53 changes: 53 additions & 0 deletions WebContent/index2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet"
href="static/bootstrap-3.3.5-dist/css/bootstrap.min.css">

</head>
<body >
<div class="container">
<div class="jumbotron">
<h2>MS Office 生成HTML,网页预览</h2>
<p>访问端口8080</p>
<form enctype="multipart/form-data" method="post" action="/convert" id="upload-form">
<div class="form-group">
<label for="file">上传文档</label>
<input type="file" class="form-control" id="file" name="file" >
</div>
<button type="button" class="btn btn-primary" onclick="upload()">上传</button>
</form>
</div>
</div>
<div id="preview"></div>
<script type="text/javascript" src="static/jquery.min.js"></script>
<script type="text/javascript"
src="static/bootstrap-3.3.5-dist/js/bootstrap.min.js"></script>
<script type="text/javascript">
function upload() {
var formData = new FormData($( "#upload-form" )[0]);
$.ajax({
url: 'poi?method=convert' ,
type: 'POST',
data: formData,
async: false,
cache: false,
contentType: false,
processData: false,
success: function (returndata) {

console.log(JSON.parse(returndata).content);
$("#preview").html(JSON.parse(returndata).content);
},
error: function (returndata) {
console.log("error:")
}
});
}

</script>

</body>
</html>
Binary file added src/.DS_Store
Binary file not shown.
Binary file added src/cc/.DS_Store
Binary file not shown.
Binary file added src/cc/mask/.DS_Store
Binary file not shown.
73 changes: 73 additions & 0 deletions src/cc/mask/utils/HtmlUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package cc.mask.utils;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public final class HtmlUtil {


private Element styleSheet = null;
public Element window = null;
public Element body = null;
public Element script = null;
public Element html = null;
public Document document = null;


public HtmlUtil(){
try {
document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
}

public void createHTML(){
html = document.createElement("html");

Element head = document.createElement("head");
styleSheet = document.createElement("style");
styleSheet.setAttribute( "type", "text/css" );
script = document.createElement("script");
script.setAttribute("language", "text/javascript");
head.appendChild(script);
head.appendChild(styleSheet);
html.appendChild(head);

body = document.createElement("body");
window = createBlock();
body.appendChild(window);
html.appendChild(body);

document.appendChild(html);

}


public void addClass(Element ele, String className, String style){
String oldClazz = ele.getAttribute( "class" );



ele.setAttribute("class", isEmpty(oldClazz) ? className : className +" " + oldClazz);

}

public Element createBlock(){
return document.createElement("div");
}

public void createStyle(){



}

public static boolean isEmpty(String str){
return str == null || str.trim().length() == 0;
}

}
45 changes: 45 additions & 0 deletions src/cc/mask/utils/StringUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package cc.mask.utils;



public class StringUtil {

/**
* Returns <code>true</code> if the provided String is either <code>null</code>
* or the empty String <code>""</code>.<p>
*
* @param value the value to check
*
* @return true, if the provided value is null or the empty String, false otherwise
*/
public static boolean isEmpty(String value) {

return (value == null) || (value.length() == 0);
}

/**
* extract file name form the given file path
* @param filePath path to the file, like 'c:/test.jpg', 'c:\\test.jpg'
* @param withExtention indicate contain file.extention. true : contain | false : ignore
* @return fileName file.name;
*/
public static String getFileName(String filePath,boolean withExtention){
int sep = filePath.lastIndexOf("\\") == -1 ? filePath.lastIndexOf("/") : filePath.lastIndexOf("\\");
if(withExtention)
return filePath.substring( sep + 1 );
return filePath.substring( sep + 1 , filePath.lastIndexOf("."));
}

/**
* get path to the given file , e.g. : c:\test\aaa.html -> c:\test\
* @param fileFullPath path to file;
* @return
*/
public static String getFilePath(String fileFullPath){
int sep = fileFullPath.lastIndexOf("\\") == -1 ? fileFullPath.lastIndexOf("/") : fileFullPath.lastIndexOf("\\");
return fileFullPath.substring(0, sep + 1);
}



}
74 changes: 74 additions & 0 deletions src/com/albert/servlet/PoiServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
*
*/
package com.albert.servlet;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collection;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Part;

import org.apache.poi.ConvertFacade;
import org.apache.poi.hwpf.converter.DocConverter;

import com.albert.domain.ResponseEntity;

/**
* @ClassName: PoiServlet
* @Description:
* @author albert
* @date 2017年9月19日 下午12:41:36
*
*/
@WebServlet(value = "/poi")
@MultipartConfig
public class PoiServlet extends BaseServlet {

/**
*
*/
private static final long serialVersionUID = 7890806721814771129L;

public void convert() throws Exception {

String savePath = req.getServletContext().getRealPath("/uploadFile");
File dir = new File(savePath);
if(!dir.exists()){
dir.mkdirs();
}
Collection<Part> parts = req.getParts();
if (parts.size() == 1) {
Part part = req.getPart("file");
String header = part.getHeader("content-disposition");
// 获取文件名
String fileName = getFileName(header);
// 把文件写到指定路径
part.write(savePath + File.separator + fileName);
String htmlFile = savePath+File.separator +fileName.replace(".","")+".html";
ConvertFacade.convert(savePath + File.separator + fileName,htmlFile);

BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(htmlFile),"utf-8")); //这里可以控制编码
StringBuffer sb = new StringBuffer();
String line = null;
while((line = br.readLine()) != null) {
sb.append(line);
}

write(resp, ResponseEntity.success(sb.toString()));
}
}

public String getFileName(String header) {
String[] tempArr1 = header.split(";");
String[] tempArr2 = tempArr1[2].split("=");
String fileName = tempArr2[1].substring(tempArr2[1].lastIndexOf("\\") + 1).replaceAll("\"", "");
return fileName;
}
}
1 change: 0 additions & 1 deletion src/com/albert/servlet/PrintServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,4 @@ public void printdata() throws IOException {
write(resp,ResponseEntity.failed(e.getMessage()));
}
}
//...其他方法
}
Binary file added src/org/.DS_Store
Binary file not shown.
Binary file added src/org/apache/.DS_Store
Binary file not shown.
48 changes: 48 additions & 0 deletions src/org/apache/poi/ConvertFacade.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
*
*/
package org.apache.poi;

import org.apache.poi.hslf.converter.PPTConverter;
import org.apache.poi.hssf.converter.XlsConverter;
import org.apache.poi.hwpf.converter.DocConverter;
import org.apache.poi.xssf.converter.XlsxConverter;
import org.apache.poi.xwpf.converter.DocxConverter;

/**
* @ClassName: ConvertFacade
* @Description: 转换门面类
* @author albert
* @date 2017年9月18日 下午4:52:00
*
*/
public class ConvertFacade {
public final static String DOC = "doc";
public final static String DOCX = "docx";
public final static String XLS = "xls";
public final static String XLSX = "xlsx";
public final static String PPT = "ppt";

public static void convert( String filePath, String output ) throws Exception{
if(!filePath.contains(".")) throw new Exception("不合法的文件名");
switch (filePath.substring(filePath.lastIndexOf(".")+1).toLowerCase()) {
case DOC:
DocConverter.convert(filePath, output);
break;
case DOCX:
DocxConverter.convert(filePath, output);
break;
case XLS:
XlsConverter.convert(filePath, output);
break;
case XLSX:
XlsxConverter.convert(filePath, output);
break;
case PPT:
PPTConverter.convert(filePath, output);
break;
default:
throw new Exception("不合法的文件");
}
}
}
Loading

0 comments on commit 1f8651e

Please sign in to comment.