我正在使用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;
}