paulwong

          EXCEL轉(zhuǎn)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>


          轉(zhuǎn)換的配置文件:
          <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

          主站蜘蛛池模板: 永川市| 禹城市| 营口市| 南江县| 南平市| 泽库县| 惠安县| 台南市| 凤台县| 南溪县| 六枝特区| 湾仔区| 通化县| 密山市| 鄂托克前旗| 白玉县| 衡阳县| 沙坪坝区| 江北区| 焦作市| 定南县| 江西省| 云龙县| 宁化县| 洞头县| 沾益县| 临汾市| 巴马| 玉环县| 蒙自县| 丹东市| 武宁县| 乐都县| 太原市| 梅州市| 兴仁县| 旬阳县| 嘉义市| 大厂| 博湖县| 东乌珠穆沁旗|