itext導出圖片,Echart圖片,表格

由於項目的需要,需要導出的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;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章