javaword导出ole对象_java导出并下载带有图⽚的word
⼀般的导出,不对图⽚进⾏处理的话,下载后图⽚由于访问问题不能在word(尤其是office)中正常显⽰.所以导出时最好将图⽚转成base64,将base64的图⽚放到word中再导出即可;此处记录下⼯作中的导出⽅式,以备后⽤;
为达到导出带有样式格式的⽂本如:
⽬标word
有以下三步来实现:
1.⽤html实现上⾯的样式:
其中标红的地⽅是需要⽤数据替换的内容,可以理解为占位符;
2.提前整理需要的数据结构,可以使个Map或者是个list,将上⾯的html作为字符串处理,通过对所需数据的遍历,将对应数据放到对应的位置;
public String createPaperHtml(Map<String, Object> dataMap,Integer type,List<String> info){
String dataHtml=null;
String from = "<!DOCTYPE html>n" +
"<html lang="en">n" +
"<head>n" +
" <meta charset="UTF-8">n" +
" <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">n" +
" <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">n" +
" <title>试卷试题</title>n" +
对象图片高清" <style>n" +
" html,body,p,ul,li,h1,h2,h3,h4,h5,h6{n" +
" margin: 0;n" +
" padding: 0;n" +
" }n" +
" html,body{n" +
" html,body{n" +
" width: 100%;n" +
" height: 100%;n" +
" background-color: #ffffff;n" +
" font-family: '宋体';n" +
" }n" +
"n" +
" .clearfix:after{n" +
" display: block;n" +
" content: ".";n" +
" height: 0;n" +
" clear: both;n" +
" visibility: hidden;n" +
" }n" +
" .fl{n" +
" float: left;n" +
" }n" +
" .fr {n" +
" float: right;n" +
" }n" +
"n" +
"n" +
" .exam_paper_container {n" +
" padding: 0 20px;n" +
" }n" +
" .exam_paper_container .exam_paper_title {n" +
" font-size: 20px;n" +
" text-align: center;n" +
" padding-top: 30px;n" +
" padding-bottom: 20px;n" +
" }n" +
"n" +
" .exam_paper_container .questions_wrapper h3{n" +
" font-size: 16px;n" +
" padding-bottom: 16px;n" +
" }n" +
"n" +
" .exam_paper_container .questions_wrapper .sq_question_body {n" +
" padding-bottom: 10px;n" +
" }n" +
"n" +
" .exam_paper_container .questions_wrapper .sq_question_structure {n" + " font-size: 14px;n" +
" line-height: 20px;n" +
" padding-bottom: 4px;n" +
" }n" +
"n" +
" .exam_paper_container .sq_question_structure .structure_type {n" +
" color: blue;n" +
" }n" +
"n" +
" .exam_paper_container .sq_question_structure .structure_answer {n" + " padding: 6px 0 0 10px;n" +
" }n" +
" </style>n" +
"</head>n" +
"<body>";
String end = "</div</div></body></html>";
StringBuffer buffer = new StringBuffer();
buffer = buffer.append(from)
.append("<div class="exam_paper_container">")
.append("<h2 class="exam_paper_title">")
.("title"))//试卷标题
.append("</h2>")
.append("<div class="questions_wrapper">");
//获取试卷题⽬信息;
//获取试卷题⽬信息;
List<QuestionGroupPvo> quesGroup = CastUtils.("quesGroups"));
if(!CollectionUtils.isNullOrEmpty(quesGroup)){
Integer qnum = 1;
for(QuestionGroupPvo gq:quesGroup){
//⼤题
String qnumCh = String())+"、"+gq.getGroupTitle(); buffer.append(" <div class="questions_wrapper_item">")
.append("<h3>")
.append(qnumCh)
.append("(共 ")
.QuestLen())
.append("题;共")
.GroupScore())
.append("分)")
.append("</h3>");
if(!CollectionUtils.Questions())){
//⼩题
int i=0;
for (QuestionPvo Questions()){
buffer.append("<div class="questions_wrapper_item_sq">")
.append("<div class="sq_question_body">")//题⼲
.append(i+1).append(".");
ains("score")&&Score()!=null) {
buffer.append("(").Score()).append("分) ");
}
buffer.Body())
.append("</div>");
if(type.equals(2)) {//教师⽤卷
/
/答案
if (ains("answer")&&!StringUtils.Result())) {
buffer.append("<div class="sq_question_structure">n" +
" <span class="structure_type">【答案】</span><span>")
.Result())
.append("</span>n" +
" </div>");
}
//考点
if (ains("knowledge")&&!StringUtils.Knowledge())) {
buffer.append("<div class="sq_question_structure">n" +
" <span class="structure_type">【考点】</span><span>");
String knowPointStr = Knowledge());
buffer.append(knowPointStr)
.append("</span>n" +
" </div>");
}
//解析
if (ains("analysis")&&!StringUtils.Analysis())) {
buffer.append("<div class="sq_question_structure">n" +
" <span class="structure_type">【解析】</span><span>")
.Analysis())
.
append("</span>n" +
" </div>");
}
}
buffer.append("</div>");
i++;
}
}
buffer.append("</div>");
qnum++;
}
}
if(type.equals(1)){//学⽣⽤卷答案解析部分
buffer.append("<div class="exam_paper_container">n" +
" <h2 class="exam_paper_title">答案解析部分</h2> n" +
" <div class="questions_wrapper">");
Integer qq=1;
for(QuestionGroupPvo gqInfo:quesGroup){
String qnumCh1 = String())+"、"+GroupTitle();
buffer.append("<p class="question_item_title">")
.append(qnumCh1)
.append("</p>");
if(!CollectionUtils.Questions())){
int j=0;
for(QuestionPvo Questions()){
//答案:
ains("answer")) {
String answerStr = !StringUtils.Result()) ? Result() : "";
buffer.append("<div class="questions_wrapper_item_sq">n" +
" <div class="clearfix">n" +
" <span class="fl">")
.append(j + 1)
.append(".</span>n" +
" <div class="sq_question_structure fl">n" +
" <span class="structure_type">【答案】</span><span>")
.append(answerStr)
.append("</span>n" +
" </div>n" +
" </div>");
}
//考点:
ains("knowledge")) {
String knowPointStr = !StringUtils.Knowledge()) ? Knowledge()) : ""; buffer.append("<div class="sq_question_structure">n" +
" <span class="structure_type">【考点】</span><span>")
.append(knowPointStr)
.append("</span>n" +
" </div>");
}
//解析;
ains("analysis")) {
String analysisStr = !StringUtils.Analysis()) ? Analysis() : "";
buffer.append(" <div class="sq_question_structure">n" +
" <span class="structure_type">【解析】</span><span>")
.append(analysisStr)
.
append("</span></div>");
}
j++;
}
}
qq++;
}
buffer.append("</div></div>");
}
}
dataHtml =buffer.append(end).toString();
return dataHtml;
return dataHtml;
}
3.将上⾯处理过的dataHtml通过处理(将包含的图⽚转化成base64)再导出到word即可
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论