프론트 데스크 톱 문자열 내 보 내기 word
package com.yunfengtech.common;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import org.apache.log4j.Logger;
import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;
import com.lowagie.text.pdf.BaseFont;
public class HtmlToWord {
private static Logger logger = Logger.getLogger("default");
public static boolean writeWordFile(String inputFile, String outputFile)
throws Exception {
boolean w = false;
outputFile = getUrl();
logger.error("export word file dir is :" + outputFile);
if (!"".equals(outputFile)) {
//
File fileDir = new File(outputFile);
logger.error("export word file dir is exist :" + fileDir.exists());
if (fileDir.exists()) {
//
String fileName = "report.doc";
// IE
inputFile = inputFile.replaceAll("colSpan", "colspan")
.replaceAll("rowSpan", "rowspan")
.replaceAll("cellSpacing", "cellspacing")
.replaceAll("\"", "\'");
ITextRenderer renderer = new ITextRenderer();
ITextFontResolver fontResolver = renderer.getFontResolver();
try{
if(isWindowsSystem()){
fontResolver.addFont("C:/Windows/fonts/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
}else{
fontResolver.addFont("/usr/share/fonts/chinese/TrueType/simsun.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
}
}catch(Exception e1){
e1.printStackTrace();
logger.error("export word addFont exception :"+e1);
}finally{
}
StringBuffer html = new StringBuffer();
html.append("<!DOCTYPE html PUBLIC \'-//W3C//DTD XHTML 1.0 Transitional//EN\' \'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\'>");
html.append("<html xmlns=\'http://www.w3.org/1999/xhtml\'>")
.append("<head>")
.append("<meta http-equiv=\'Content-Type\' content=\'text/html; charset=UTF-8\' />")
.append("<style type=\'text/css\' mce_bogus=\'1\'>body {font-family: SimSun;} .reportTitle{font-size:20pt;font-weight:bold;}</style>")
.append("</head>").append("<body>")
.append(inputFile);
html.append("</body></html>");
String content = html.toString();
ByteArrayInputStream bais = null;
POIFSFileSystem poifs = null;
FileOutputStream ostream = null;
try{
byte b[] = content.getBytes();
bais = new ByteArrayInputStream(b);
poifs = new POIFSFileSystem();
DirectoryEntry directory = poifs.getRoot();
@SuppressWarnings("unused")
DocumentEntry documentEntry = directory.createDocument("WordDocument", bais);
ostream = new FileOutputStream(outputFile + fileName);
poifs.writeFilesystem(ostream);
}catch(Exception e){
e.printStackTrace();
logger.error("export word exception :"+e);
}finally{
if(bais != null){
bais.close();
}
if(ostream != null){
ostream.close();
}
}
}
}
return w;
}
public static String getUrl() {
String path = HtmlToWord.class.getProtectionDomain().getCodeSource()
.getLocation().getPath();
int index = path.lastIndexOf("WEB-INF/lib");
if (index == -1) {
index = path.lastIndexOf("WEB-INF/classes");
}
String realPath = path.substring(0, index);
realPath = realPath.replace("%20", " ");
return realPath;
}
public static Boolean isWindowsSystem(){
Boolean b = false ;
String os = System.getProperties().getProperty("os.name");
logger.error("the os is :"+os);
if(os != null && os.toLowerCase().startsWith("windows")){
b = true;
}
return b;
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.