|
本帖最后由 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中,如下- Workbook workBook = (Workbook)ModelUtils.getRequestContext().getActionContext().getParameter("excel");
- Sheet sheet=workBook.getSheetAt(1); //获得sheet2中的数据
- Iterator<Row> it=sheet.iterator();
- Map map=new HashMap();
- Row row=null;
- Cell cell=null;
- while(it.hasNext()){
- row=it.next();
- int i=1;
- Iterator<Cell> ce=row.cellIterator();
- while(ce.hasNext()){
- cell=ce.next();
- map.put(i++, cell);
- }
- String ksql="insert into EX_Table1 ex (ex.fName,ex.fSex,ex.fAge) values("+map.get(1)+","+map.get(2)+","+map.get(3)+")"; //例子里数据只有三列数值,所以写死了
- KSQL.executeUpdate(ksql, null,"/excel/excel/data", null);
- }
复制代码 第二种方法:通过import.mapping.xml文件进行对数据的保存
获得mapping文件中的节点值,修改/BIZ/SA/excel/logic/code/src/com/justep/excel/ImportConfig.java,增加如下方法。修改这个文件后,需要进行模型编译,再重启tomcat
加红色的部分是需要添加的代码
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);
- }
复制代码 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代码如下- <?xml version="1.0" encoding="UTF-8"?>
- <mapping>
- <default-config>
- <sheet all="false" start="1" end="2"/>
- <row all="false" start="1" end="6"/>
- </default-config>
- <concept name="Demo_table1">
- <!-- <primary-key>-->
- <!-- <key-value cell-number="1"/>-->
- <!-- </primary-key>-->
- <relation name="FSTRING" cell-number="1"/>
- <relation name="FINTEGER" cell-number="2" check="true"/>
- <relation name="FFLOAT" cell-number="3"/>
- <relation name="FDECIMAL" cell-number="4"/>
- <relation name="FDATE" value-type="date" cell-number="5"/>
- <relation name="FDATETIME" value-type="datetime" cell-number="6"/>
- </concept>
- <options name="EX_Table1"> <!--这是第二张表的数据配置-->
- <relation name="fName" cell-number="1"/>
- <relation name="fSex" cell-number="2"/>
- <relation name="fAge" cell-number="3"/>
- </options>
- </mapping>
复制代码
|
|