|
楼主 |
发表于 2017-4-14 11:59:21
|
显示全部楼层
正确的。不过我现在用eclipse写了一个程序实现了。
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.message.SAMessages;
import com.justep.model.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.NumberToTextConverter;
import com.justep.excel.ExcelException;
import com.justep.model.ModelUtils;
public class ZxjcglProcess {
public static void mainActivityBeforeImportExcelBeforeAction() {
System.out.println("---------------导入前-----------------");
String currentPerson = ContextHelper.getPerson().getName();
Workbook workBook = (Workbook) ModelUtils.getRequestContext().getActionContext().getParameter("excel");
Sheet sheet = workBook.getSheetAt(0); // 获得sheet1表
String ksql = "";
String Bh = "";
for (int i = 1; i <= sheet.getLastRowNum(); i++) { // 对行进行循环
Row row = sheet.getRow(i);
Cell cFid = row.getCell(2);// 实物编码
Cell cPid = row.getCell(1);// 主表ID
String as = getCellValue(cFid).trim();
if (cFid == null || !isNotNullOrEmpty(getCellValue(cFid).trim())) {// 增加一条数据
System.out.println("导入实物表增加数据");
String ss = cPid.getStringCellValue();
ksql = "SELECT t,t.fBSM FROM SW_SWS_ZB t WHERE t.fJDMC='" + ss + "' and t.fJB=2";
Table tab = KSQL.select(ksql, null, "/swtz/swtree/data", null);
Iterator<com.justep.system.data.Row> it = tab.iterator();
if (it.hasNext()) {
com.justep.system.data.Row r = (com.justep.system.data.Row) it.next();
ss = r.getString(0);
Bh = r.getString(1);
String xh = BizUtils.createNextSequenceString("", "000000");
ksql = "INSERT INTO SW_SWS_CB t "
+ "(t,t.version,t.fZBID,t.fSWBM,t.fMC,t.fDJH,t.fXH,t.fPP,t.fXLH,t.fDW,t.fGZRQ,t.fGZJG,t.fKSSYRQ,t.fCFDD,t.fBGR,t.fSYBM,t.fSWGLBM,t.fZT,t.fBZ,t.fCJR,t.fCJSJ,t.fYHFL,t.fBSM)"
+ " VALUES(guid(),0,'"
+ ss// 实物分类
+ "','" + Bh + xh// 实物编码
+ "','" + (row.getCell(3) == null ? "" : getCellValue(row.getCell(3)))// 名称
+ "','" + (row.getCell(4) == null ? "" : getCellValue(row.getCell(4)))// 单据号
+ "','" + (row.getCell(5) == null ? "" : getCellValue(row.getCell(5)))// 型号
+ "','" + (row.getCell(6) == null ? "" : getCellValue(row.getCell(6)))// 品牌
+ "','" + (row.getCell(7) == null ? "" : getCellValue(row.getCell(7)))// 序列号
+ "','" + (row.getCell(8) == null ? "" : getCellValue(row.getCell(8)))// 单位
+ "'," + (row.getCell(9) == null ? "null" : "'" + getCellValue(row.getCell(9)) + "'")// 购置日期
+ "," + (row.getCell(10) == null ? "0.00" : getCellValue(row.getCell(10)))// 购置价格
+ "," + (row.getCell(11) == null ? "null" : "'" + getCellValue(row.getCell(11)) + "'")// 开始使用日期
+ ",'" + (row.getCell(12) == null ? "" : getCellValue(row.getCell(12)))// 存放地点
+ "','" + (row.getCell(13) == null ? "" : getCellValue(row.getCell(13)))// 保管人
+ "','" + (row.getCell(14) == null ? "" : getCellValue(row.getCell(14)))// 使用部门
+ "','" + (row.getCell(15) == null ? "" : getCellValue(row.getCell(15)))// 实物管理部门
+ "','" + (row.getCell(16) == null ? "" : getCellValue(row.getCell(16)))// 状态
+ "','" + (row.getCell(17) == null ? "" : getCellValue(row.getCell(17)))// 备注
+ "','" + currentPerson// 创建人
+ "','" + getNowDateTime() + "','','" + xh + "')";
int record = 0;
try {
record = KSQL.executeUpdate(ksql, null, "/swtz/swtree/data", null);
} catch (Exception e) {
throw ExcelException.create(e, "增加第" + i + "行数据时发生错误,导入没有完成!");
}
} else {// 找不到实物分类,停止运行,抛出错误信息
throw ExcelException.create("第" + i + "行,找不到实物分类为:'" + ss + "'的记录!");
}
} else {// 修改数据
System.out.println("导入实物表修改数据");
ksql = "UPDATE SW_SWS_CB t ";
ksql = ksql + "set t.version=t.version+1,t.fMC='" + (row.getCell(3) == null ? "" : getCellValue(row.getCell(3))) + "'"// 名称+"'"
+ ",t.fDJH='" + (row.getCell(4) == null ? "" : getCellValue(row.getCell(4)))// 单据号
+ "',t.fXH='" + (row.getCell(5) == null ? "" : getCellValue(row.getCell(5)))// 型号
+ "',t.fPP='" + (row.getCell(6) == null ? "" : getCellValue(row.getCell(6)))// 品牌
+ "',t.fXLH='" + (row.getCell(7) == null ? "" : getCellValue(row.getCell(7)))// 序列号
+ "',t.fDW='" + (row.getCell(8) == null ? "" : getCellValue(row.getCell(8)))// 单位
+ "',t.fGZRQ=" + (row.getCell(9) == null ? "null" : "'" + getCellValue(row.getCell(9)) + "'")// 购置日期
+ ",t.fGZJG=" + (row.getCell(10) == null ? "0.00" : getCellValue(row.getCell(10)))// 购置价格
+ ",t.fKSSYRQ=" + (row.getCell(11) == null ? "null" : "'" + getCellValue(row.getCell(11)) + "'")// 开始使用日期
+ ",t.fCFDD='" + (row.getCell(12) == null ? "" : getCellValue(row.getCell(12)))// 存放地点
+ "',t.fBGR='" + (row.getCell(13) == null ? "" : getCellValue(row.getCell(13)))// 保管人
+ "',t.fSYBM='" + (row.getCell(14) == null ? "" : getCellValue(row.getCell(14)))// 使用部门
+ "',t.fSWGLBM='" + (row.getCell(15) == null ? "" : getCellValue(row.getCell(15)))// 实物管理部门
+ "',t.fZT='" + (row.getCell(16) == null ? "" : getCellValue(row.getCell(16)))// 状态
+ "',t.fBZ='" + (row.getCell(17) == null ? "" : getCellValue(row.getCell(17)))// 备注
+ "' WHERE t.fSWBM='" + getCellValue(cFid)// 实物编码
+ "'";
int record = 0;
try {
record = KSQL.executeUpdate(ksql, null, "/swtz/swtree/data", null);
} catch (Exception e) {
throw ExcelException.create(e, "修改第" + i + "行数据时发生错误,导入没有完成!");
}
if (record == 0) {
throw ExcelException.create("第" + i + "行,实物编号为:'" + getCellValue(cFid) + "'的记录不存在!");
}
}
// 根据每行第二列的值判断是否导入这行数据 import.mapping.xml里对应的为relation
// name="FINTEGER" cell-number="2" check="true"/>
// 注意:check="true" 必须加在要删除列所对应的relation上
// check:
// excel中行数据是否有效判断,当check="true"时cell-number对应列值为空,认为此行数据无效将被忽略
// if (cell.getNumericCellValue() > 100) { // 如果值大于100
// 对应这行的值就不导入
row.removeCell(cPid);
// }
}
}
/**
* 判断字符串是否为空
*
* @param str
* 需要判断的字符串
* @return 返回真或假
*/
public static boolean isNotNullOrEmpty(String str) {
if (null == str) {
return false;
}
if ("".equals(str.trim())) {
return false;
}
return true;
}
/**
* 获取当前日期时间
*
* @return 返回当前日期时间
*/
public static String getNowDateTime() {
Date date = new Date(new Date().getTime());
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String d = df.format(date);
return d;
}
/**
* 获取当前日期
*
* @return 返回当前日期
*/
public static String getNowDate() {
Date date = new Date(new Date().getTime());
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String d = df.format(date);
return d;
}
/**
* 格式化单元格内的值
*/
public static String getCellValue(Cell cell) {
String ret;
switch (cell.getCellType()) {
case Cell.CELL_TYPE_BLANK:
ret = "";
break;
case Cell.CELL_TYPE_BOOLEAN:
ret = String.valueOf(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_ERROR:
ret = null;
break;
case Cell.CELL_TYPE_FORMULA:
Workbook wb = cell.getSheet().getWorkbook();
CreationHelper crateHelper = wb.getCreationHelper();
FormulaEvaluator evaluator = crateHelper.createFormulaEvaluator();
ret = getCellValue(evaluator.evaluateInCell(cell));
break;
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
Date theDate = cell.getDateCellValue();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ret = df.format(theDate);
} else {
ret = NumberToTextConverter.toText(cell.getNumericCellValue());
}
break;
case Cell.CELL_TYPE_STRING:
ret = cell.getRichStringCellValue().getString();
break;
default:
ret = null;
}
return ret; // 有必要自行trim
}
} |
|