java基於freemarker匯出指定word格式帶多圖片原始碼。
1.把word轉換成xml
2.把要替換的地方用${name}替代,name就是顯示的資料。例:
3.然後另存為ftl格式儲存。
4,附上程式碼
/**
* 匯出駕駛培訓電子教學日誌 word格式
*
* @return
*/
public boolean on_export_classhours_log() {
this.createWord();
if (true) {
msg = "<script type=\"text/javascript\">alert('匯出成功到D盤!');</script>";
} else {
msg = "<script type=\"text/javascript\">alert('匯出失敗!');</script>";
}
return true;
}
private Configuration configuration = null;
public void createWord() {
Map<String, Object> dataMap = new HashMap<String, Object>();
getData(dataMap);
configuration.setClassForTemplateLoading(this.getClass(), "");// 模板檔案所在路徑
Template t = null;
try {
t = configuration.getTemplate("bxjxrz.ftl"); // 獲取模板檔案
} catch (IOException e) {
e.printStackTrace();
}
File outFile = new File("D:/outFile/" + Math.random() * 10000 + ".doc"); // 匯出檔案
Writer out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(outFile)));
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
try {
t.process(dataMap, out); // 將填充資料填入模板檔案並輸出到目標檔案
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 匯出的學時資料
*
* @param dataMap
*/
private void getData(Map<String, Object> dataMap) {
String nowid = queryDetail.getValue();
VPClasshoursstatic vpchs = cdao.findById(nowid);
this.getContext().getSession().removeAttribute("vpchs");
this.getContext().getSession().setAttribute("vpchs", vpchs);
Date btime = vpchs.getBegintime();
String year = DateUtil.getYearPart(btime);
String month = DateUtil.getMonthPart(btime);
String day = DateUtil.getDayPart(btime);
SimpleDateFormat simDateFormat = new SimpleDateFormat("HH:mm");
String kssjString = simDateFormat.format(vpchs.getBegintime());
String jssjString = simDateFormat.format(vpchs.getEndtime());
PersonAllHoursViwe pAllHoursViwe = new PersonAllHoursViwe();
List<PersonAllHoursViwe> pAllHours = pDao.findByProperty("iccardid",
vpchs.getIccardid());
dataMap.put("jpjgmc", vpchs.getEnterprisename());
dataMap.put("xyxm", vpchs.getName());
dataMap.put("xybh", vpchs.getTableno());
dataMap.put("yyyy", year);
dataMap.put("mm", month);
dataMap.put("dd", day);
dataMap.put("image1", getImageOne());
dataMap.put("image2", getImageTwo());
dataMap.put("kssj", kssjString);
dataMap.put("jssj", jssjString);
dataMap.put("jly", vpchs.getTrainman());
dataMap.put("jlcph", vpchs.getCartno());
dataMap.put("cx", vpchs.getVehicletypename());
dataMap.put("zxs", pAllHours.get(0).getTotalhours());
dataMap.put("bcxs", vpchs.getEBtime());
dataMap.put("bclc", "");
dataMap.put("bcsd", "");
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for (int i = 0; i < 10; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("jpjgmc", i);
map.put("xyxm", i);
map.put("xybh", i);
map.put("yyyy", i);
map.put("mm", i);
map.put("dd", i);
map.put("image1", i);
map.put("image2", i);
map.put("kssj", i);
map.put("jssj", i);
map.put("jly", i);
map.put("jlcph", i);
map.put("cx", i);
map.put("zxs", i);
map.put("bcxs", i);
map.put("bclc", i);
map.put("bcsd", i);
list.add(map);
}
dataMap.put("list", list);
}
/**
* 獲取第一個照片
*
* @return
*/
private String getImageOne() {
List listImage = getImageList();
if (listImage.size()>0) {
return setImage((String) listImage.get(0));
}
return setImage(basePath+"/jxtrain/img/photo2.png");
}
/**
* 獲取第二個照片
*
* @return
*/
private String getImageTwo() {
List listImage = getImageList();
if (listImage.size()>=2) {
return setImage((String) listImage.get(1));
}
return setImage(basePath+"/jxtrain/img/photo2.png");
}
/**
* 接收照片地址轉換成輸出編碼
*
* @return
*/
private String setImage(String listImage) {
String imgFile = "";
InputStream is = null;
byte[] data = null;
if (!"".equals(listImage) && listImage != null) {
imgFile = listImage;
} else {
imgFile = basePath+"/jxtrain/img/photo2.png";
}
try {
BufferedImage bufferImg = ImageIO.read(new URL(imgFile));
ByteArrayOutputStream bs = new ByteArrayOutputStream();
ImageOutputStream imOut = ImageIO.createImageOutputStream(bs);
ImageIO.write(bufferImg, "jpg", imOut); // scaledImage1為BufferedImage,jpg為影象的型別
is = new ByteArrayInputStream(bs.toByteArray());
data = new byte[is.available()];
is.read(data);
is.close();
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);
}
/**
* 從資料庫獲取照片列表
*/
private List getImageList() {
listTime = new ArrayList();
listImg = new ArrayList();
String nowid = queryDetail.getValue();
if (nowid == null || nowid.equals("")) {
// condition.append("無相片!");
} else {
this.getContext().getSession().removeAttribute("persionid");
this.getContext().getSession().setAttribute("persionid", nowid);
Object object2 = this.getContext().getSession().getAttribute(
"persionid");
VPClasshoursstatic vpch = (VPClasshoursstatic) this.getContext()
.getSession().getAttribute("vpchs");
List personlist = cdao.querycondition("idcard ='"
+ vpch.getIdcard() + "'");
this.getContext().getSession().removeAttribute("personlist");
this.getContext().getSession().setAttribute("personlist",
personlist);
Object object1 = this.getContext().getSession().getAttribute(
"personlist");
String cartid = null;
String endtime = null;
String begintime = null;
String image = "image";// 預設查本月
// condition.append("<table cellspacing=\"0\" cellpadding=\"0\"
// border=\"0\"> <tr>");
SimpleDateFormat gobalFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
SimpleDateFormat NowFormat = new SimpleDateFormat("yyyyMM");
Connection conn = null;
PreparedStatement psmt = null;
ResultSet rs = null;
if (object1 != null && object2 != null) {
try {
List lsit = (ArrayList) object1;
String persionid = (String) object2;
for (int s = 0; s < lsit.size(); s++) {
VPClasshoursstatic vPClasshoursstatic = (VPClasshoursstatic) lsit
.get(s);
// System.out.printhln(vPClasshoursstatic.getId().toString());
if (vPClasshoursstatic.getId().toString().equals(
persionid)) {
cartid = vPClasshoursstatic.getCartid();
endtime = gobalFormat.format(vPClasshoursstatic
.getEndtime());
// System.out.println(endtime);
begintime = gobalFormat.format(vPClasshoursstatic
.getBegintime());
// System.out.println(endtime);
}
}
String TimeWhere = NowFormat.format(DateUtil
.getDateFromStr(begintime));// 條件時間
String NowTime = NowFormat.format(DateUtil.getNowDate());// 當前時間
if (!TimeWhere.equals(NowTime)) {
image += TimeWhere;
}
String sql = "select imageaddress,gpstime from "
+ image
+ " t where t.caroid='"
+ cartid
+ "' and t.gpstime between to_date('"
+ begintime
+ "','yyyy-mm-dd hh24:mi:ss') and to_date('"
+ endtime
+ "','yyyy-mm-dd hh24:mi:ss') order by t.gpstime asc";
// System.out.print(sql);
conn = DataUtil.getConnection();
psmt = conn.prepareStatement(sql);
rs = psmt.executeQuery();
while (rs.next()) {
// condition.append("<td>");
String imageaddress = rs.getString("imageaddress");
String gpstime = gobalFormat.format(rs
.getTimestamp("gpstime"));
listTime.add(gpstime);
listImg.add(basePath218 + imageaddress);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DataUtil.close(conn, psmt, rs);
}
}
}
return listImg;
}