Forum » Programiranje » [JAVA] apache poi, branje in pisanje v excel - problem
[JAVA] apache poi, branje in pisanje v excel - problem
worxer ::
Lep pozdrav, imam problem z sledeco kodo, torej problem je, ko brisem ali pisem nove podatke notri, takrat mi vrze error file not found, ampak program use pravilno nardit, noter zapise in se enkrat nalozi datoteko.
Vecji problem sele nastane, ko moram brisat doloceno vrstico, novo vrstico notr vstavt pa se enkrat prebrat excel, takrat mi pa vcasih neda notri nove vrstice.
Klicanje, ko zelim staro zbrisat in novo vstavit notri:
Koda:
Vecji problem sele nastane, ko moram brisat doloceno vrstico, novo vrstico notr vstavt pa se enkrat prebrat excel, takrat mi pa vcasih neda notri nove vrstice.
Klicanje, ko zelim staro zbrisat in novo vstavit notri:
excel(2); excel(1); excel(0); pane.setEnabled(true);
Koda:
static String[] novvnos = { "", "", "", "", "", "", ""}; private static void excel(int i) { FileInputStream input = null; HSSFWorkbook workbook = null; try { input = new FileInputStream(new File("baza.xls")); workbook = new HSSFWorkbook(input); } catch (FileNotFoundException e) { System.out.println("error 1"); e.printStackTrace(); System.out.println("error 1"); } catch (IOException e) { e.printStackTrace(); } HSSFSheet sheet = workbook.getSheet("Popis pnevmatik"); //Branje if (i == 0) { for(int y = listModel.getRowCount()-1; y >= 0; y--) { listModel.removeRow(y); } Iterator<Row> rowIterator = sheet.iterator(); int x = 0; boolean iskanje = false; while(rowIterator.hasNext()) { iskanje = false; x = 0; Row row = rowIterator.next(); Iterator<Cell> cellIterator = row.cellIterator(); while(cellIterator.hasNext()) { Cell cell = cellIterator.next(); if (x != 7) { switch(cell.getCellType()) { case Cell.CELL_TYPE_BLANK: novvnos[x] = cell.getStringCellValue(); break; case Cell.CELL_TYPE_ERROR: novvnos[x] = cell.getStringCellValue(); break; case Cell.CELL_TYPE_FORMULA: novvnos[x] = cell.getStringCellValue(); break; case Cell.CELL_TYPE_BOOLEAN: novvnos[x] = cell.getStringCellValue(); break; case Cell.CELL_TYPE_NUMERIC: novvnos[x] = cell.getStringCellValue(); break; case Cell.CELL_TYPE_STRING: novvnos[x] = cell.getStringCellValue(); break; } String s1 = novvnos[x].toLowerCase(); String s2 = tfsearch.getText().toLowerCase(); String s3 = (String)cbSearch.getSelectedItem(); if (iskanje == false) { if ((cbSearch.getSelectedIndex() == 0)&&(s1.indexOf(s2) >= 0 )) { iskanje = true; } else if ((s1.indexOf(s2) >= 0 ) && (novvnos[1].indexOf(s3) >= 0)) { iskanje = true; } } } x++; } if ((novvnos[0] != "") && (iskanje == true)) { listModel.addRow(novvnos); iskanje = false; novvnos[1] = ""; } } } //pisanje if (i == 1) { int iLeto = Calendar.getInstance().get(Calendar.YEAR); int iDan = Calendar.getInstance().get(Calendar.DAY_OF_MONTH); int iMesec = Calendar.getInstance().get(Calendar.MONTH) + 1; Map<String, Object[]> data = new HashMap<String, Object[]>(); sheet.shiftRows(0, sheet.getLastRowNum(), 1); data.put("1", new Object[] {iDan + "." + iMesec + " " + iLeto, (String)cbVrsta.getSelectedItem(), (String)cbFirma.getSelectedItem(), (String)tfModel.getText(), (String)cbDim1.getSelectedItem() + "/" + (String)cbDim2.getSelectedItem() + "R" + (String)cbDim3.getSelectedItem(), spZaloga.getValue().toString(), spProdajna.getValue().toString(), spKupna.getValue().toString() }); Set<String> keyset = data.keySet(); int rownum = 0; for (String key : keyset) { Row row = sheet.createRow(rownum++); Object[] objArr = data.get(key); int cellnum = 0; for (Object obj : objArr) { Cell cell = row.createCell(cellnum++); if(obj instanceof Date) cell.setCellValue((Date)obj); else if(obj instanceof Boolean) cell.setCellValue((Boolean)obj); else if(obj instanceof String) cell.setCellValue((String)obj); else if(obj instanceof Double) cell.setCellValue((Double)obj); } } } if (i == 2) { int lastRowNum=sheet.getLastRowNum(); if(rowIndex>=0&&rowIndex<lastRowNum){ sheet.shiftRows(rowIndex+1,lastRowNum, -1); } if(rowIndex==lastRowNum){ HSSFRow removingRow=sheet.getRow(rowIndex); if(removingRow!=null){ sheet.removeRow(removingRow); } } } if (i == 3) { Row myRow = sheet.getRow(rowIndex); Cell myCell = myRow.getCell(1); String cellValue = myCell.getStringCellValue(); cbVrsta.setSelectedItem(cellValue); myCell = myRow.getCell(2); cellValue = myCell.getStringCellValue(); cbFirma.setSelectedItem(cellValue); myCell = myRow.getCell(3); cellValue = myCell.getStringCellValue(); tfModel.setText(cellValue); myCell = myRow.getCell(4); cellValue = myCell.getStringCellValue(); cbDim1.setSelectedItem(cellValue.substring(0, cellValue.lastIndexOf("/"))); cbDim2.setSelectedItem(cellValue.substring(cellValue.indexOf("/")+1, cellValue.lastIndexOf("R"))); cbDim3.setSelectedItem(cellValue.substring(cellValue.lastIndexOf("R")+1)); myCell = myRow.getCell(5); cellValue = myCell.getStringCellValue(); spZaloga.setValue(Integer.parseInt(cellValue)); myCell = myRow.getCell(6); cellValue = myCell.getStringCellValue(); spProdajna.setValue(Integer.parseInt(cellValue)); myCell = myRow.getCell(7); cellValue = myCell.getStringCellValue(); spKupna.setValue(Integer.parseInt(cellValue)); } try { input.close(); FileOutputStream out = new FileOutputStream(new File("baza.xls")); workbook.write(out); out.close(); } catch (FileNotFoundException e1) { System.out.println("error 2"); e1.printStackTrace(); System.out.println("error 2"); } catch (IOException e1) { System.out.println("error 2.2"); e1.printStackTrace(); System.out.println("error 2.2"); } }
worxer ::
Tko mi vrze error, ob tistem, ko zelim izbrisat, dat novo notri, pa ponovno nalozit
error 2 error 2 java.io.FileNotFoundException: baza.xls (The requested operation cannot be performed on a file with a user-mapped section open) at java.io.FileOutputStream.open(Native Method) at java.io.FileOutputStream.<init>(Unknown Source) at java.io.FileOutputStream.<init>(Unknown Source) at Vulkanizerstvo.excel(Vulkanizerstvo.java:433) at Vulkanizerstvo.actionPerformed(Vulkanizerstvo.java:516) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$200(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)
worxer ::
razmišlausm o možnosti, da mogoč datoteka ni dokonca shranjena, da bi dau timer nakoncu "funkcije" za ene dve sekundi, preden se izvede funkcija ponovno ... kaj pravte?
Vredno ogleda ...
Tema | Ogledi | Zadnje sporočilo | |
---|---|---|---|
Tema | Ogledi | Zadnje sporočilo | |
» | [Java] Urejanje baze z JTableOddelek: Programiranje | 1015 (799) | vonNeumann |
» | [Visual C#] TableAdapterOddelek: Programiranje | 1134 (1010) | detroit |
» | Javascript - izračun razlike v datumihOddelek: Programiranje | 1914 (1769) | kogledom |
» | Calc/excel delo s tabeloOddelek: Programiranje | 1455 (1222) | salabajs |
» | Visual Basic in ExcelOddelek: Programiranje | 2674 (2240) | Vesoljc |