paulwong

          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

          posted on 2014-10-29 17:25 paulwong 閱讀(1099) 評論(0)  編輯  收藏 所屬分類: J2SE

          主站蜘蛛池模板: 巫山县| 上栗县| 张北县| 丹棱县| 无极县| 茶陵县| 平凉市| 西平县| 乳源| 邓州市| 晋州市| 图木舒克市| 山东省| 蓬安县| 邻水| 新安县| 湘乡市| 泗水县| 临邑县| 兴业县| 孝感市| 宣城市| 安平县| 阜城县| 崇左市| 富裕县| 宁蒗| 天全县| 长治市| 梧州市| 宁晋县| 黄陵县| 溆浦县| 大安市| 田林县| 腾冲县| 岳普湖县| 微山县| 金门县| 安福县| 临桂县|