Результат Excel Excel из БД Oracle - PullRequest
0 голосов
/ 15 апреля 2019

Я написал нижеприведенный Java-код, который прекрасно работает для экспорта результатов в файл xlsx из БД Oracle.Но может ли кто-нибудь помочь мне улучшить производительность этого кода, с помощью которого приведенный ниже код может очень быстро извлечь результат.

Ниже приведен код Java, который я пробовал на данный момент;Но при экспорте результата он медленный по сравнению с экспортом в Excel из SQL Developer

public class FilesFromFolder {
    private Workbook writeWorkbook;

    public void ExportService(DBConnection con) {

        writeWorkbook = new XSSFWorkbook();
        Sheet desSheet = writeWorkbook.createSheet("Data");
        Statement stmt = null;
        ResultSet rs = null;
        int columnsNumber = 0;
        ResultSetMetaData rsmd = null;
        FileOutputStream fileOut = null;
        Connection cntn = null;
        String filePath = "C:\\Users\\Desktop\\OracleExport";
        File files = new File(filePath);
        File[] file = files.listFiles();
        String fileNameWithOutExt = null;
        if (file != null) {
            for (int i = 0; i < file.length; i++) {
                if (file[i].isFile()) {
                    String tempFilename = file[i].getName();
                    fileNameWithOutExt = tempFilename.replaceFirst("[.][^.]+$", "");
                    File fileTemp = file[i];
                    try {
                        String fileContent = FileUtils.readFileToString(fileTemp, "UTF8");
                        // System.out.println(fileContent);
                        cntn = con.getConnection();
                        stmt = cntn.createStatement();
                        rs = stmt.executeQuery(fileContent);
                        rsmd = rs.getMetaData();
                        columnsNumber = rsmd.getColumnCount();
                        Row desRow1 = desSheet.createRow(0);
                        for (int col = 0; col < columnsNumber; col++) {
                            Cell newpath = desRow1.createCell(col);
                            newpath.setCellValue(rsmd.getColumnLabel(col + 1));
                        }
                        while (rs.next()) {
                            System.out.println("Row number -->" + rs.getRow());
                            Row desRow = desSheet.createRow(rs.getRow());
                            for (int col = 0; col < columnsNumber; col++) {
                                Cell newpath = desRow.createCell(col);
                                newpath.setCellValue(rs.getString(col + 1));
                            }
                            String outputFile = "C:\\Users\\Desktop\\OracleExport\\" + fileNameWithOutExt
                                    + ".xlsx";
                            fileOut = new FileOutputStream(outputFile);
                            writeWorkbook.write(fileOut);
                        }
                        System.out.println(fileNameWithOutExt + " export complete");
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    } finally {
                        if (fileOut!= null) {
                            try {
                                fileOut.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        if (cntn != null) {
                            con.closeConnection();
                        }
                    }
                }
            }
        }
    }
}
...