从Excel文件读取失败(ORA-00947-值不够)

时间:2015-02-05 13:08:20

标签: java apache-poi import-from-excel

使用第一种方法我将查询结果写入xls文件,然后使用第二种方法将上传到目标数据库。它适用于第一种方法创建的Excel文件。如果我编辑任何创建的excel文件,它无法读取一些单元格并抛出错误。任何机构都面临这个问题。任何解决方案或建议。 TIA

public void exportData(Map<String, String> queries,Connection conn) {
    Connection connection=conn; 
    try{
            List<String> list1=new ArrayList<String>(queries.keySet());
            for(int j=0;list1.size()>j;j++){
                Workbook book1=new HSSFWorkbook();
                Sheet Sheet1= book1.createSheet("sheet1");
                PreparedStatement preparedStatement = connection.prepareStatement(queries.get(list1.get(j)));
                ResultSet resultSet = preparedStatement.executeQuery();
                ResultSetMetaData rsmd=resultSet.getMetaData();
                boolean isDataAvail = resultSet.next();
                if(isDataAvail){
                    Row headerRow = Sheet1.createRow(0);
                    for (int i = 1; i <= rsmd.getColumnCount(); i++) {
                        Cell cell1 = headerRow.createCell(i-1);
                        cell1.setCellValue(rsmd.getColumnName(i));
                        }
                    int row=1;
                    do {
                        Row row1 = Sheet1.createRow(row);
                        for (int i = 1; i <=rsmd.getColumnCount(); i++) {
                            Cell cell2 = row1.createCell(i-1);
                            String s=rsmd.getColumnTypeName(i);
                            if(s.equalsIgnoreCase("NUMBER")){
                                if(resultSet.getString(i)==null)
                                cell2.setAsActiveCell();
                                else{
                                cell2.setCellValue(resultSet.getDouble(i));
                                }
                            }else if(s.equalsIgnoreCase("VARCHAR2")||s.equalsIgnoreCase("VARCHAR")){
                                if(resultSet.getString(i)==null)
                                cell2.setAsActiveCell();
                                else
                                cell2.setCellValue(resultSet.getString(i));
                                }else if(s.equalsIgnoreCase("DATE")){
                                if(resultSet.getDate(i)==null)
                                cell2.setAsActiveCell();
                                else{
                                cell2.setCellValue((Date)resultSet.getTimestamp(i));
                                CellStyle style = book1.createCellStyle();
                                style.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
                                cell2.setCellStyle(style);
                                }
                                }else{
                                cell2.setCellValue(resultSet.getString(i));
                                }
                            } row++;
                    }while (resultSet.next());
                    String fname1=folder+list1.get(j)+".xls";
                    FileOutputStream output;
                         output=new FileOutputStream(new File(fname1));
                    book1.write(output);
                    output.close();
                    listOfDataTables.add(list1.get(j));
                    fileNum++;
                    query++;
                    logger.info("F.no."+fileNum+"."+list1.get(j)+"--File created successfully");
                    }
                else{
                    query++;
                    logger.info("No data found for table"+"("+query+")     :"+list1.get(j));
                    }
                resultSet.close();
            }
            }
        catch (Exception exception) {
            logger.info("*****ERROR*****"+exception);
            logger.log(Level.SEVERE, exception.getMessage(), exception); 
            exception.getLocalizedMessage();
            exception.printStackTrace();
            status="ERROR";
        }
        finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (Exception e) {
                    logger.info("*****ERROR*****"+e);
                    e.printStackTrace();
                }
            }
        }
    }





public int dataTransfer(String fileName,Connection connection) {
        int no=0;
        String fName=folder+fileName+".xls";
        try {
        Statement preparedStatement =connection.createStatement();
        FileInputStream inputFile = new FileInputStream(fName);
        POIFSFileSystem myFileSystem = new POIFSFileSystem(inputFile);
        HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);
        HSSFSheet mySheet = myWorkBook.getSheetAt(0);
        Iterator<Row> rowIterator = mySheet.iterator();
        String insertQuery="";
        String columnQuery="";
        if(rowIterator.hasNext()){
            columnQuery="insert into "+fileName+"(";
                        Row row = rowIterator.next(); 
                        Iterator<Cell> cellIterator = row.cellIterator(); 
                        while (cellIterator.hasNext())               
                        {
                        Cell cell = cellIterator.next();
                        columnQuery=columnQuery+cell.getStringCellValue()+",";
                        no++;
                        }
                        columnQuery=columnQuery.substring(0, columnQuery.length()-1);
                        columnQuery=columnQuery+") values (";
                        }
        int rowCounter=0;
        int set=0;
        while (rowIterator.hasNext() && set<=no)          
        {   
            insertQuery=columnQuery;
                        Row row = rowIterator.next();   
                        Iterator<Cell> cellIterator = row.cellIterator(); 
                        while (cellIterator.hasNext())               
                        { 
                            Cell cell = cellIterator.next();  
                                if(cell.getCellType()==Cell.CELL_TYPE_NUMERIC){
                                    if(HSSFDateUtil.isCellDateFormatted(cell)){
                                        double temp=cell.getNumericCellValue();
                                        Date date = HSSFDateUtil.getJavaDate(temp);                             
                                            String dateFmt = cell.getCellStyle().getDataFormatString();
                                            String strValue = new CellDateFormatter(dateFmt).format(date); 
                                            strValue="to_date("+"'"+strValue+"'"+",'mm/dd/yyyy hh24:mi')";
                                            insertQuery=insertQuery+strValue+",";       
                                    }else{
                                        insertQuery=insertQuery+cell.getNumericCellValue()+",";
                                    }
                                }
                                else if(cell.getCellType()==Cell.CELL_TYPE_STRING)
                                    insertQuery=insertQuery+"'"+cell.getStringCellValue()+"',";
                                else if(cell.getCellType()==2){
                                    insertQuery=insertQuery+cell.getDateCellValue()+",";
                                }
                                else if(cell.getCellType()==Cell.CELL_TYPE_BLANK)
                                    insertQuery=insertQuery+"null,";
                        }
                        insertQuery=insertQuery.substring(0, insertQuery.length()-1);
                        insertQuery=insertQuery+")";
                        preparedStatement.addBatch(insertQuery);System.out.println(insertQuery);
                        rowCounter++;
                    }
                    preparedStatement.executeBatch();
                    inputFile.close();
                    listOfProcessedTables.add(fileName);
                    preparedStatement.close();
                    return rowCounter;
            }
            catch (Exception exception) {
                    logger.info("*****ERROR*****"+exception);
                    exception.printStackTrace();
                    logger.log(Level.SEVERE, exception.getMessage(), exception); 
                    StatusCount++;
                    return 0;
            }
            }

0 个答案:

没有答案