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

QQ登录

只需一步,快速开始

查看: 2941|回复: 4

[结贴] 导入数据的几个问题?急

[复制链接]

170

主题

838

帖子

2136

积分

金牌会员

Rank: 6Rank: 6

积分
2136
QQ
发表于 2017-3-24 14:59:44 | 显示全部楼层 |阅读模式
本帖最后由 hua_love2011 于 2017-3-24 15:00 编辑

按照这个帖子的步骤做(http://bbs.wex5.com/forum.php?mod=viewthread&tid=37297),但在导入时出现以下问题
1、在导入时,报如下错误信息
Caused by: java.lang.ClassCastException: org.apache.poi.xssf.usermodel.XSSFWorkbook cannot be cast to org.apache.poi.ss.usermodel.Workbook
at ZxjcglProcess.mainActivityBeforeImportExcelBeforeAction(ZxjcglProcess.java:30)
也就是这行代码有误
Workbook workBook = (Workbook) ModelUtils.getRequestContext().getActionContext().getParameter("excel");


2、如果导入主从表,且格式不一样能否导入,前台的import.mapping文件应该怎么写。格式如下:
QQ截图20170324145907.png

你不向前奔跑,没有人愿意停下来等你!

91

主题

13万

帖子

3万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
35918
发表于 2017-3-24 15:52:40 | 显示全部楼层
1.确认导入的类是否正确
构建路径中加的jar是否正确

2.平台不会去分析导入到几行导入从表
最好是分开两个shell也导入,自己获取第二个shell中的数据进行导入
http://bbs.wex5.com/forum.php?mod=viewthread&tid=49351
远程的联系方法QQ1392416607,添加好友时,需在备注里注明其论坛名字及ID,公司等信息
发远程时同时也发一下帖子地址,方便了解要解决的问题  WeX5教程  WeX5下载



如按照该方法解决,请及时跟帖,便于版主结贴
回复 支持 反对

使用道具 举报

170

主题

838

帖子

2136

积分

金牌会员

Rank: 6Rank: 6

积分
2136
QQ
 楼主| 发表于 2017-3-24 21:40:39 | 显示全部楼层
jishuang 发表于 2017-3-24 15:52
1.确认导入的类是否正确
构建路径中加的jar是否正确


我在code下新建了一个lib文件夹,并将/SA/excel/logic/code下的jar包拷贝到该目录下,并将jar包导入;
然后在process中引用了/SA/excel/logic/action和/SA/excel/logic/code目录,
并在动作设置中添加importExcelBeforeAction的执行前事件,部分代码如下:
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();
。。。。。。
}
}
目录结构如下:
QQ截图20170324212519.png
jar包导入如下:
QQ截图20170324212855.png
w文件如下:
QQ截图20170324214002.png

你不向前奔跑,没有人愿意停下来等你!
回复 支持 反对

使用道具 举报

91

主题

13万

帖子

3万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
35918
发表于 2017-3-27 09:54:16 | 显示全部楼层
java中import导入的正确吗?
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
远程的联系方法QQ1392416607,添加好友时,需在备注里注明其论坛名字及ID,公司等信息
发远程时同时也发一下帖子地址,方便了解要解决的问题  WeX5教程  WeX5下载



如按照该方法解决,请及时跟帖,便于版主结贴
回复 支持 反对

使用道具 举报

170

主题

838

帖子

2136

积分

金牌会员

Rank: 6Rank: 6

积分
2136
QQ
 楼主| 发表于 2017-4-14 11:59:21 | 显示全部楼层
jishuang 发表于 2017-3-27 09:54
java中import导入的正确吗?
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermo ...

正确的。不过我现在用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
        }
}
你不向前奔跑,没有人愿意停下来等你!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-4-26 23:03 , Processed in 0.066551 second(s), 26 queries .

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

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