Excel,Pdf,Xml 文件操作(二)

首先,还是列出接口IExport.java
public interface IExport {
 /**
  * 文件下载方法
  * @param outfileName,输出显示文件名
  * @param outfile,需要下载的文件名
  * @param response
  */
     public int download(String outfileName,String outfile,HttpServletResponse response);
     /**
   * 文件下载方法
   * @param outfileName,输出显示文件名
   * @param outfile,需要下载的文件名
   * @param response
   */
     public int download(String outfileName,String outfile,WebResponse response);
     /**
      * 数据导出方法,将数据导出到outFile中,但是不会下载 ,如需下载还需调用download方法
      * @param headerMap,显示表头内容
      * 构造表头内容:
      * headerMap.put("name",rolename) ;
      * name位list中的key,rolename位需要代替显示的内容
      * @param outFile,导出到文件
      * @param value,数据对象
      */
     public int export(Map headerMap,String outFile,List value);
     /**
      * 数据导出方法,将数据导出到outFile中,但是不会下载 ,如需下载还需调用download方法
      * @param outFile,导出到文件
      * @param value,数据
      */
     public int export(String outputFile,List value) ;
}

这个接口提供了四个方法,两个文件下载方法,在前面Download.java中已经实现。两个导出方法,其中参数在方法前面已经说明(注:List value中,value并不是数值列表,而是对象列表,例如有一个持久化对象User.java,那么这个value就可以是User,java的list)

二:  导出

导出具体实现原理:

     首先循环对象,取出第一个对象,从这个对象中获取所有域名,然后比较这些域名和headMap中对应的字段,如相同就从headMap中取出对应地displayName(这样做是为了控制数据库中字段名对普通用户屏蔽)。当然,也可以通过在headMap中直设定需要导出的域名。没有写入headMap的域将不会被导出。

    取出displayName以后,就写入excel文件中。然后,就依次循环对象value,通过反射技术从对象中取出值,写入到相应的displayName下。

1.这里用到的api时poi,可以到http://jakarta.apache.org/poi/上下载

2。poi的基本操作这里就不多说了,网上有很多关于这方面的资料

3。

ExcelExport.java

package org.xiaohongli.common.export;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.xiaohongli.common.IExport;

public class ExcelExport extends Download implements IExport{
  private List<Map<String, Comparable>> list  ;
  private String fileName ;
  private String sheetName ;
  
  public ExcelExport(){
   list = new ArrayList<Map<String, Comparable>>() ;
  }

 public int export(Map headerMap, String outFile, List value) {
  if(null==outFile||outFile.equals(""))
   outFile = Util.TEMP_EXCEL_FILE ;
  try {
    HSSFWorkbook workbook = new HSSFWorkbook() ;
    HSSFSheet sheet = workbook.createSheet() ;
   
    HSSFRow row = sheet.createRow(0) ;
    List<String> displayFieldList = new ArrayList<String>() ;
    for(int i=0 ;i<(value==null?0:value.size());i++){//循环对象value
     Object obj = value.get(i) ;
     Method[] method = obj.getClass().getDeclaredMethods();//获取对象里所有方法
     List<String> headerlist = new ArrayList<String>() ;
     for(int i1=0; i1<method.length;i1++){
      String str = method[i1].getName() ;
      if(str.indexOf("get")==0){
       String fieldname = Util.getFieldName(str) ;//获取值字段名
       headerlist.add(fieldname) ;//将字段名添加到list 中(判断有多少个field)
      }else{
       continue ;
      }
     }


     int insertColumn = 0 ;
     for(int i2 = 0 ;i2 <( headerlist==null?0:headerlist.size());i2++){
      String headerValue = Util.convertNull((String)headerlist.get(i2)).toLowerCase() ;
      String headerDisplay = (String)headerMap.get(headerValue) ;//从headMap中取出对应要显示的值
      if(headerDisplay!=null&&!"".equals(headerDisplay)){
       HSSFCell cell = row.createCell((short) insertColumn) ;
       cell.setEncoding(HSSFCell.ENCODING_UTF_16);//设置编码,可以正确显示中文
       cell.setCellValue(headerDisplay);
       displayFieldList.add(headerValue);
       insertColumn ++ ;
      }      
       
     }
    
     break ;
   }

   for(int i=0 ;i<(value==null?0:value.size());i++){
     Object obj = value.get(i) ;
     Method[] method = obj.getClass().getDeclaredMethods();
     HSSFRow row1 = sheet.createRow(i+2) ;
     List<String[]> valuelist = new ArrayList<String[]>() ;
     for(int i1=0; i1<method.length;i1++){
     String name = method[i1].getName() ;
     if(name.indexOf("set")==0) continue ;
     if(name.indexOf("get")==0){
      String fieldname = Util.getFieldName(name) ;
      Object value1 = method[i1].invoke(obj,new Object[0]);
      String value2 = value1==null?null:value1.toString() ;

      String[] str = new String[]{fieldname.toLowerCase(),value2} ;
      valuelist.add(str) ;
     }
     }
     int insertColumn = 0 ;
     for(int i2 = 0 ;i2 <( valuelist==null?0:valuelist.size());i2++){
      String[] str = (String[])valuelist.get(i2) ;
      String fn = str[0] ;
      String fv = str[1] ;

      if(displayFieldList.contains(fn)){
       HSSFCell cell = row1.createCell((short) insertColumn) ;
       cell.setEncoding(HSSFCell.ENCODING_UTF_16);
          cell.setCellValue(fv);
          insertColumn++ ;
      }
     }
    }
    
   FileOutputStream fOut = new FileOutputStream(outFile);
   sheet.setAutobreaks(true) ;
      workbook.write(fOut);
      fOut.flush();
      fOut.close();
  
  } catch (SecurityException e) {
   e.printStackTrace();
   return Util.ERROR ;
  } catch (IllegalArgumentException e) {
   e.printStackTrace();
   return Util.ERROR ;
  } catch (IllegalAccessException e) {
   e.printStackTrace();
   return Util.ERROR ;
  } catch (InvocationTargetException e){
   e.printStackTrace();
   return Util.ERROR ;
  } catch (FileNotFoundException e) {
   e.printStackTrace();
   return Util.ERROR ;
  } catch (IOException e) {
   e.printStackTrace();
   return Util.ERROR ;
  }
  return Util.SUCCESS ; 
 }

 public int export(String outFile, List value) {
  if(null==outFile||outFile.equals(""))
   outFile = Util.TEMP_EXCEL_FILE ;
   try {
    HSSFWorkbook workbook = new HSSFWorkbook() ;
    HSSFSheet sheet = workbook.createSheet() ;
   
    HSSFRow row = sheet.createRow(0) ;
    for(int i=0 ;i<(value==null?0:value.size());i++){
     Object obj = value.get(i) ;
     Method[] method = obj.getClass().getDeclaredMethods();
     List<String> headerlist = new ArrayList<String>() ;
     for(int i1=0; i1<method.length;i1++){
      String str = method[i1].getName() ;
      if(str.indexOf("set")==0){
       String fieldname = Util.getFieldName(str) ;
       headerlist.add(fieldname) ;
      }else{
       continue ;
      }
     }
     for(int i2 = 0 ;i2 <( headerlist==null?0:headerlist.size());i2++){
      HSSFCell cell = row.createCell((short) i2) ;
         cell.setCellValue(Util.convertNull((String)headerlist.get(i2)));
     }
    
     break ;
   }
   for(int i=0 ;i<(value==null?0:value.size());i++){
     Object obj = value.get(i) ;
     Method[] method = obj.getClass().getDeclaredMethods();
     HSSFRow row1 = sheet.createRow(i+2) ;
     List<String> valuelist = new ArrayList<String>() ;
     for(int i1=0; i1<method.length;i1++){
     String name = method[i1].getName() ;
     if(name.indexOf("set")==0) continue ;
     if(name.indexOf("get")==0){
      Object value1 = method[i1].invoke(obj,new Object[0]);
      String value2 = value1==null?null:value1.toString() ;
      valuelist.add(value2) ;
     }
     }
     for(int i2 = 0 ;i2 <( valuelist==null?0:valuelist.size());i2++){
      HSSFCell cell = row1.createCell((short) i2) ;
         cell.setCellValue(Util.convertNull((String)valuelist.get(i2)));
     }
    }
   FileOutputStream fOut = new FileOutputStream(outFile);
   sheet.setAutobreaks(true) ;
       workbook.write(fOut);
       fOut.flush();
       fOut.close();
  } catch (SecurityException e) {
   e.printStackTrace();
   return Util.ERROR ;
  } catch (IllegalArgumentException e) {
   e.printStackTrace();
   return Util.ERROR ;
  } catch (IllegalAccessException e) {
   e.printStackTrace();
   return Util.ERROR ;
  } catch (InvocationTargetException e){
   e.printStackTrace();
   return Util.ERROR ;
  } catch (FileNotFoundException e) {
   e.printStackTrace();
   return Util.ERROR ;
  } catch (IOException e) {
   e.printStackTrace();
   return Util.ERROR ;
  }
  return Util.SUCCESS ;
 }

 

//这里还提供两个跟这个接口无关的两个方法,可以写值到指定的cell

/**
     *
     * @param 使用:
     * @param 1。 获取文件路径和文件名,对应file
     * @param 2。 获取sheet name,如只有一个sheet ,sheetName可为空null
     * @param 3。 row 为指定的行号 ,从1开始
     * @param 4。 cell 为制定的cell ,从1开始
     * @param 5。 value 为要写入的数据(都要转化为字符型数据)
     * @param 6。 调用方法 ExcelUtil.writeAppointPosition(file,sheetName,row,cell,value) ;
     */
    public static void writeAppointPosition(String file ,String sheetName,int row,int cell,String value){
     row = row -1 ;
     cell = cell - 1 ;
     try {
   HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(file));
   HSSFSheet sheet = null ;//workbook.getSheet(sheetName);
   if(sheetName==null||sheetName.equals("")) sheet = workbook.getSheetAt(0);
   else sheet = workbook.getSheet(sheetName);
   
   HSSFRow row1 = null ;//sheet.getRow(row) ;
   if( row1 == null ) row1 = sheet.createRow(row) ;
   else row1 = sheet.getRow(row) ;
   
   HSSFCell cell1 = null ;//row1.getCell((short)cell) ;
   if(cell1==null)cell1 = row1.createCell((short)cell) ;
   else cell1 = row1.getCell((short)cell) ;
   
   cell1.setCellValue(value==null?"":value) ;
   
   FileOutputStream fOut = new FileOutputStream(file);
   workbook.write(fOut);
   fOut.flush();
   fOut.close();
   
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
    }
   
    public   ExcelExport write(int row,int cell,String value){
     Map<String, Comparable> map = new HashMap<String, Comparable>() ;
     map.put("row",row) ;
     map.put("cell",cell) ;
     map.put("value",value) ;
     list.add(map) ;
     return this ;
    }
   
    /**
     *
     * @param file
     * @param sheetName
     * @param list
     */
    public static void writeAppointPosition(String file ,String sheetName,List list){
   
    
     try {
    HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(file));
    HSSFSheet sheet = null ;//workbook.getSheet(sheetName);
    if(sheetName==null||sheetName.equals("")) sheet = workbook.getSheetAt(0);
    else sheet = workbook.getSheet(sheetName);
    
    
     for(int i=0 ;i<(list==null?0:list.size());i++){
         Map map      = (Map)list.get(i) ;
         int row      = (Integer)map.get("row") - 1;
         int cell     = (Integer)map.get("cell") -1;
         String value = (String)map.get("value") ;
        
         HSSFRow row1 = null ;//sheet.getRow(row) ;
      if( row1 == null ) row1 = sheet.createRow(row) ;
      else row1 = sheet.getRow(row) ;
      
      HSSFCell cell1 = null ;//row1.getCell((short)cell) ;
      if(cell1==null)cell1 = row1.createCell((short)cell) ;
      else cell1 = row1.getCell((short)cell) ;
      
      cell1.setCellValue(value==null?"":value) ;
        
    }
    
    FileOutputStream fOut = new FileOutputStream(file);
    workbook.write(fOut);
    fOut.flush();
    fOut.close();
    
   } catch (FileNotFoundException e) {
    e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace();
   }
    
     //for(Iterator iter1=list.iterator();iter1.hasNext();){
        //    Map row1=(Map)iter1.next();
     //}
    }
   
  public void execute(){
     writeAppointPosition(fileName,sheetName,list) ;
 }

}

XmlExport.java

package org.xiaohongli.common.export;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.jdom.Document;
import org.jdom.Element;
import org.xiaohongli.common.IExport;
import org.xiaohongli.common.JDOMXml;

public class XmlExport extends Download implements IExport{

 public int export(Map headerMap, String outFile, List value) {
  try {
   Element element = null ;
   Document document = null ;

   for(int i=0 ;i<(value==null?0:value.size());i++){
     Object obj = value.get(i) ;
     String nodeName = obj.getClass().getSimpleName().toLowerCase() ;
     Method[] method = obj.getClass().getDeclaredMethods();
     List<String> headerlist = new ArrayList<String>() ;
     for(int i1=0; i1<method.length;i1++){
      String str = method[i1].getName() ;
      if(str.indexOf("get")==0){
       String fieldname = Util.getFieldName(str) ;
       headerlist.add(fieldname) ;
      }else{
       continue ;
      }
     }
    
    element = JDOMXml.createElement(nodeName, null, false);
    document = JDOMXml.createDocument(element) ;
    
     for(int i2 = 0 ;i2 <( headerlist==null?0:headerlist.size());i2++){
      String headerValue = Util.convertNull((String)headerlist.get(i2)).toLowerCase() ;
      String headerDisplay = (String)headerMap.get(headerValue) ;
      if(headerDisplay!=null&&!"".equals(headerDisplay)){
        Element element1 = JDOMXml.createElement(headerDisplay, null, false);
       JDOMXml.addElement(element, element1) ;
      }      
     }
    
     break ;
   }
   
   
   List<String[]> valuelist = new ArrayList<String[]>() ;
   for(int i=0 ;i<(value==null?0:value.size());i++){
     Object obj = value.get(i) ;
     Method[] method = obj.getClass().getDeclaredMethods();
     for(int i1=0; i1<method.length;i1++){
     String name = method[i1].getName() ;
     if(name.indexOf("set")==0) continue ;
     if(name.indexOf("get")==0){
      String fieldname = Util.getFieldName(name) ;
      Object value1 = method[i1].invoke(obj,new Object[0]);
      String value2 = value1==null?null:value1.toString() ;
      String[] str = new String[]{fieldname.toLowerCase(),value2} ;
      valuelist.add(str) ;
     }
     }
    
    }
   
   for(int i=0 ;i<valuelist.size() ;i++){
    String[] str = (String[])valuelist.get(i) ;
    String key = str[0] ;
    String keyValue = str[1] ;
    if(Util.mapContainKey(headerMap, key)){
     Element element1 = JDOMXml.getElement(element, (String)headerMap.get(key)) ;
     Element element2 = JDOMXml.createElement("value", keyValue, false);
     JDOMXml.addElement(element1, element2) ;
    }
   }
   if(null==outFile||outFile.equals(""))
    outFile = Util.TEMP_XML_FILE ;
   JDOMXml.outputDocumentToFile(document,outFile) ;
  } catch (SecurityException e) {
   e.printStackTrace();
   return Util.ERROR ;
  } catch (IllegalArgumentException e) {
   e.printStackTrace();
   return Util.ERROR ;
  } catch (IllegalAccessException e) {
   e.printStackTrace();
   return Util.ERROR ;
  } catch (InvocationTargetException e) {
   e.printStackTrace();
   return Util.ERROR ;
  }
  return Util.SUCCESS ;
 }

 public int export(String outFile, List value) {
  try {
   Element element = null;
   Document document = null;
   for(int i=0 ;i<(value==null?0:value.size());i++){
     Object obj = value.get(i) ;
     String nodeName = obj.getClass().getSimpleName().toLowerCase() ;
     Method[] method = obj.getClass().getDeclaredMethods();
     List<String> headerlist = new ArrayList<String>() ;
     for(int i1=0; i1<method.length;i1++){
      String str = method[i1].getName() ;
      if(str.indexOf("get")==0){
       String fieldname = Util.getFieldName(str) ;
       headerlist.add(fieldname) ;
      }else{
       continue ;
      }
     }
     element = JDOMXml.createElement(nodeName, null, false);
     document = JDOMXml.createDocument(element) ;
     for(int i2 = 0 ;i2 <( headerlist==null?0:headerlist.size());i2++){
     Element element1 = JDOMXml.createElement(headerlist.get(i2), null, false);
     JDOMXml.addElement(element, element1) ;    
     }
     break ;
   }
   
   
   List<String[]> valuelist = new ArrayList<String[]>() ;
   for(int i=0 ;i<(value==null?0:value.size());i++){
     Object obj = value.get(i) ;
     Method[] method = obj.getClass().getDeclaredMethods();
     for(int i1=0; i1<method.length;i1++){
     String name = method[i1].getName() ;
     if(name.indexOf("set")==0) continue ;
     if(name.indexOf("get")==0){
      String fieldname = Util.getFieldName(name) ;
      Object value1 = method[i1].invoke(obj,new Object[0]);
      String value2 = value1==null?null:value1.toString() ;
      String[] str = new String[]{fieldname.toLowerCase(),value2} ;
      valuelist.add(str) ;
     }
     }
    
    }
   
   for(int i=0 ;i<valuelist.size() ;i++){
    String[] str = (String[])valuelist.get(i) ;
    String key = str[0] ;
    String keyValue = str[1] ;
    Element element1 = JDOMXml.getElement(element, key) ;
    Element element2 = JDOMXml.createElement("value", keyValue, false);
    JDOMXml.addElement(element1, element2) ;
   }
   if(null==outFile||outFile.equals(""))
    outFile = Util.TEMP_XML_FILE ;
   JDOMXml.outputDocumentToFile(document,outFile) ;
  } catch (SecurityException e) {
   e.printStackTrace();
   return Util.ERROR ;
  } catch (IllegalArgumentException e) {
   e.printStackTrace();
   return Util.ERROR ;
  } catch (IllegalAccessException e) {
   e.printStackTrace();
   return Util.ERROR ;
  } catch (InvocationTargetException e) {
   e.printStackTrace();
   return Util.ERROR ;
  }
  return Util.SUCCESS ;
 }


}

  PdfExport.java

package org.xiaohongli.common.export;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.xiaohongli.common.IExport;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.PdfWriter;

public class PdfExport extends Download implements IExport {

 public int export(Map headerMap, String outFile, List value) {
  if(null==outFile||outFile.equals(""))
   outFile = Util.TEMP_PDF_FILE ;
  try {
   /***************************************/
   //   得到表头数据
   List<Position> tableHeaderList = new ArrayList<Position>() ;
   for(int i=0 ;i<(value==null?0:value.size());i++){
     Object obj = value.get(i) ;
     Method[] method = obj.getClass().getDeclaredMethods();
     List<String> headerlist = new ArrayList<String>() ;
     for(int i1=0; i1<method.length;i1++){
      String str = method[i1].getName() ;
      if(str.indexOf("get")==0){
       String fieldname = Util.getFieldName(str) ;
       headerlist.add(fieldname) ;
      }else{
       continue ;
      }
     }
     int insertColumn = 0 ;
     for(int i2 = 0 ;i2 <( headerlist==null?0:headerlist.size());i2++){
      String headerValue = Util.convertNull((String)headerlist.get(i2)).toLowerCase() ;
      String headerDisplay = (String)headerMap.get(headerValue) ;
      if(headerDisplay!=null&&!"".equals(headerDisplay)){
       Position position = new Position(headerValue,headerDisplay,insertColumn);
       tableHeaderList.add(position);
       insertColumn ++ ;
      }      
       
     }
    
     break ;
   }
   //得到table数据
   List<List<Position>> valueTableList = new ArrayList<List<Position>>();
   for(int i=0 ;i<(value==null?0:value.size());i++){
     Object obj = value.get(i) ;
     Method[] method = obj.getClass().getDeclaredMethods();
     List<Position> valuelist = new ArrayList<Position>() ;
     //得到一行纪录
     for(int i1=0; i1<method.length;i1++){
     String name = method[i1].getName() ;
     if(name.indexOf("set")==0) continue ;
     if(name.indexOf("get")==0){
      String fieldname = Util.getFieldName(name) ;
      if(Util.mapContainKey(headerMap, fieldname)){
       Object value1 = method[i1].invoke(obj,new Object[0]);
       String value2 = value1==null?null:value1.toString() ;
       Position position = new Position(fieldname,value2,-1);
       valuelist.add(position) ;
      }
     }
     }
     valueTableList.add(valuelist);
    }
   /***************************************/
   
   FileOutputStream fos;
   try {
    fos = new FileOutputStream(outFile);
   } catch (FileNotFoundException e) {
    try {
     File file = new File(outFile) ;
     file.createNewFile() ;
     fos = new FileOutputStream(file);
    } catch (FileNotFoundException e1) {
     fos = null ;
     e1.printStackTrace();
     return Util.ERROR ;
    } catch (IOException e1) {
     fos = null ;
     e1.printStackTrace();
     return Util.ERROR ;
    }
   }
   
   Document document = new Document();//创建pdf文档对象实例
   PdfWriter.getInstance(document, fos);//建立书写器关联pdf文档对象实例
   document.open();//打开文档
   Table table = new Table(tableHeaderList.size());

   for(int i=0 ;i<tableHeaderList.size() ;i++){
     Position position = (Position)tableHeaderList.get(i) ;
     table.addCell(position.getDisplayValue()) ;
   }
   for(int i=0 ;i<valueTableList.size() ;i++){
    List valueList = valueTableList.get(i) ;
    for(int i1=0 ;i1<valueList.size() ;i1++){
     Position p = (Position)valueList.get(i1) ;
     table.addCell(p.getDisplayValue()) ;
    }
   }
   document.add(table);//添加内容
   document.close();//关闭文档
  } catch (DocumentException e) {
   e.printStackTrace();
   return Util.ERROR ;
  } catch (IllegalArgumentException e) {
   e.printStackTrace();
   return Util.ERROR ;
  } catch (IllegalAccessException e) {
   e.printStackTrace();
   return Util.ERROR ;
  } catch (InvocationTargetException e) {
   e.printStackTrace();
   return Util.ERROR ;
  }
  return Util.SUCCESS ;
 }

 public int export(String outFile, List value) {
  if(null==outFile||outFile.equals(""))
   outFile = Util.TEMP_PDF_FILE ;
  try {
   /***************************************/
   //   得到表头数据
   List<String> tableHeaderList = new ArrayList<String>() ;
   for(int i=0 ;i<(value==null?0:value.size());i++){
     Object obj = value.get(i) ;
     Method[] method = obj.getClass().getDeclaredMethods();
     List<String> headerlist = new ArrayList<String>() ;
     for(int i1=0; i1<method.length;i1++){
      String str = method[i1].getName() ;
      if(str.indexOf("get")==0){
       String fieldname = Util.getFieldName(str) ;
       headerlist.add(fieldname) ;
      }else{
       continue ;
      }
     }
     for(int i2 = 0 ;i2 <( headerlist==null?0:headerlist.size());i2++){
      String headerValue = Util.convertNull((String)headerlist.get(i2)).toLowerCase() ;
      tableHeaderList.add(headerValue);

     }
    
     break ;
   }
   //得到table数据
   List<List<Position>> valueTableList = new ArrayList<List<Position>>();
   for(int i=0 ;i<(value==null?0:value.size());i++){
     Object obj = value.get(i) ;
     Method[] method = obj.getClass().getDeclaredMethods();
     List<Position> valuelist = new ArrayList<Position>() ;
     //得到一行纪录
     for(int i1=0; i1<method.length;i1++){
     String name = method[i1].getName() ;
     if(name.indexOf("set")==0) continue ;
     if(name.indexOf("get")==0){
      String fieldname = Util.getFieldName(name) ;
      Object value1 = method[i1].invoke(obj,new Object[0]);
      String value2 = value1==null?null:value1.toString() ;
      Position position = new Position(fieldname,value2,-1);
      valuelist.add(position) ;
     }
     }
     valueTableList.add(valuelist);
    }
   /***************************************/
   
   FileOutputStream fos;
   try {
    fos = new FileOutputStream(outFile);
   } catch (FileNotFoundException e) {
    try {
     File file = new File(outFile) ;
     file.createNewFile() ;
     fos = new FileOutputStream(file);
    } catch (FileNotFoundException e1) {
     fos = null ;
     e1.printStackTrace();
     return Util.ERROR ;
    } catch (IOException e1) {
     fos = null ;
     e1.printStackTrace();
     return Util.ERROR ;
    }
   }
   
   Document document = new Document();//创建pdf文档对象实例
   PdfWriter.getInstance(document, fos);//建立书写器关联pdf文档对象实例
   document.open();//打开文档
   Table table = new Table(tableHeaderList.size());
   for(int i=0 ;i<tableHeaderList.size() ;i++){
    String position = (String)tableHeaderList.get(i) ;
     table.addCell(position) ;
   }
   for(int i=0 ;i<valueTableList.size() ;i++){
    List valueList = valueTableList.get(i) ;
    for(int i1=0 ;i1<valueList.size() ;i1++){
     Position p = (Position)valueList.get(i1) ;
     table.addCell(p.getDisplayValue()) ;
    }
   }
   document.add(table);//添加内容
   document.close();//关闭文档
  } catch (DocumentException e) {
   e.printStackTrace();
   return Util.ERROR ;
  } catch (IllegalArgumentException e) {
   e.printStackTrace();
   return Util.ERROR ;
  } catch (IllegalAccessException e) {
   e.printStackTrace();
   return Util.ERROR ;
  } catch (InvocationTargetException e) {
   e.printStackTrace();
   return Util.ERROR ;
  }
  return Util.SUCCESS ;
 }
 
 static class Position{
  public String columnValue ;
  public String displayValue ;
  public int  position ;
  public Position(String columnValue,String displayValue,int position){
   this.columnValue = columnValue ;
   this.displayValue = displayValue;
   this.position = position ;
  }
  public int getPosition() {
   return position;
  }
  public void setPosition(int position) {
   this.position = position;
  }
  public String getColumnValue() {
   return columnValue;
  }
  public void setColumnValue(String columnValue) {
   this.columnValue = columnValue;
  }
  public String getDisplayValue() {
   return displayValue;
  }
  public void setDisplayValue(String displayValue) {
   this.displayValue = displayValue;
  }
  
 }

}

ExportFactory.java

package org.xiaohongli.common;

import org.xiaohongli.common.export.ExcelExport;
import org.xiaohongli.common.export.PdfExport;
import org.xiaohongli.common.export.XmlExport;

public class ExportFactory {
   
 /**
  * 得到xml导出对象的实例
  * @return
  */
 public IExport getXmlExportInstance(){
      return new XmlExport() ;
     }
    
 /**
  * 得到excel文件导出对像的实例
  * @return
  */
     public IExport getExcelExportInstance(){
      return new ExcelExport() ;
     }
    
     /**
   * 得到excel文件导出对像的实例
   * @return
   */
      public IExport getPdfExportInstance(){
       return new PdfExport() ;
      }
     
}

 

 

package org.xiaohongli.common.export;

import java.util.Map;

public class Util {
  public static int ERROR = -1 ;
  public static int SUCCESS = 1 ;
  public static String TEMP_PDF_FILE = getRootPath()+"/temp_pdf.pdf" ;
  public static String TEMP_EXCEL_FILE = getRootPath()+"/temp_excel.xls" ;
  public static String TEMP_XML_FILE = getRootPath()+"/temp_pdf.xml" ;
  public  static String getFieldName(String fieldname){
   if(fieldname==null||"".equals(fieldname)) return null ;
   else{
    return fieldname.substring(3).toLowerCase();
   }
 }

  public static String convertNull(String str){
   if(str==null)return "" ;
   return str ;
  }
 
  public static boolean mapContainKey(Map map,String key){
   return map.containsKey(key) ;
  }
  public static void log(String name,int i){
   if(i==ERROR){
    System.out.println(name+",fail!");
   }else
   if(i==SUCCESS){
    System.out.println(name+",succeed!");
   }else{
    System.out.println("Unknown error!");
   }
  }
 
  private static String getRootPath(){
  return System.getProperty("user.dir");
  }
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值