EXCEL转JAVA BEAN

EXCEL转JAVA BEAN

pom.xml回入以下包:
         < dependency >
             < groupId >net.sf.jxls </ groupId >
             < artifactId >jxls-core </ artifactId >
         </ dependency >
         < dependency >
             < groupId >net.sf.jxls </ groupId >
             < artifactId >jxls-reader </ artifactId >
         </ dependency >
         < dependency >
             < groupId >net.sf.jxls </ groupId >
             < artifactId >jxls-examples </ artifactId >
         </ dependency >


转换的配置文件:
< workbook >
   < worksheet  name ="Sheet1" >
     < section  startRow ="0"  endRow ="0"   />
     < loop  startRow ="1"  endRow ="1"  items ="result"  var ="app"  varType ="n.app.valueobject.App" >
       < section  startRow ="1"  endRow ="1" >
         < mapping  row ="1"  col ="0" >app.title </ mapping >
         < mapping  row ="1"  col ="1" >app.categoryId </ mapping >
         < mapping  row ="1"  col ="2" >app.updateContent </ mapping >
         < mapping  row ="1"  col ="3" >app.rank </ mapping >
         < mapping  row ="1"  col ="4" >app.installedQty </ mapping >
         < mapping  row ="1"  col ="5" >app.installedType </ mapping >
         < mapping  row ="1"  col ="6" >app.discuss </ mapping >
         < mapping  row ="1"  col ="7" >app.summary </ mapping >
         < mapping  row ="1"  col ="8" >app.deviceTypes </ mapping >
         < mapping  row ="1"  col ="9" >app.description </ mapping >
         < mapping  row ="1"  col ="10" >app.newFeatures </ mapping >
         < mapping  row ="1"  col ="11" >app.shortRecommend </ mapping >
         < mapping  row ="1"  col ="12" >app.appUrl </ mapping >
       </ section >
       < loopbreakcondition >
         < rowcheck  offset ="0" >
           < cellcheck  offset ="0"   />
         </ rowcheck >
       </ loopbreakcondition >
     </ loop >
   </ worksheet >
</ workbook >



JAVA代码:
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.jxls.reader.ReaderBuilder;
import net.sf.jxls.reader.XLSReader;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.SAXException;

import application.app.valueobject.App;

public  class ExcelUtil<T> {
    
     private  static Logger logger = LoggerFactory.getLogger(ExcelUtil. class);
    
     public List<T> parseExcelFileToBeans(InputStream xlsFileInputStream, InputStream jxlsConfigInputStream)  throws IOException, SAXException, InvalidFormatException  {
        
        
        List<T> result =  new ArrayList<T>();
        Map<String, Object> beans =  new HashMap<String, Object>();
        beans.put("result", result);
        InputStream inputStream =  null;
         try {
            XLSReader xlsReader = ReaderBuilder.buildFromXML(jxlsConfigInputStream);
            inputStream =  new BufferedInputStream(xlsFileInputStream);
            xlsReader.read(inputStream, beans);
        }  catch (IOException e) {
            logger.error(e.getMessage(), e);
             throw e;
        }  catch (SAXException e) {
            logger.error(e.getMessage(), e);
             throw e;
        }  catch (InvalidFormatException e) {
            logger.error(e.getMessage(), e);
             throw e;
        }  finally {
             if (inputStream !=  null) {
                 try {
                    inputStream.close();
                }  catch (Exception e) {
                }
            }
        }
         return result;
    }
    
     public  static  void main(String[] args)  throws Exception {
        
        String path = "D:/DATA/TESTING-FILE/EXCEL";
        
        path = System.getProperty("user.home");
        
        ExcelUtil<App> util =  new ExcelUtil<App>();
        String excelFilePath = path + File.separator + "appData.xls";
        
        InputStream configInputStream = 
                ExcelUtil. class.getResourceAsStream("/excel/template/config/app_config.xml");
        InputStream xlsFileInputStream =  new FileInputStream(excelFilePath);
        
        List<App> appList = util.parseExcelFileToBeans(xlsFileInputStream, configInputStream);

         for (App app : appList) {
            System.out.println(app.toString());
        }
        
         /* String [] args2 = {""};
        GroupingSample.main(args2);
*/

    }

}


http://www.yihaomen.com/article/java/530.htm

你可能感兴趣的:(EXCEL转JAVA BEAN)