rendered paste body/*
* Storage.java
*
* Created on 22. November 2006, 19:55
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package javaapplication5;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
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;
/**
*
* @author kj09
*/
public class Storage {
//private WritableWorkbook workbook;
//private WritableSheet sheet;
private HSSFWorkbook wb;
private HSSFSheet sheet1;
private FileOutputStream fileOut;
/** Creates a new instance of Storage */
public Storage(String Workbookpath, String SheetName) {
wb = new HSSFWorkbook();
try {
fileOut = new FileOutputStream("output.xls");
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
sheet1 = wb.createSheet("data");
}
public synchronized int add(String[] exdata) throws IOException{
//Letzte Zeile raussuchen:
int lastrow = sheet1.getLastRowNum();
System.out.println(exdata);
for(int i=0; i<exdata.length; i++){//Alle Einträge durchgehen:
//reinschreiben
addData(i, lastrow+1, (String) exdata[i]);
}
return(lastrow);
}
public synchronized int add(String[] exdata, int id) throws IOException{
//Letzte Zeile raussuchen:
int lastrow = sheet1.getLastRowNum();
HSSFRow row = sheet1.createRow((short) lastrow);
int lastcol = row.getLastCellNum();
for(int i=0; i<exdata.length; i++){//Alle Einträge durchgehen:
//reinschreiben
addData(lastcol+i, lastrow+1, (String) exdata[i]);
}
return(lastrow);
}
private synchronized void addData(int Col, int Row, String data) throws IOException{ //Fügt eine Zelle hinzu.
System.out.println("Data saved - Column: "+Col+" Row: "+Row+" Data: "+data);
HSSFRow row = sheet1.createRow((short) Row);
row.createCell((short)Col).setCellValue(data);
}
public void close() throws IOException{
System.out.println("..Finalizing data-storage..");
wb.write(fileOut);
fileOut.close();
System.out.println("Done.");
}
public synchronized int lastrowID(){
return(sheet1.getLastRowNum());
}
}