由于项目的需要,需要导出的word包括表格,图片
需要的jar包
需要说明的是必须是这个版本哦,而且我也测试过了哦。首先感谢http://blog.csdn.net/aeolus1019/article/details/7973255的作者。及参考地址
代码如下:
echartImage的获取是从echarts的对象中getDateUrl()获取的,是base64位的。
经过处理:
echartImage = echartImage.replaceAll(" ", "+");
String[] arr = echartImage.split("base64,");
if (arr.length > 1) {
echartImage=arr[1]=;
}
public void exportImage(String echartImage,HttpServletResponse response,String fileName){
try {
//建立一个书写器与document对象关联,通过书写器可以将文档写入到输出流中
RtfWriter2.getInstance(document, os);
document.open();
// echartImage = "/9j/"+echartImage;
BASE64Decoder decoder = new BASE64Decoder();
byte[] buffer = decoder.decodeBuffer(echartImage);
//添加图片
Image img = Image.getInstance(buffer);
img.setAbsolutePosition(0, 0);
img.setAlignment(Image.ALIGN_CENTER);
img.scaleAbsolute(100,100);
img.scalePercent(50);
img.scalePercent(50, 50);
img.setRotation(30);
img.setOriginalType(Image.ORIGINAL_PNG);
document.add(img);
setHeader(response,fileName);
} catch (Exception e) {
e.printStackTrace();
}finally{
document.close();
System.out.println("完成");
}
}
//表格
public void exportWordTable(String titleString,HttpServletResponse response,String fileName){
try {
/** 创建Document对象(word文档) */
Rectangle rectPageSize = new Rectangle(PageSize.A4);
rectPageSize = rectPageSize.rotate();
// String fileName="F:/企业详细信息登记表_"+System.currentTimeMillis()+".doc";
// OutputStream out = new FileOutputStream(fileName);
/** 建立一个书写器与document对象关联,通过书写器可以将文档写入到输出流中 */
RtfWriter2.getInstance(document, os);
document.open();
/** 标题字体*/
RtfFont titleFont = new RtfFont("微软雅黑", 12, Font.BOLD,Color.BLACK);
RtfFont contentFont = new RtfFont("微软雅黑", 12, Font.NORMAL,Color.BLACK);
Table table = getTable();
/** 第一行(标题)*/
Paragraph title = new Paragraph(titleString);
// 设置标题格式对其方式
title.setAlignment(Element.ALIGN_CENTER);
document.add(title);
// 设置第一行空的列数(缩进)
// context.setFirstLineIndent(20);
Cell cell=null;
if(null != columnNames && columnNames.length>0){
for(int i=0;i<columnNames.length;i++){
Paragraph p =new Paragraph(columnNames[i],titleFont);
p.setAlignment(Element.ALIGN_CENTER);
p.setFont(titleFont);
cell=new Cell(p);
cell.setHeader(true);
table.addCell(cell);
}
}
if(null != dataList && dataList.size()>0){
for(int i=0;i<dataList.size();i++){
Map<String,Object> map = dataList.get(i);
if(null != map && map.size()>0 && null != mappingKeys && mappingKeys.length>0){
for(int j=0;j<mappingKeys.length;j++){
Paragraph p =null;
Object value = map.get(mappingKeys[j]);
if(null != value){
if(j==0){
p =new Paragraph(value.toString(),titleFont);
}else{
p =new Paragraph(value.toString(),contentFont);
}
}else{
if(j==0){
p =new Paragraph("",titleFont);
}else{
p =new Paragraph("",contentFont);
}
}
p.setAlignment(Element.ALIGN_CENTER);
cell=new Cell(p);
table.addCell(cell);
}
}
}
}
document.add(table);
setHeader(response,fileName);
} catch (Exception e) {
e.printStackTrace();
}finally{
document.close();
System.out.println("完成");
}
}
private Table getTable() {
Table table = null;
try {
/** 表格设置 第一个参数是列,第二个参数是行*/
table = new Table(columnNames.length, dataList.size());
// table = new Table(4, 3);
/** 居中显示*/
table.setAlignment(Element.ALIGN_CENTER);
/** 自动填满 */
table.setAutoFillEmptyCells(true);
table.setBorderWidth(5); // 边框宽度
table.setBorderColor(new Color(0, 125, 255)); // 边框颜色
table.setPadding(12);// 衬距,看效果就知道什么意思了
table.setSpacing(0);// 即单元格之间的间距
table.setBorder(5);// 边框
} catch (Exception e) {
e.printStackTrace();
}
return table;
}