0

How can I put the sheetData from my function below into a Hashmap?

private static void showExcelData(List sheetData) {

for (int i = 0; i < sheetData.size(); i++) {
  List list = (List) sheetData.get(i);
    for (int j = 0; j < list.size(); j++) {
      Cell cell = (Cell) list.get(j);
      if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC)
        System.out.print(cell.getNumericCellValue());
      else if (cell.getCellType() == Cell.CELL_TYPE_STRING)
        System.out.print(cell.getRichStringCellValue());
      else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN)
        System.out.print(cell.getBooleanCellValue());
    }
    if (j < list.size() - 1) {
      System.out.print(", ");
    }
}
  System.out.println("");
}

}

The program is reading the data from an excel file!

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;

public class readexcel{

@SuppressWarnings({ "unchecked", "unchecked" })
public static void main(String[] args) throws Exception {

String filename = "C:\\Users\\xxxx\\Documents\\test5.xls";


List sheetData = new ArrayList();
FileInputStream fis = null;
try {

fis = new FileInputStream(filename);


HSSFWorkbook workbook = new HSSFWorkbook(fis);

HSSFSheet sheet = workbook.getSheetAt(0);

Iterator rows = sheet.rowIterator();
while (rows.hasNext()) {
HSSFRow row = (HSSFRow) rows.next();
Iterator cells = row.cellIterator();

List data = new ArrayList();
while (cells.hasNext()) {
HSSFCell cell = (HSSFCell) cells.next();
data.add(cell);
}

sheetData.add(data);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
fis.close();
}
}

showExcelData(sheetData);
}

private static void showExcelData(List sheetData) {

for (int i = 0; i < sheetData.size(); i++) {
List list = (List) sheetData.get(i);
for (int j = 0; j < list.size(); j++) {
Cell cell = (Cell) list.get(j);
if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
System.out.print(cell.getNumericCellValue());
} else if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
System.out.print(cell.getRichStringCellValue());
} else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
System.out.print(cell.getBooleanCellValue());
 }
if (j < list.size() - 1) {
System.out.print(", ");
}
}
System.out.println("");
 }
 }
 }
eftixia
  • 11
  • 2
  • 2
    Please indent your code properly, it is very difficult to read otherwise. – devrobf Apr 11 '13 at 12:18
  • Have you tried something where you use the index of the cell, and map the cell to that? What is contained in a Cell object, what have you tried to do so far? – CBredlow Apr 11 '13 at 12:20
  • Possible duplicate of http://stackoverflow.com/q/14136721/545127 – Raedwald Apr 11 '13 at 12:32
  • Even with the update, I still don't know what is contained inside the cell objects. If it is a custom thing, can't you create an index to map to? – CBredlow Apr 11 '13 at 13:12

1 Answers1

0

It's not clear, but I think you are asking how to use the Strategy design pattern, so you use a Map rather than if..else if.

So you need to

  1. Write an interface or abstract base class that represents the abstract operation.
  2. Write a mapping of types to operations (a Map from the cell types to the operations.
  3. Alter your showExcelData method to use that mapping to find which operation to use, and then delegate to that operation.
Dharman
  • 30,962
  • 25
  • 85
  • 135
Raedwald
  • 46,613
  • 43
  • 151
  • 237