Java 读取excel宏的速度非常慢

Java 读取excel宏的速度非常慢,java,eclipse,excel,Java,Eclipse,Excel,我正在使用eclipse阅读java中的excel宏文件。当我运行应用程序时,它非常慢。应用程序需要大量时间来读取宏。我不明白我做错了什么:( 你能给我一些建议吗,帮帮我 该方法读取excel宏并将读取的值保存到映射中。Hier是我的代码: /** * <b>This method only work with Excel file with xlsx or xlsm extension!</b> * </br> This method r

我正在使用eclipse阅读java中的excel宏文件。当我运行应用程序时,它非常慢。应用程序需要大量时间来读取宏。我不明白我做错了什么:(

你能给我一些建议吗,帮帮我

该方法读取excel宏并将读取的值保存到映射中。Hier是我的代码:

/**
     * <b>This method only work with Excel file with xlsx or xlsm extension!</b>
     * </br> This method read an ExcelSheet from the given Directory, get the ID
     * and his value and transform the value in hexformat. </br>The ID and its
     * value will be store in a Hashmap as <b>Key - Value pair</b>, so you kann
     * search a value according to a key. </br>You have to enter the position of
     * the sheet to read into the excel file, ex: If the sheet to read is the
     * first sheet of the excel file please enter 0, if it is the second please
     * enter 1 etc...
     * 
     * @param inputPath
     *            the path to the excel file
     * @param sheetToRead
     *            is the position of the sheet to read.
     * @param PLACellPosition
     *            the position of the PLA Colum in the excel file. If the colum
     *            exists, a number should be wrote, else write null.
     * @param FourKCellPosition
     *            the position of the 4K Colum in the excel file. If the colum
     *            exists, a number should be wrote, else write null.
     * @param EIGTHKCellPosition
     *            the position of the 8K Colum in the excel file. If the colum
     *            exists, a number should be wrote, else write null.
     * @return all needed values as a map.
     */
    @SuppressWarnings("boxing")
    public HashMap<String, String> readCarConfig(String inputPath,
            int sheetToRead, Integer PLACellPosition,
            Integer FourKCellPosition, Integer EIGTHKCellPosition) {
        FileInputStream fileIn = null;
        Cell cellIDValue = null;
        HashMap<String, String> myMap = new HashMap<String, String>();
        try {
            if (inputPath.endsWith(".xlsx") || inputPath.endsWith(".xlsm")) { //$NON-NLS-1$ //$NON-NLS-2$
                fileIn = new FileInputStream(new File(inputPath));
                /** Get the workbook instance for XLS file */
                XSSFWorkbook workbook = new XSSFWorkbook(fileIn);

                /** Get first sheet from the workbook */
                XSSFSheet sheet = workbook.getSheetAt(sheetToRead);

                /** Get iterator to all the rows in current sheet */
                Iterator<Row> rowIterator = sheet.rowIterator();
                Row row = sheet.getRow(2);

                /** Get iterator to all rows of current sheet */
                while (rowIterator.hasNext()) {
                    Cell first = row.getCell(0);
                    /** get the ID from the excel file, if its not null */
                    if (first != null) {
                        if (first.toString().startsWith("$")) { //$NON-NLS-1$
                            String CellID = first.getStringCellValue()
                                    .substring(
                                            1,
                                            first.getStringCellValue().indexOf(
                                                    " ")); //$NON-NLS-1$

                            //                          System.out.println("cellID: " + CellID); //$NON-NLS-1$
                            /**looks if the PLA Column exists*/
                            if (PLACellPosition != null) {

                                cellIDValue = row.getCell(PLACellPosition);
                                if (cellIDValue == null) {
                                    cellIDValue
                                            .setCellType(Cell.CELL_TYPE_BLANK);
                                }
                                //                                  System.out.println("cell1: " + cellIDValue); //$NON-NLS-1$
                                if (cellIDValue.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                                    int cellIDValueNumeric = (int) cellIDValue
                                            .getNumericCellValue();
                                    if (CellID.equalsIgnoreCase("F17C")) { //$NON-NLS-1$
                                        myMap.put(CellID, ""); //$NON-NLS-1$
                                    } else {
                                        myMap.put(
                                                CellID,
                                                Integer.toHexString(cellIDValueNumeric));
                                    }
                                } else if (cellIDValue.getCellType() == Cell.CELL_TYPE_BLANK) {
                                    String cellIDValueNumeric = " "; //$NON-NLS-1$
                                    // myMap.put(CellID,
                                    // cellIDValueNumeric);
                                    if (CellID.equalsIgnoreCase("F17C")) { //$NON-NLS-1$
                                        myMap.put(CellID, ""); //$NON-NLS-1$
                                    } else {
                                        myMap.put(CellID, cellIDValueNumeric);
                                    }
                                } else if (cellIDValue.getCellType() == Cell.CELL_TYPE_STRING) {
                                    String cellIDValueNumeric = cellIDValue
                                            .getStringCellValue();
                                    if (CellID.equalsIgnoreCase("F17C")) { //$NON-NLS-1$
                                        myMap.put(CellID, ""); //$NON-NLS-1$
                                    } else {

                                        myMap.put(CellID,
                                                this.toHex(cellIDValueNumeric));
                                    }
                                }
                                if (rowIterator.hasNext()) {
                                    row = rowIterator.next();
                                    // System.out
                                    //                                      .println("Next Row cell1: " + row.getCell(0)); //$NON-NLS-1$
                                }

                            } 
                            /**looks if the 8K Column exists*/
                            else if (EIGTHKCellPosition != null) {

                                cellIDValue = row.getCell(EIGTHKCellPosition);
                                if (cellIDValue == null) {
                                    cellIDValue
                                            .setCellType(Cell.CELL_TYPE_BLANK);
                                }
                                System.out.println("cell1: " + cellIDValue); //$NON-NLS-1$
                                if (cellIDValue.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                                    int cellIDValueNumeric = (int) cellIDValue
                                            .getNumericCellValue();
                                    if (CellID.equalsIgnoreCase("F17C")) { //$NON-NLS-1$
                                        myMap.put(CellID, ""); //$NON-NLS-1$
                                    } else {
                                        myMap.put(
                                                CellID,
                                                Integer.toHexString(cellIDValueNumeric));
                                    }
                                } else if (cellIDValue.getCellType() == Cell.CELL_TYPE_BLANK) {
                                    String cellIDValueNumeric = " "; //$NON-NLS-1$
                                    // myMap.put(CellID,
                                    // cellIDValueNumeric);
                                    if (CellID.equalsIgnoreCase("F17C")) { //$NON-NLS-1$
                                        myMap.put(CellID, ""); //$NON-NLS-1$
                                    } else {
                                        myMap.put(CellID, cellIDValueNumeric);
                                    }
                                } else if (cellIDValue.getCellType() == Cell.CELL_TYPE_STRING) {
                                    String cellIDValueNumeric = cellIDValue
                                            .getStringCellValue();
                                    if (CellID.equalsIgnoreCase("F17C")) { //$NON-NLS-1$
                                        myMap.put(CellID, ""); //$NON-NLS-1$
                                    } else {

                                        myMap.put(CellID,
                                                toHex(cellIDValueNumeric));
                                    }
                                }
                                if (rowIterator.hasNext()) {
                                    row = rowIterator.next();
                                    System.out
                                            .println("Next Row cell1: " + row.getCell(0)); //$NON-NLS-1$
                                }

                            } 
                            /**looks if the 4K Column exists*/
                            else if (FourKCellPosition != null) {

                                cellIDValue = row.getCell(FourKCellPosition);
                                if (cellIDValue == null) {
                                    cellIDValue
                                            .setCellType(Cell.CELL_TYPE_BLANK);
                                }
                                //                                  System.out.println("cell1: " + cellIDValue); //$NON-NLS-1$
                                if (cellIDValue.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                                    int cellIDValueNumeric = (int) cellIDValue
                                            .getNumericCellValue();
                                    if (CellID.equalsIgnoreCase("F17C")) { //$NON-NLS-1$
                                        myMap.put(CellID, ""); //$NON-NLS-1$
                                    } else {
                                        myMap.put(CellID,

                                        Integer.toHexString(cellIDValueNumeric));
                                    }
                                } else if (cellIDValue.getCellType() == Cell.CELL_TYPE_BLANK) {
                                    String cellIDValueNumeric = " "; //$NON-NLS-1$
                                    // myMap.put(CellID,
                                    // cellIDValueNumeric);
                                    if (CellID.equalsIgnoreCase("F17C")) { //$NON-NLS-1$
                                        myMap.put(CellID, ""); //$NON-NLS-1$
                                    } else {
                                        myMap.put(CellID, cellIDValueNumeric);
                                    }
                                } else if (cellIDValue.getCellType() == Cell.CELL_TYPE_STRING) {
                                    String cellIDValueNumeric = cellIDValue
                                            .getStringCellValue();
                                    if (CellID.equalsIgnoreCase("F17C")) { //$NON-NLS-1$
                                        myMap.put(CellID, ""); //$NON-NLS-1$
                                    } else {

                                        myMap.put(CellID,
                                                toHex(cellIDValueNumeric));
                                    }
                                }
                                if (rowIterator.hasNext()) {
                                    row = rowIterator.next();
                                    // System.out
                                    //                                              .println("Next Row cell1: " + row.getCell(0)); //$NON-NLS-1$
                                }
                                break;
                            }

                        } else {
                            if (rowIterator.hasNext()) {
                                row = rowIterator.next();
                            }

                        }
                    } else {
                        if (rowIterator.hasNext()) {
                            row = rowIterator.next();
                        }
                    }

                }

                // System.out.println(myMap);
                if (fileIn != null) {
                    fileIn.close();
                }
            } else {

                System.err
                        .print("WRONG FILE EXTENSION: Please check the file extension!"); //$NON-NLS-1$
            }
        }

        catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return myMap;

    }
/**
*此方法仅适用于扩展名为xlsx或xlsm的Excel文件!
*
此方法从给定目录读取Excel工作表,获取ID *和他的值,并将值转换为十六进制格式。
ID及其 *值将作为键值对存储在Hashmap中,因此 *根据键搜索值。
您必须输入 *要读取到excel文件中的工作表,例如:如果要读取的工作表是 *excel文件的第一页请输入0,如果是第二页请输入 *输入1等。。。 * *@param inputPath *excel文件的路径 *@param sheetToRead *是要读取的图纸的位置。 *@param PLACellPosition *PLA列在excel文件中的位置。如果 *存在,应写入一个数字,否则写入null。 *@param-fourkcell位置 *excel文件中4K列的位置。如果 *存在,应写入一个数字,否则写入null。 *@param eigthkcell位置 *8K列在excel文件中的位置。如果 *存在,应写入一个数字,否则写入null。 *@以映射形式返回所有需要的值。 */ @抑制警告(“装箱”) 公共HashMap readCarConfig(字符串输入路径, 内部图纸读取,整数位置, 整数FourKCellPosition,整数EIGTHKCellPosition){ FileInputStream fileIn=null; Cell-cellIDValue=null; HashMap myMap=新HashMap(); 试一试{ if(inputPath.endsWith(“.xlsx”)| | inputPath.endsWith(“.xlsm”){//$NON-NLS-1$/$NON-NLS-2$ fileIn=newfileinputstream(新文件(inputPath)); /**获取XLS文件的工作簿实例*/ XSSF工作簿=新XSSF工作簿(fileIn); /**从工作簿中获取第一张工作表*/ XSSFSheet sheet=workbook.getSheetAt(sheetread); /**获取当前工作表中所有行的迭代器*/ 迭代器rowIterator=sheet.rowIterator(); Row Row=sheet.getRow(2); /**获取当前工作表所有行的迭代器*/ while(roweiterator.hasNext()){ Cell first=row.getCell(0); /**从excel文件中获取ID(如果不为null)*/ 如果(第一个!=null){ if(first.toString().startsWith(“$”){/$NON-NLS-1$ String CellID=first.getStringCellValue() .子串( 1. first.getStringCellValue().indexOf( “”);//$NON-NLS-1$ //System.out.println(“cellID:+cellID);/$NON-NLS-1$ /**查看PLA列是否存在*/ if(PLACellPosition!=null){ cellIDValue=row.getCell(PLACellPosition); if(cellIDValue==null){ 细胞值 .setCellType(Cell.Cell\u TYPE\u BLANK); } //System.out.println(“cell1:+cellIDValue);/$NON-NLS-1$ if(cellIDValue.getCellType()==Cell.Cell\u TYPE\u NUMERIC){ int cellIDValueNumeric=(int)cellIDValue .getNumericCellValue(); if(CellID.equalsIgnoreCase(“F17C”){/$NON-NLS-1$ myMap.put(CellID,“”;//$NON-NLS-1$ }否则{ 我的地图( 细胞, toHexString(cellIDValueNumeric)); } }else if(cellIDValue.getCellType()==Cell.Cell\u TYPE\u BLANK){ 字符串cellIDValueNumeric=“”;//$NON-NLS-1$ //myMap.put(CellID, //cellIDValueNumeric); if(CellID.equalsIgnoreCase(“F17C”){/$NON-NLS-1$ myMap.put(CellID,“”;//$NON-NLS-1$ }否则{ myMap.put(CellID,cellIDValueNumeric); } }else if(cellIDValue.getCellType()==Cell.Cell\u TYPE\u字符串){ 字符串cellIDValueNumeric=cellIDValue .getStringCellValue(); if(CellID.equalsIgnoreCase(“F17C”){/$NON-NLS-1$ myMap.put(CellID,“”;//$NON-NLS-1$ }否则{ myMap.put(CellID, this.toHex(cellIDValueNumeric)); } } if(roweiterator.hasNe)