Re: Excel dump to java array
Hi,
Thanks for helping, I tried lots of things, but ... I can't find the
solutuion.
Hereby my code, I guess the solution is trivial.
How can I dump the values from Lezen.leesL6 into the Array, where
now, for testing, figures 12345?
Please give me a consistent hint.
<code>
class Lezen {
public static void leesL6(){
//int celwaarde = 0;
try {
POIFSFileSystem fs = new POIFSFileSystem (
new FileInputStream("test.xls"));
HSSFWorkbook workBook = new HSSFWorkbook (fs);
HSSFSheet sheetL6 = workBook.getSheetAt(0);
Iterator<Row> rows = sheetL6.rowIterator();
while (rows.hasNext()) {
Row row = rows.next();
Iterator<Cell> cells = row.cellIterator();
while (cells.hasNext()) {
Cell cell = cells.next();
switch(cell.getCellType()){
case Cell.CELL_TYPE_NUMERIC : {
System.out.println(cell.getNumericCellValue());
break;
}
case Cell.CELL_TYPE_FORMULA : {
int formulegetal = (int) cell.getNumericCellValue();
System.out.println(formulegetal);
break;
}
case Cell.CELL_TYPE_STRING : {
HSSFRichTextString richTextString =
(HSSFRichTextString) cell.getRichStringCellValue();
System.out.println("String value: " +
richTextString.getString() );
break;
}
default :{
System.out.println("Type niet ondersteund");
}
}
}
}
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//tel het aantal rijen
class RijenL6 {
public static int aantalrijenL6 (){
int aantal = 0;
try {
POIFSFileSystem fs = new POIFSFileSystem (
new FileInputStream("test.xls"));
HSSFWorkbook workBook = new HSSFWorkbook (fs);
HSSFSheet sheetL6 = workBook.getSheetAt(0); //neemt de eerste
sheet: L6
Iterator<Row> rows = sheetL6.rowIterator();
while (rows.hasNext()) {
Row row = rows.next();
aantal = row.getRowNum();
}
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return (aantal + 1);
}
}
//tel aantal kolommen
class KolommenL6 {
public static int aantalkolommenL6() {
int aantal = 0;
int aantalrijen = 0;
int aantalcellen = 0;
try {
POIFSFileSystem fs = new POIFSFileSystem (
new FileInputStream("test.xls"));
HSSFWorkbook workBook = new HSSFWorkbook (fs);
HSSFSheet sheetL6 = workBook.getSheetAt(0); //neemt de eerste
sheet: L6
Iterator<Row> rows = sheetL6.rowIterator();
int n = 1;
while (rows.hasNext()) {
Row row = rows.next ();
aantalrijen = row.getRowNum();
//aantal cellen tellen
Iterator<Cell> cells = row.cellIterator ();
while (cells.hasNext ()){
Cell cell =cells.next();
aantalcellen = n;
n = n +1;
}
}
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return (aantalcellen/aantalrijen);
}
}
//plaats de gegevens uit L6 in een array
class ArrayL6 {
public int fillArrayL6(int x, int y ) {
int ARRAY_ROWS = RijenL6.aantalrijenL6();
int ARRAY_COLUMNS = KolommenL6.aantalkolommenL6();
int [][] arrayL6 = new int [ARRAY_ROWS][ARRAY_COLUMNS];
for (int i= 0; i < ARRAY_ROWS; i ++) {
for (int j = 0; j < ARRAY_COLUMNS ;j ++) {
for (int k = 0;k <ARRAY_ROWS*ARRAY_COLUMNS; k++){
arrayL6[i][j] = 12345;
}
}
}
return arrayL6[x][y];
}
}
public class ExceltoJavaArray {
public static void main (String args[]) {
Lezen a;
a = new Lezen();
a.leesL6();
RijenL6 r ;
r = new RijenL6();
System.out.println("Aantal rijen: " + r.aantalrijenL6());
KolommenL6 k;
k = new KolommenL6();
System.out.println("Aantal kolommen: " + k.aantalkolommenL6());
ArrayL6 lijstL6;
lijstL6 = new ArrayL6();
System.out.println ("Arraytest " + lijstL6.fillArrayL6(6,4));
}
}
</code>