save contents of excel file before reading it using poi
I am making an application that created 2 new spreadsheet files. The idea
is for the user to enter data into this excel files and make program read
it. However the problem is that, after the data has been entered and saved
(pressing the save icon in excel) the program just does not see this
data,it remains as if nothing was entered there. What I need is somehow to
save it programmicaly using poi. Any suggestions?
Here is the code:
my gui class:
public class GUI extends JFrame{
private Panel buttonHolder;
private Button crossReference;
private Button generateHPD;
private CrossReference ref = null;
private XSSFWorkbook log;
private XSSFWorkbook sheet;
private XSSFSheet logSheet;
private XSSFSheet sSheet;
public GUI () throws Exception{
this.setTitle("NYWM Cross Reference Application");
this.setSize(400,100);
this.setVisible(true);
this.setLocationRelativeTo(null);
buttonHolder = new Panel (new BorderLayout());
this.add(buttonHolder);
crossReference = new Button ("CrossReference");
generateHPD = new Button ("Generate HPD");
buttonHolder.add(crossReference, BorderLayout.NORTH);
buttonHolder.add(generateHPD, BorderLayout.SOUTH);
logSheet = createExcelSheet ("D:/Log.xlsx", "Log");
sSheet = createExcelSheet("D:/Spreadsheet.xlsx", "Spreadsheet");
crossReference.addActionListener(new crossReferenceButtonListener());
generateHPD.addActionListener(new generateHPDButtonListener());
}
public XSSFSheet getLogSheet() {
return logSheet;
}
public XSSFSheet getsSheet() {
return sSheet;
}
private XSSFSheet createExcelSheet (String path, String fileName) throws
Exception{
try{
FileOutputStream out = new FileOutputStream (path);
XSSFWorkbook wb = new XSSFWorkbook ();
XSSFSheet sheet = wb.createSheet(fileName);
wb.write(out);
Process p = Runtime.getRuntime().exec(
"rundll32 url.dll, FileProtocolHandler " + path);
return sheet;
}
catch (Exception e){
throw e;
}
}
private class crossReferenceButtonListener implements ActionListener {
public void actionPerformed (ActionEvent event){
try {
XSSFSheet sSheet = getsSheet();
XSSFSheet logSheet = getLogSheet();
ref = new CrossReference();
//ref = new CrossReference (log.getSheetAt(0),
sheet.getSheetAt(0));
ref.CrossReferenceCont(logSheet, sSheet);
ref.CrossRef();
JOptionPane.showMessageDialog(null, "Cross Reference was
successful!");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
CrossReferenceCont method
public void CrossReferenceCont(XSSFSheet wsLog, XSSFSheet wsSheet)
throws Exception {
log = new PropertyList(wsLog);
sheet = new PropertyList(wsSheet);
logArray = log.getPropertyList();
sheetArray = log.getPropertyList();
}
property list mehtod:
public PropertyList(XSSFSheet ws) throws Exception{
try{
//File excel = new File (excelFilePath);
//FileInputStream fis = new FileInputStream (excel);
// XSSFWorkbook wb = new XSSFWorkbook(fis);
int rowNum = ws.getLastRowNum();
System.out.println ("Number of rows is " + rowNum);
propertyList = new ArrayList <Property>(rowNum);
for (int i = 0; i <= rowNum; i++){
property = new Property (ws, i);
propertyList.add(new Property(property));
}
and finaly the property method: here where program crashes
public Property(XSSFSheet sheet, int row) throws FileNotFoundException {
Cell cell = sheet.getRow(row).getCell(0); //crashes here, nullPointer
int type = cell.getCellType();
//setting the address-------------------------------------------------
if (type == Cell.CELL_TYPE_STRING) {
address = cell.getStringCellValue();
} else if (type == Cell.CELL_TYPE_NUMERIC) {
int addressTemp = (int) cell.getNumericCellValue();
address = String.valueOf(addressTemp);
}
No comments:
Post a Comment