起步软件技术论坛
搜索
 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9003|回复: 4

导入包含多张不同sheet表的Execl的两种方法

  [复制链接]

11

主题

1134

帖子

1261

积分

金牌会员

Rank: 6Rank: 6

积分
1261
QQ
发表于 2013-7-29 10:59:16 | 显示全部楼层 |阅读模式
本帖最后由 song_ning_ning 于 2013-7-30 16:15 编辑

下面例子是导入多张包含不同sheet表的Excel  

方法如下:
1、在构建路径中添加/SA/excel/logic/code下的jar包
2    因为不同的sheet数据要放在不同的数据库表中 所以需要再新建一个数据库表
3、在process中引用/SA/excel/logic/action和/SA/excel/logic/code目录
4、在动作设置中添加importExcelBeforeAction的执行后事件,代码如下
5、在w文件的excel导入组件的action属性中选择/SA/excel/logic/action/importExcel

importExcelBeforeAction的执行后事件,代码如下
     
第一种方法:sheet1表中的数据不管 由系统自动保存
                     sheet2表中的数据通过自己的操作,保存在自己新建的数据库EX_Table1中,如下
  1. Workbook workBook = (Workbook)ModelUtils.getRequestContext().getActionContext().getParameter("excel");
  2. Sheet sheet=workBook.getSheetAt(1); //获得sheet2中的数据
  3. Iterator<Row> it=sheet.iterator();
  4. Map map=new HashMap();
  5. Row row=null;
  6. Cell cell=null;
  7. while(it.hasNext()){
  8.      row=it.next();
  9.      int i=1;
  10.      Iterator<Cell> ce=row.cellIterator();
  11.      while(ce.hasNext()){
  12.            cell=ce.next();
  13.            map.put(i++, cell);
  14.      }
  15. String ksql="insert into EX_Table1 ex (ex.fName,ex.fSex,ex.fAge) values("+map.get(1)+","+map.get(2)+","+map.get(3)+")";  //例子里数据只有三列数值,所以写死了
  16. KSQL.executeUpdate(ksql, null,"/excel/excel/data", null);
  17. }
复制代码
第二种方法:通过import.mapping.xml文件进行对数据的保存
                     获得mapping文件中的节点值,修改/BIZ/SA/excel/logic/code/src/com/justep/excel/ImportConfig.java,增加如下方法。修改这个文件后,需要进行模型编译,再重启tomcat
                     加红色的部分是需要添加的代码

                    importExcelBeforeAction的执行后事件代码如下:
  1. ImportConfig importConfig = (ImportConfig)ModelUtils.getRequestContext().getActionContext().getParameter("config");
  2. List<ImportRelationConfig> list=importConfig.getExtendConfig("option");//这的参数是import.mapping.xml中要自己要导入表的的节点名称
  3. Sheet sheet=workBook.getSheetAt(1);
  4. Iterator<Row> it=sheet.iterator();
  5. Map map=new HashMap();
  6. List listName=new ArrayList();
  7. List listValue=new ArrayList();
  8. Row row=null;
  9. Cell cell=null;
  10. for(ImportRelationConfig irc:list){
  11.      listName.add(irc.getMetaCellIndex(),irc.getMetaName());
  12. }
  13. while(it.hasNext()){
  14.      row=it.next();
  15. Iterator<Cell> ce=row.cellIterator();
  16.      int num=0;
  17.      while(ce.hasNext()){
  18.             cell=ce.next();
  19.             listValue.add(num++,cell);
  20.      }
  21. StringBuffer sb1=new StringBuffer();
  22. StringBuffer sb2=new StringBuffer();
  23. for(int i=0;i<listName.size();i++){
  24.      sb1.append(","+"ex."+listName.get(i));
  25. }
  26. for(int j=0;j<num;j++){
  27.      sb2.append(","+listValue.get(j));
  28. }
  29. String ksql="insert into "+importConfig.getName()+" ex "+"("+sb1.deleteCharAt(0)+") values("+sb2.deleteCharAt(0)+")";
  30. KSQL.executeUpdate(ksql, null,"/excel/excel/data", null);
  31. }
复制代码
package com.justep.excel;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.poi.ss.usermodel.Workbook;
import org.dom4j.Element;
import com.justep.excel.ImportConceptConfig;
import com.justep.excel.ImportRange;
import com.justep.excel.ImportRelationConfig;
public class ImportConfig {
private List<ImportRelationConfig> relationConfigs = new ArrayList<ImportRelationConfig>();
private String name;
private int cellSize = 0;

private Element mappingE = null;
private Element configE = null;
private ImportConceptConfig conceptConfig;
public ImportConfig(Element mappingElement, Element configElement) {
           mappingE = mappingElement;
           configE = configElement;
           conceptConfig = new ImportConceptConfig(mappingElement, configElement);
  
}
public String getToKind() {
           return configE.attributeValue("to");
}
public ImportRange getRowRange(int sheet, Workbook workBook) {
         ImportRange result = new ImportRange();
         int start = 1;
         int end = workBook.getSheetAt(sheet).getPhysicalNumberOfRows();
        Element E = useDefault() ? mappingE.element("default-config") : configE;
        if (null != E) {
              Element rowE = E.element("row");
        if ("false".equalsIgnoreCase(rowE.attributeValue("all"))) {
            try {
                    int i = Integer.parseInt(rowE.attributeValue("start"));
                    start = i > start ? i : start;
                    i = Integer.parseInt(rowE.attributeValue("end"));
                    end = i < end ? i : end;
             } catch (Exception e) {
             }
       }
  }
  result.setStart(start);
  result.setEnd(end);
  return result;
}
public ImportRange getSheetRange(int sheetNum) {
      ImportRange result = new ImportRange();
      int start = 1;
      int end = sheetNum;
      Element E = useDefault() ? mappingE.element("default-config") : configE;
      if (null != E) {
         Element sheetE = E.element("sheet");
      if ("false".equalsIgnoreCase(sheetE.attributeValue("all"))) {
          try {
          int i = Integer.parseInt(sheetE.attributeValue("start"));
          start = i > start ? i : start;
          i = Integer.parseInt(sheetE.attributeValue("end"));
          end = i < end ? i : end;
          } catch (Exception e) {
          }
      }
  }
  result.setStart(start);
  result.setEnd(end);
  return result;
}
private boolean useDefault() {
         return "true".equalsIgnoreCase(configE.attributeValue("use-default"));
}
public ImportConceptConfig getConceptConfig() {
        return conceptConfig;
}
public  List<ImportRelationConfig> getExtendConfig(String concept){
  Element mappConceptE = mappingE.element(concept);
  if (null == mappConceptE)
        throw new RuntimeException("映射定义中缺少概念定义信息");
  String s1 = mappConceptE.attributeValue("name");
  name = s1;
  List<?> relationEs = mappConceptE.elements("relation");
  for (Object relationE : relationEs) {
       ImportRelationConfig o = new ImportRelationConfig((Element) relationE, mappConceptE);
       if (cellSize < o.getMetaCellIndex())
             cellSize = o.getMetaCellIndex();
            relationConfigs.add(o);
  }
       return relationConfigs;
}

public String getName() {
      return name;
}

public void setName(String name) {
       this.name = name;
}


}




import.mapping.xml代码如下
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <mapping>
  3. <default-config>
  4.     <sheet all="false" start="1" end="2"/>
  5.     <row all="false" start="1" end="6"/>
  6. </default-config>
  7. <concept name="Demo_table1">
  8.      <!-- <primary-key>-->
  9.          <!-- <key-value cell-number="1"/>-->
  10.      <!-- </primary-key>-->
  11.      <relation name="FSTRING" cell-number="1"/>
  12.      <relation name="FINTEGER" cell-number="2" check="true"/>
  13.      <relation name="FFLOAT" cell-number="3"/>
  14.      <relation name="FDECIMAL" cell-number="4"/>
  15.      <relation name="FDATE" value-type="date" cell-number="5"/>
  16.      <relation name="FDATETIME" value-type="datetime" cell-number="6"/>
  17. </concept>
  18. <options name="EX_Table1">   <!--这是第二张表的数据配置-->
  19.      <relation name="fName" cell-number="1"/>
  20.      <relation name="fSex" cell-number="2"/>
  21.      <relation name="fAge" cell-number="3"/>
  22. </options>

  23. </mapping>
复制代码

远程的联系方法QQ2025089647。添加好友时,需要填写论坛账号
发远程时同时也发一下帖子的地址,方便了解要

1

主题

25

帖子

250

积分

中级会员

Rank: 3Rank: 3

积分
250
发表于 2013-7-29 11:13:47 | 显示全部楼层
学习

73

主题

247

帖子

313

积分

中级会员

Rank: 3Rank: 3

积分
313
QQ
发表于 2014-9-4 14:40:12 | 显示全部楼层
学习
点评回复

使用道具 举报

19

主题

78

帖子

204

积分

中级会员

Rank: 3Rank: 3

积分
204
QQ
发表于 2016-12-12 18:11:29 | 显示全部楼层
以下是我照著教學做,有修改到的所有代碼,您的意思是要修改哪裡的java文件?




1、在构建路径中添加/SA/excel/logic/code下的jar包
2、在process中引用/SA/excel/logic/action和/SA/excel/logic/code目录
3、在动作设置中添加importExcelBeforeAction的执行后事件,代码如下
5、在w文件的excel导入组件的action属性中选择/SA/excel/logic/action/importExcel
6、修改/BIZ/SA/excel/logic/code/src/com/justep/excel/ImportConfig.java,增加如下方法。修改这个文件后,需要进行模型编译,再重启tomcat
      红色的部分是需要添加的代码

package com.justep.excel;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.poi.ss.usermodel.Workbook;
import org.dom4j.Element;
import com.justep.excel.ImportConceptConfig;
import com.justep.excel.ImportRange;
import com.justep.excel.ImportRelationConfig;
public class ImportConfig {
private List<ImportRelationConfig> relationConfigs = new ArrayList<ImportRelationConfig>();
private String name;
private int cellSize = 0;

private Element mappingE = null;
private Element configE = null;
private ImportConceptConfig conceptConfig;
public ImportConfig(Element mappingElement, Element configElement) {
           mappingE = mappingElement;
           configE = configElement;
           conceptConfig = new ImportConceptConfig(mappingElement, configElement);
  
}
public String getToKind() {
           return configE.attributeValue("to");
}
public ImportRange getRowRange(int sheet, Workbook workBook) {
         ImportRange result = new ImportRange();
         int start = 1;
         int end = workBook.getSheetAt(sheet).getPhysicalNumberOfRows();
        Element E = useDefault() ? mappingE.element("default-config") : configE;
        if (null != E) {
              Element rowE = E.element("row");
        if ("false".equalsIgnoreCase(rowE.attributeValue("all"))) {
            try {
                    int i = Integer.parseInt(rowE.attributeValue("start"));
                    start = i > start ? i : start;
                    i = Integer.parseInt(rowE.attributeValue("end"));
                    end = i < end ? i : end;
             } catch (Exception e) {
             }
       }
  }
  result.setStart(start);
  result.setEnd(end);
  return result;
}
public ImportRange getSheetRange(int sheetNum) {
      ImportRange result = new ImportRange();
      int start = 1;
      int end = sheetNum;
      Element E = useDefault() ? mappingE.element("default-config") : configE;
      if (null != E) {
         Element sheetE = E.element("sheet");
      if ("false".equalsIgnoreCase(sheetE.attributeValue("all"))) {
          try {
          int i = Integer.parseInt(sheetE.attributeValue("start"));
          start = i > start ? i : start;
          i = Integer.parseInt(sheetE.attributeValue("end"));
          end = i < end ? i : end;
          } catch (Exception e) {
          }
      }
  }
  result.setStart(start);
  result.setEnd(end);
  return result;
}
private boolean useDefault() {
         return "true".equalsIgnoreCase(configE.attributeValue("use-default"));
}
public ImportConceptConfig getConceptConfig() {
        return conceptConfig;
}
public  List<ImportRelationConfig> getExtendConfig(String concept){
  Element mappConceptE = mappingE.element(concept);
  if (null == mappConceptE)
        throw new RuntimeException("映射定义中缺少概念定义信息");
  String s1 = mappConceptE.attributeValue("name");
  name = s1;
  List<?> relationEs = mappConceptE.elements("relation");
  for (Object relationE : relationEs) {
       ImportRelationConfig o = new ImportRelationConfig((Element) relationE, mappConceptE);
       if (cellSize < o.getMetaCellIndex())
             cellSize = o.getMetaCellIndex();
            relationConfigs.add(o);
  }
       return relationConfigs;
}

public String getName() {
      return name;
}

public void setName(String name) {
       this.name = name;
}


}


importExcelBeforeAction的执行后事件代码如下:
  • ImportConfig importConfig = (ImportConfig)ModelUtils.getRequestContext().getActionContext().getParameter("config");
  • List<ImportRelationConfig> list=importConfig.getExtendConfig("option");//这的参数是import.mapping.xml中要自己要导入表的的节点名称
  • Sheet sheet=workBook.getSheetAt(1);
  • Iterator<Row> it=sheet.iterator();
  • Map map=new HashMap();
  • List listName=new ArrayList();
  • List listValue=new ArrayList();
  • Row row=null;
  • Cell cell=null;
  • for(ImportRelationConfig irc:list){
  •      listName.add(irc.getMetaCellIndex(),irc.getMetaName());
  • }
  • while(it.hasNext()){
  •      row=it.next();
  • Iterator<Cell> ce=row.cellIterator();
  •      int num=0;
  •      while(ce.hasNext()){
  •             cell=ce.next();
  •             listValue.add(num++,cell);
  •      }
  • StringBuffer sb1=new StringBuffer();
  • StringBuffer sb2=new StringBuffer();
  • for(int i=0;i<listName.size();i++){
  •      sb1.append(","+"ex."+listName.get(i));
  • }
  • for(int j=0;j<num;j++){
  •      sb2.append(","+listValue.get(j));
  • }
  • String ksql="insert into "+importConfig.getName()+" ex "+"("+sb1.deleteCharAt(0)+") values("+sb2.deleteCharAt(0)+")";
  • KSQL.executeUpdate(ksql, null,"/excel/excel/data", null);
  • }




高级模式
B Color Image Link Quote Code Smilies

本版积分规则

小黑屋|手机版|X3技术论坛|Justep Inc.    

GMT+8, 2024-4-20 02:42 , Processed in 0.076556 second(s), 23 queries .

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表