使用核心java验证Excel工作表

时间:2017-12-04 10:58:53

标签: java excel apache

我已经构建了核心java代码来读取excel数据并推入数据库。它工作正常。现在我想在excel表中添加一些验证检查。我无法做到这一点。考虑我有一些列是SettleDate,TradeDate,TradeID,IFStatus,IFVersion,IFDate,IFTime。我想在vlidation规则下面做。

  1. 定居不应该超过TradeDate。
  2. TradID应为14 letteres。
  3. IFStatus,IFVersion,IFDate,IFTime应为空白
  4. 以下是我的代码........

    try {
        Class forName = Class.forName("com.sybase.jdbc2.jdbc.SybDriver");
        Connection con = null;
        con = DriverManager.getConnection("jdbc:sybase:Tds:tkbgssvt1:4105", "kauai_rwu", "rwu_kauai");
        System.out.println("Database connected to Sybase..");
        con.setAutoCommit(false);
        PreparedStatement pstm = null;
        FileInputStream input = new FileInputStream(
                "C:\\Users\\suresnar\\Desktop\\Mizu_FGloss\\Kauai_IN_Table_test.xlsx");                
        XSSFWorkbook workbook = new XSSFWorkbook(input);
        XSSFSheet sheet = workbook.getSheetAt(0);
        Row row;
        for (int i = 1; i <= sheet.getLastRowNum(); i++) {
            row = (Row) sheet.getRow(i);
            DataFormatter formatter = new DataFormatter();
            String TradeAction = formatter.formatCellValue(row.getCell(0));
            String TradeID = formatter.formatCellValue(row.getCell(1));
            String Version = formatter.formatCellValue(row.getCell(2));
            String TradeType = formatter.formatCellValue(row.getCell(3));
            String Book = formatter.formatCellValue(row.getCell(4));
            String Direction = row.getCell(5).getStringCellValue();
            String SafekeepFlag = row.getCell(6).getStringCellValue();
            String OurSettlePlace = formatter.formatCellValue(row.getCell(7));
            String OurSettleDepot = row.getCell(8).getStringCellValue();
            String TheirSettlePlace = row.getCell(9).getStringCellValue();
            String TheirSettleDepot = row.getCell(10).getStringCellValue();
            String BrokerCode = row.getCell(11).getStringCellValue();
            String CustomerCode = formatter.formatCellValue(row.getCell(12));
            String ProductCode = formatter.formatCellValue(row.getCell(13));
            String TradeDate = formatter.formatCellValue(row.getCell(14));
            String TradeTime = formatter.formatCellValue(row.getCell(15));
            String CreditPerson = row.getCell(16).getStringCellValue();
            String Quantity = formatter.formatCellValue(row.getCell(17));
            String Factor = formatter.formatCellValue(row.getCell(18));
            String ActualQuantity = formatter.formatCellValue(row.getCell(19));
            String SettleDate = formatter.formatCellValue(row.getCell(20));
            String Price = formatter.formatCellValue(row.getCell(21));
            String TradeCcy = row.getCell(22).getStringCellValue();
            String TradeValue = formatter.formatCellValue(row.getCell(23));
            String AccDays = formatter.formatCellValue(row.getCell(24));
            String TradeAICcy = formatter.formatCellValue(row.getCell(25));
            String TradeAI = formatter.formatCellValue(row.getCell(26));
            String SettleAmt = formatter.formatCellValue(row.getCell(27));
            String RateCalc = formatter.formatCellValue(row.getCell(28));
            String FxRate = formatter.formatCellValue(row.getCell(29));
            String SettleCcy = formatter.formatCellValue(row.getCell(30));
            String SettleAmtInFX = formatter.formatCellValue(row.getCell(31));
            String Memo1 = row.getCell(32).getStringCellValue();
            String Memo2 = row.getCell(33).getStringCellValue();
            String Memo3 = row.getCell(34).getStringCellValue();
            String Memo4 = row.getCell(35).getStringCellValue();
            String Memo5 = row.getCell(36).getStringCellValue();
            String Remark1 = row.getCell(37).getStringCellValue();
            String Remark2 = row.getCell(38).getStringCellValue();
            String SelfStBrCode = formatter.formatCellValue(row.getCell(39));
            String SelfStBrSubCode = formatter.formatCellValue(row.getCell(40));
            String CustStBrCode = formatter.formatCellValue(row.getCell(41));
            String CustStClCode = formatter.formatCellValue(row.getCell(42));
            String CustStSubCode = formatter.formatCellValue(row.getCell(43));
            String DiscretionFlg = formatter.formatCellValue(row.getCell(44));
            String InputTime = formatter.formatCellValue(row.getCell(45));
            String UpdateTime = formatter.formatCellValue(row.getCell(46));
            String IFStatus = formatter.formatCellValue(row.getCell(47));
            String IFVersion = formatter.formatCellValue(row.getCell(48));
            String IFDate = formatter.formatCellValue(row.getCell(49));
            String IFTime = formatter.formatCellValue(row.getCell(50));
            String AzIFStatus = formatter.formatCellValue(row.getCell(51));
            String AzIFVersion = formatter.formatCellValue(row.getCell(52));
            String AzIFTime = formatter.formatCellValue(row.getCell(53));
            String AzCKTime = formatter.formatCellValue(row.getCell(54));
            String sql = "INSERT INTO FB_KAUAI_IN (\"my query\")";
            pstm = (PreparedStatement) con.prepareStatement(sql);
            pstm.execute();
            System.out.println("Imported rows " + i);
        }
        con.commit();
        pstm.close();
        con.close();
        input.close();
        System.out.println("Success import excel to mysql table");
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        // Handle errors for Class.forName
        e.printStackTrace();
    }
    

    有人能帮帮我吗? 你的帮助很大... 谢谢, Suresh N

1 个答案:

答案 0 :(得分:0)

我想有几种不同的方法可以做到这一点:

  • 如果您真的拥有Excel文件并创建它。您可以创建自己的公式来“验证”您想要的单元格。
  • 您可以为要验证的每种类型创建不同的类,每个“单元格”都有自己的类/验证
  • 您可以手动创建验证并申请您要验证的每个字段(直接在java中,根据您从“字符串”中获取 excel文件)。
  • 因为您(真的)应该将SQL查询更改为Hibernate,之后您可以使用hibernate验证器(在插入db之前)。