转载请标明出处:
https://blog.csdn.net/weixin_41965979/article/details/80969287
本文出自付付讶的博客
1:所需jar包:freemarker(我用的是freemarker-2.3.18);
首先准备一个word模板(英文部分是要被你的数据所代替的)
2:把这个模板保存为xml格式
3:页面上写好按钮和下拉框选择第几组图片
<div style="width:100%;border:1px solid white;border-top:0px">
<select id="imageoption">
<option value="first" selected>第一组照片</option>
<option value="second">第二组照片</option>
<option value="third">第三组照片</option>
</select>
</div>
<input type="hidden" value="" name="first" id="before"></input>
<!-- 多选框选中传的ids -->
<input type="hidden" id="ids" name="ids" value=""></input>
<div class="search">
<input type="button" value="导出为word" id="exportword1" οnclick="pass_word()">
</input>
</div>
4:导出按钮触发的事件
//导出word
function pass_word(){
//判断选择第几组照片
var options=$("#imageoption option:selected").val();
if(options=="first"){
$("#before").val("prefirst");
}else if(options=="second"){
$("#before").val("underfirst");
}else{
$("#before").val("afterfirst");
}
var names="";
//road为复选框的name值
var checkb = document.getElementsByName("road");
//选择导出多条时给name拼接一个逗号
for(var i=1;i<checkb.length;i++)
{
if(checkb[i].checked==true){names +="'"+checkb[i].value+"'"+",";}
//alert(checkb[i].value);
}
//赋值给ids
$("#ids").val(names);
//form表单上的那个action为空,在这赋值
document.getElementById("searchForm_word").action="<%=path%>/word.do";
document.getElementById("searchForm_word").submit();
}
5:通过word.do跳转到了wordExportServlet
public class WordExportServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//分别为页面左侧按钮的第一组照片,第二组照片,第三组照片
String first=request.getParameter("first");
String ids=request.getParameter("ids"); //获取传过来的ids的值,等于是选中的数据的那个name
List<Table_2> list=getList(ids);
Map<String, Object> dataMap = new HashMap<String, Object>();
List<Map<String, Object>> list1 = new ArrayList<Map<String, Object>>();
for (int i = 0; i < list.size(); i++) {
//分别判断沥青不为空和获取沥青的值然后求和
String lq1= list.get(i).getTaskAsphalt_9cm_10()==null||list.get(i).getTaskAsphalt_9cm_10().equals("")?"0":list.get(i).getTaskAsphalt_9cm_10();
String lq2= list.get(i).getTaskAsphalt_5cm_10()==null||list.get(i).getTaskAsphalt_5cm_10().equals("")?"0":list.get(i).getTaskAsphalt_5cm_10();
String lq3= list.get(i).getAsphalt_9cm_10_400()==null||list.get(i).getAsphalt_9cm_10_400().equals("")?"0":list.get(i).getAsphalt_9cm_10_400();
String lq4= list.get(i).getAsphalt_5cm_10_400()==null||list.get(i).getAsphalt_5cm_10_400().equals("")?"0":list.get(i).getAsphalt_5cm_10_400();
String lq5= list.get(i).getTaskAsphalt_9cm_400()==null||list.get(i).getTaskAsphalt_9cm_400().equals("")?"0":list.get(i).getTaskAsphalt_9cm_400();
String lq6= list.get(i).getTaskAsphalt_5cm_400()==null||list.get(i).getTaskAsphalt_5cm_400().equals("")?"0":list.get(i).getTaskAsphalt_5cm_400();
double liqing_sum=Double.parseDouble(lq1)+Double.parseDouble(lq2)+Double.parseDouble(lq3)+Double.parseDouble(lq4)+Double.parseDouble(lq5)+Double.parseDouble(lq6);
//分别判断步道不为空和获取步道的值然后求和
String bd1= list.get(i).getTaskSidewalk()==null||list.get(i).getTaskSidewalk().equals("")?"0":list.get(i).getTaskSidewalk();
String bd2= list.get(i).getMangdao()==null||list.get(i).getMangdao().equals("")?"0":list.get(i).getMangdao();
String bd3= list.get(i).getTaskPlaster()==null||list.get(i).getTaskPlaster().equals("")?"0":list.get(i).getTaskPlaster();
String bd4= list.get(i).getTaskMachine_stone()==null||list.get(i).getTaskMachine_stone().equals("")?"0":list.get(i).getTaskMachine_stone();
String bd5= list.get(i).getShicaibudao()==null||list.get(i).getShicaibudao().equals("")?"0":list.get(i).getShicaibudao();
String bd6= list.get(i).getShicaimangdao()==null||list.get(i).getShicaimangdao().equals("")?"0":list.get(i).getShicaimangdao();
double budao_sum=Double.parseDouble(bd1)+Double.parseDouble(bd2)+Double.parseDouble(bd3)+Double.parseDouble(bd4)+Double.parseDouble(bd5)+Double.parseDouble(bd6);
//分别判断路缘石不为空和获取路缘石的值然后求和
String lys1= list.get(i).getTaskCurb()==null||list.get(i).getTaskCurb().equals("")?"0":list.get(i).getTaskCurb();
String lys2= list.get(i).getShicailuyuanshi()==null||list.get(i).getShicailuyuanshi().equals("")?"0":list.get(i).getShicailuyuanshi();
double lys_sum=Double.parseDouble(lys1)+Double.parseDouble(lys2);
//key为模板中的英文单词,value赋值
Map<String, Object> map = new HashMap<String, Object>();
map.put("number", list.get(i).getTaskNumber());
map.put("date", list.get(i).getTaskDate());
map.put("address", list.get(i).getTaskPlace());
map.put("office", list.get(i).getTaskOffice());
map.put("liqing", String.valueOf(liqing_sum));
map.put("budao", String.valueOf(budao_sum));
map.put("luyuanshi", String.valueOf(lys_sum));
//判断选第几组照片和路径
String one ="";
String two ="";
String three ="";
if("prefirst".equals(first)){
one=list.get(i).getPrefirst();
two=list.get(i).getPresecond();
three=list.get(i).getPrethird();
}else if("underfirst".equals(first)){
one=list.get(i).getUnderfirst();
two=list.get(i).getUndersecond();
three=list.get(i).getUnderthird();
}else if("afterfirst".equals(first)){
one=list.get(i).getAfterfirst();
two=list.get(i).getAftersecond();
three=list.get(i).getAfterthird();
}else{
}
if(one==null||"".equals(one)){
map.put("image2",GetImageStrFromPath("C:/Program Files/Apache Software Foundation/Tomcat 8.5/webapps/lalio/free.jpg"));
}else{
map.put("image2",GetImageStrFromPath("C:/Program Files/Apache Software Foundation/Tomcat 8.5/webapps/lalio/"+one));
}if(two==null||"".equals(two)){
map.put("image",GetImageStrFromPath("C:/Program Files/Apache Software Foundation/Tomcat 8.5/webapps/lalio/free.jpg"));
}else{
map.put("image",GetImageStrFromPath("C:/Program Files/Apache Software Foundation/Tomcat 8.5/webapps/lalio/"+two));
}if(three==null||"".equals(three)){
map.put("image1",GetImageStrFromPath("C:/Program Files/Apache Software Foundation/Tomcat 8.5/webapps/lalio/free.jpg"));
}else{
map.put("image1",GetImageStrFromPath("C:/Program Files/Apache Software Foundation/Tomcat 8.5/webapps/lalio/"+three));
}
list1.add(map);
}
//table2为模板xml里循环的名称
dataMap.put("table2", list1);
WordUtils.exportMillCertificateWord(request, response, dataMap, "abc", "fctestpaper.ftl");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
/**
* @Title: GetImageStrFromPath
* @Description: TODO(将一张本地图片转化成Base64字符串)
* @param imgPath
* @return
*/
public static String GetImageStrFromPath(String imgPath) {
InputStream in = null;
byte[] data = null;
// 读取图片字节数组
try {
in = new FileInputStream(imgPath);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
// 对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
// 返回Base64编码过的字节数组字符串
return encoder.encode(data);
}
//查询数据库的数据
public List<Table_2> getList(String ids) {
List<Table_2> list=new ArrayList<Table_2>();
//HashMap<String,Object> map=new HashMap<String, Object>();
//把选中的多条给他拆分
String sql_id="and taskName in "+"("+ids.substring(0, ids.length()-1)+")";
//连接数据库查询
Connection conn=null;
try{
conn=Utils.getConn();
String sql="select * from Table_2 where 1=1 ";
sql+=sql_id;
PreparedStatement pstm=conn.prepareStatement(sql);
ResultSet rs=pstm.executeQuery();
while(rs.next()){
Table_2 tab=new Table_2();
tab.setTaskDate(rs.getString("taskDate"));
tab.setFinishDate(rs.getString("FinishDate"));
tab...
....
list.add(tab);
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(conn!=null){
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
return list;
}
}
6:用工具打开一开始转成xml格式的模板
在body后面写在循环(table2在servlet里为获取的所有值,servlet里有注释,plan2为别名),后面属性的英文单词全部换成这种格式写好
7:改好之后把后缀改成ftl格式,然后弄到项目里(注意servlet引入的要和你的ftl的名字一样)然后就可以导出了
8:导出效果(暂无图片是我自己引入的图片,代表数据库这条数据没有图片)
来源:CSDN
作者:付付讶
链接:https://blog.csdn.net/weixin_41965979/article/details/80969287