使用freemarker导出word
2014-12-04 10:21
477 查看
使用freemarker首先需要引入freemarker包。
在一些需要复杂样式的word导出时,代码操作将会变得非常麻烦或根本无法实现。freemarker导出word主要分为三个步骤:1.制作模板;2.填充数据;3.输出word;
一.制作模板
首先编写word模板,记住图片的位置需要放一张图并设置好大小,然后将word文件保存为xml文件转换时应保存2003xml和勾选仅保存数据,
下一步修改xml文件中标签,在需要循环的地方加上<#list cardList as card> </#list>注 :cardlist 和card为自己命名数据的标识,需要输出使用${card.photo},在要输出图片的地方将base64替换为输出标签。例:
<w:pict><w:binData w:name="${"wordml://0200000"+card_index+2+".jpg"}"
xml:space="preserve">
${card.photo}
</w:binData><v:shape id="图片 1" o:spid="_x0000_i1025" type="#_x0000_t75"
style="width:80.25pt;height:99pt;visibility:visible;mso-wrap-style:square"><v:imagedata src="${"wordml://0200000"+card_index+2+".jpg"}" o:title="nophoto" /></v:shape></w:pict>
如果图片循环输出请修改图片的name和src不然输出将会是第一张图。修改完后将文件保存为ftl文件。模板制作完成
二。填充数据
try {
fileName = new String(fileName.getBytes("GBK"), "iso8859-1");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
OutputStream os = null;
Writer out = null;
Configuration configuration= null;
String templatePackagePath="/com/template";//模板包
configuration = new Configuration();
configuration.setDefaultEncoding("UTF-8");//设置默认编码方式
configuration.setClassForTemplateLoading(this.getClass(),templatePackagePath);
Template t = null;
try {
t = configuration.getTemplate("***.ftl");模板
} catch (IOException e) {
e.printStackTrace();
}
Map<String, Object> dataMap = new HashMap<String, Object>();
List<Map<String, Object>> cardList1=new ArrayList<Map<String,Object>>();
for(int i=0;i<yxzg.length;i++)
{ Gy_cardDto dto=cardList.get(i);
Map<String, Object> map=new HashMap<String, Object>();
String photo=getImgString(***);//将图片转换为base64码
map.put("photo",photo);//填充数据
map.put("index", i+1);
cardList1.add(map);
}
dataMap.put("cardList", cardList1);
/**
*将图片转换为base64码
*/
private String getImgString(String path)
{
String img=null;
InputStream in;
byte[] picdata=null;
try {
in=new FileInputStream(path);
picdata=new byte[in.available()];
in.read(picdata);
in.close();
} catch (Exception e) {
e.printStackTrace();
}
BASE64Encoder encoder=new BASE64Encoder();
img=encoder.encode(picdata);
return img;
}
三.输出文件
try{
os=getResponse().getOutputStream();
getResponse().reset();// 清空输出流
getResponse().setHeader("Content-disposition", "attachment; filename=" + fileName);// 设定输出文件头
out = new BufferedWriter(new OutputStreamWriter(os,"utf-8"));
}catch(Exception e)
{
}
try {
t.process(dataMap, out);
os.close();
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
在一些需要复杂样式的word导出时,代码操作将会变得非常麻烦或根本无法实现。freemarker导出word主要分为三个步骤:1.制作模板;2.填充数据;3.输出word;
一.制作模板
首先编写word模板,记住图片的位置需要放一张图并设置好大小,然后将word文件保存为xml文件转换时应保存2003xml和勾选仅保存数据,
下一步修改xml文件中标签,在需要循环的地方加上<#list cardList as card> </#list>注 :cardlist 和card为自己命名数据的标识,需要输出使用${card.photo},在要输出图片的地方将base64替换为输出标签。例:
<w:pict><w:binData w:name="${"wordml://0200000"+card_index+2+".jpg"}"
xml:space="preserve">
${card.photo}
</w:binData><v:shape id="图片 1" o:spid="_x0000_i1025" type="#_x0000_t75"
style="width:80.25pt;height:99pt;visibility:visible;mso-wrap-style:square"><v:imagedata src="${"wordml://0200000"+card_index+2+".jpg"}" o:title="nophoto" /></v:shape></w:pict>
如果图片循环输出请修改图片的name和src不然输出将会是第一张图。修改完后将文件保存为ftl文件。模板制作完成
二。填充数据
try {
fileName = new String(fileName.getBytes("GBK"), "iso8859-1");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
OutputStream os = null;
Writer out = null;
Configuration configuration= null;
String templatePackagePath="/com/template";//模板包
configuration = new Configuration();
configuration.setDefaultEncoding("UTF-8");//设置默认编码方式
configuration.setClassForTemplateLoading(this.getClass(),templatePackagePath);
Template t = null;
try {
t = configuration.getTemplate("***.ftl");模板
} catch (IOException e) {
e.printStackTrace();
}
Map<String, Object> dataMap = new HashMap<String, Object>();
List<Map<String, Object>> cardList1=new ArrayList<Map<String,Object>>();
for(int i=0;i<yxzg.length;i++)
{ Gy_cardDto dto=cardList.get(i);
Map<String, Object> map=new HashMap<String, Object>();
String photo=getImgString(***);//将图片转换为base64码
map.put("photo",photo);//填充数据
map.put("index", i+1);
cardList1.add(map);
}
dataMap.put("cardList", cardList1);
/**
*将图片转换为base64码
*/
private String getImgString(String path)
{
String img=null;
InputStream in;
byte[] picdata=null;
try {
in=new FileInputStream(path);
picdata=new byte[in.available()];
in.read(picdata);
in.close();
} catch (Exception e) {
e.printStackTrace();
}
BASE64Encoder encoder=new BASE64Encoder();
img=encoder.encode(picdata);
return img;
}
三.输出文件
try{
os=getResponse().getOutputStream();
getResponse().reset();// 清空输出流
getResponse().setHeader("Content-disposition", "attachment; filename=" + fileName);// 设定输出文件头
out = new BufferedWriter(new OutputStreamWriter(os,"utf-8"));
}catch(Exception e)
{
}
try {
t.process(dataMap, out);
os.close();
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
相关文章推荐
- 使用freemarker导出Word
- 使用FreeMarker导出复杂的Word(具体实现)
- 使用freemarker导出word文档包含多张图片
- java中使用freemarker导出word文档
- Java使用freemarker导出word和excel
- Java使用freemarker导出word文档
- 超详细 Java使用freemarker模板技术导出word
- java使用freemarker导出word
- 使用freemarker实现word导出
- 使用freemarker导出word(含图片 表格动态行)
- Java 使用Freemarker导出word 文档,解决当一段内容中既有文字又有图片的问题
- word导出(使用freemarker模板导出)
- java使用freemarker模板导出word(带有合并单元格)文档
- 使用FreeMarker导出Word
- 使用freemarker导出Word
- 使用FreeMarker自定义导出word
- 使用freemarker 导出word供别人下载
- 使用freemarker按模板导出带表格word文档
- 使用freemarker导出word,wps可以打开,office打不开问题
- 使用FreeMarker导出word文档