本例实现的是excelImport导入时根据某列的值判断数据库中是否存在,如果存在就修改数据库中的记录,如果不存在就新增记录
excelImport导入的excel可以在导入excel前action(importExcelBeforeAction)的执行后事件中获取
然后遍历excel获取校验列的值进行对比处理
做法如下
1、在构建路径中添加/SA/excel/logic/code下的jar包
2、在process中引用/SA/excel/logic/action和/SA/excel/logic/code目录
3、在动作设置中添加importExcelBeforeAction的执行后事件,代码如下
4、在w文件的excel导入组件的action属性中选择/SA/excel/logic/action/importExcel
importExcelBeforeAction执行后事件代码如下:
import java.math.BigDecimal; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import com.justep.system.util.*; import com.justep.system.process.*; import com.justep.system.context.*; import com.justep.system.opm.*; import com.justep.system.data.*; import com.justep.system.action.*; import com.justep.model.*; public class ExcelCheckProcess { public static void excelCheckProcessAfterImportExcelBeforeAction() { Workbook workBook = (Workbook) ModelUtils.getRequestContext().getActionContext().getParameter("excel"); Sheet sheet = workBook.getSheetAt(0); // 获得sheet1表 for (int i = 0; i <= sheet.getLastRowNum(); i++) { // 对行进行循环 Row row = sheet.getRow(i); Cell cell = row.getCell(1);//获取第二列 cell.setCellType(Cell.CELL_TYPE_STRING); // 根据每行第二列的值判断是否数据库中是否有相应的记录 String code = cell.getStringCellValue(); String ksql = "select ec.fCode from AP_EXCELDRXY ec where ec.fCode = '"+code+"'"; Table table = KSQL.select(ksql, null, "/appdemo/test/data", null); //如果table的size大于0说明数据库中有,就获取excel中其他列的值对数据库执行update,并删除这个这列 // 注意: UI2下的import.mapping.xml里配置的第二列对应的关系要配置check="true" //如:<relation name="fCode" value-type="String" cell-number="2" check="true"></relation> // excel中行数据是否有效判断,当check="true"时cell-number对应列值为空,认为此行数据无效将被忽略不会新增 if(table.size()>0){ String fName = row.getCell(0).getStringCellValue(); String fDescription = row.getCell(2).getStringCellValue(); java.sql.Date fRQ = new java.sql.Date(row.getCell(3).getDateCellValue().getTime()); BigDecimal fSZ = new BigDecimal(row.getCell(4).getNumericCellValue()+""); BigDecimal fJE = new BigDecimal(row.getCell(5).getNumericCellValue()+""); String updateKsql = "update AP_EXCELDRXY ec set ec.fName = '"+fName+"',ec.fDescription= '"+fDescription+"',ec.fRQ= stringToDate('"+fRQ+"'),ec.fSZ= "+fSZ+",ec.fJE= "+fJE+" where ec.fCode = '"+code+"'"; KSQL.executeUpdate(updateKsql, null, "/appdemo/test/data", null); row.removeCell(cell); } } } }
import.mapping.xml配置文件的内容如下:
<?xml version="1.0" encoding="UTF-8"?> <mapping> <default-config> <sheet all="false" start="1" end="1" /> <row all="false" start="2" end="99999" /> </default-config> <concept name="AP_EXCELDRXY"> <relation name="fName" value-type="String" cell-number="1"></relation> <relation name="fCode" value-type="String" cell-number="2" check="true"></relation> <relation name="fDescription" value-type="String" cell-number="3"></relation> <relation name="fRQ" value-type="Date" cell-number="4"></relation> <relation name="fSZ" value-type="Decimal" cell-number="5"></relation> <relation name="fJE" value-type="Decimal" cell-number="6"></relation> </concept> </mapping>
评一波