java导word

function exportToWord() {
var content = UE.getEditor('container').getContent();
var realContent=content.replace(/\/ueditor/g,"http://localhost/ueditor");
/*  console.log(realContent);
 $.ajax({
url : "FrmMeetingSummary.getContent",
dataType : 'json',
type : 'post',
data : {
"content" : realContent
},
success : function(data) {
alert("导出word成功!请前去"+data.localPath+"进行查看!");
}
});  */ 

var testJson2=return2Br(realContent);//br
testJson2= nbsp2Space(testJson2);//空格
testJson2=escape2Html(testJson2);//转意符换成普通字符
testJson2=StringAs(testJson2);

var testJson = '{realContent: "'+testJson2+'" }';
var testJson3=encodeURIComponent(encodeURIComponent(testJson2));

//console.log(realContent1.name);
window.location.href="FrmMeetingSummary.getContent?content="+testJson3+""; 
}
function return2Br(str) { 
return str.replace(/\r?\n/g,"<br/>"); 

function nbsp2Space(str) { 
var arrEntities = {'nbsp' : ' '}; 
return str.replace(/&(nbsp);/ig, function(all, t){return arrEntities[t]}) 

function escape2Html(str) { 
var arrEntities={'lt':'<','gt':'>','nbsp':' ','amp':'&','quot':'"'}; 
return str.replace(/&(lt|gt|nbsp|amp|quot);/ig,function(all,t){return arrEntities[t];}); 

function StringAs(string) {
return '"' + string.replace(/(\\|\"|\n|\r|\t)/g, "\\$1") + '"';

}




public IPage getContent() throws Exception {
//JsonPage json = new JsonPage(this);
String localPath = "";
this.getResponse().setContentType("text/json;charset=utf-8");;//下载
//String agent = this.getResponse().getHeader("USER-AGENT").toLowerCase();
String codedFileName = java.net.URLEncoder.encode("监督员表", "UTF-8");
/*if (agent.contains("firefox")) {*/
this.getResponse().setCharacterEncoding("utf-8");
this.getResponse().setHeader("content-disposition",
"attachment;filename=" + new String("监督员".getBytes(), "ISO8859-1") + ".doc");
try {
// 生成临时文件名称
/* String path = file.toString();
System.out.println("path路径:"+path);*/
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMddHHmmss");
//String fileName = "会议纪要(" + sdf2.format(new Date()) + ").doc";
OutputStream out = this.getResponse().getOutputStream();
Document document = new Document(PageSize.A4);
RtfWriter2.getInstance(document, out);
document.open();
Paragraph context = new Paragraph();
//MyHtmlRequest myrequest = new MyHtmlRequest(this.getRequest());

String htmlContent=URLDecoder.decode(this.getRequest().getParameter("content"),"utf-8");
//String htmlContent  = new String(this.getRequest().getParameter("content").getBytes("ISO8859-1"), "UTF-8"); 
StyleSheet ss = new StyleSheet();
List htmlList = HTMLWorker.parseToList(new StringReader(htmlContent), ss);
for (int i = 0; i < htmlList.size(); i++) {
com.lowagie.text.Element e = (com.lowagie.text.Element) htmlList.get(i);
context.add(e);
}
document.add(context);
document.close();
out.close();
System.out.println("ok");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*Map<String, String> map = new HashMap<String, String>();
map.put("localPath", localPath);
json.setData(map)*/;
return null;


}

猜你喜欢

转载自blog.csdn.net/u013362412/article/details/77509641